55import re
66import platform
77import shutil
8- from typing import Tuple
8+ from typing import Optional , Tuple
99import subprocess
1010import argparse
1111from pathlib import Path
@@ -23,18 +23,49 @@ def log(*args, **kwargs):
2323 print (* args , ** kwargs , file = sys .stderr )
2424
2525
26+ def get_default_godot_version_from_meson (build_dir : Path ) -> str :
27+ # Ultra lazy & fast json parsing ^^
28+ meson_build_options = build_dir / "meson-info/intro-buildoptions.json"
29+ version = (
30+ meson_build_options .read_text ()
31+ .split ('"name": "godot_version", "value": "' , 1 )[- 1 ]
32+ .split ('"' , 1 )[0 ]
33+ )
34+ assert version
35+ return version
36+
37+
38+ def parse_raw_version (raw_version : str ) -> GodotBinaryVersion :
39+ # Provided value is version information with format <major>.<minor>.<patch>[-<extra>]
40+ match = re .match (r"^([0-9]+)\.([0-9]+)\.([0-9]+)(?:-(\w+))?$" , raw_version )
41+ if match :
42+ major , minor , patch , extra = match .groups ()
43+ else :
44+ raise ValueError (
45+ f"`{ raw_version } ` is neither an existing file nor a valid <major>.<minor>.<patch>[-<extra>] Godot version format"
46+ )
47+ return (major , minor , patch , extra or "stable" )
48+
49+
2650def parse_godot_binary_hint (
2751 godot_binary_hint : str ,
28- ) -> Tuple [GodotBinaryPlatform , GodotBinaryVersion ]:
52+ ) -> Tuple [GodotBinaryPlatform , Optional [ GodotBinaryVersion ] ]:
2953 try :
3054 build_machine , raw_version = godot_binary_hint .split (":" )
3155 except ValueError :
3256 build_machine = ""
3357 raw_version = godot_binary_hint
34- raw_version = raw_version
35- build_machine = build_machine or platform .machine ()
3658
37- build_platform = f"{ platform .system ()} -{ build_machine } " .lower ()
59+ # e.g. `build_machine == "x86"`
60+ build_machine = (build_machine or platform .machine ()).lower ()
61+ # e.g. `build_platform_prefix == "windows-"`
62+ build_platform_prefix = f"{ platform .system ()} -" .lower ()
63+ if build_machine .startswith (build_platform_prefix ):
64+ build_platform = build_machine
65+ else :
66+ build_platform = build_platform_prefix + build_machine
67+ # e.g. `build_platform == "windows-x86"`
68+
3869 godot_build_platform = {
3970 "linux-x86_64" : "linux.x86_64" ,
4071 "linux-x64" : "linux.x86_64" ,
@@ -53,20 +84,16 @@ def parse_godot_binary_hint(
5384 f"Don't know how to download a Godot binary for your platform `{ build_platform } `"
5485 )
5586
56- # Provided value is version information with format <major>.<minor>.<patch>[-<extra>]
57- match = re .match (r"^([0-9]+)\.([0-9]+)\.([0-9]+)(?:-(\w+))?$" , raw_version )
58- if match :
59- major , minor , patch , extra = match .groups ()
60- else :
61- raise ValueError (
62- f"`{ raw_version } ` is neither an existing file nor a valid <major>.<minor>.<patch>[-<extra>] Godot version format"
63- )
64- return godot_build_platform , (major , minor , patch , extra or "stable" )
87+ # If version has not been provided, it will be determined later with `get_default_godot_version_from_meson()`
88+ version = parse_raw_version (raw_version ) if raw_version else None
89+ return godot_build_platform , version
6590
6691
6792def fetch_godot_binary (
68- build_dir : Path , godot_platform : GodotBinaryPlatform , version : GodotBinaryVersion
93+ build_dir : Path , godot_platform : GodotBinaryPlatform , version : Optional [ GodotBinaryVersion ]
6994) -> Path :
95+ if not version :
96+ version = parse_raw_version (get_default_godot_version_from_meson (build_dir ))
7097 major , minor , patch , extra = version
7198 strversion = f"{ major } .{ minor } .{ patch } " if patch != "0" else f"{ major } .{ minor } "
7299 binary_name = f"Godot_v{ strversion } -{ extra } _{ godot_platform } "
0 commit comments