|
1 | 1 | #!/usr/bin/env python3
|
| 2 | +import json |
2 | 3 | import re
|
3 | 4 | import shutil
|
4 | 5 | import subprocess
|
@@ -74,30 +75,55 @@ def cmake_install(build_dir: Path) -> None:
|
74 | 75 |
|
75 | 76 |
|
76 | 77 | @cache
|
77 |
| -def get_git_repo_version() -> str: |
| 78 | +def get_git_commit_hash(identifier: str | None = None) -> str: |
78 | 79 | """
|
79 |
| - Gets a version string representing the current state of the git repo. |
| 80 | + Gets the full commit hash of the current git repo. |
80 | 81 |
|
| 82 | + Args: |
| 83 | + identifier: The identifier of the commit to get, or None to get the latest. |
81 | 84 | Returns:
|
82 |
| - The version string. |
| 85 | + The commit hash. |
83 | 86 | """
|
84 |
| - commit_hash = subprocess.run( |
85 |
| - ["git", "show", "-s", "--format=%H"], |
| 87 | + args = ["git", "show", "-s", "--format=%H"] |
| 88 | + if identifier is not None: |
| 89 | + args.append(identifier) |
| 90 | + |
| 91 | + return subprocess.run( |
| 92 | + args, |
| 93 | + cwd=Path(__file__).parent, |
86 | 94 | check=True,
|
87 | 95 | stdout=subprocess.PIPE,
|
88 | 96 | encoding="utf8",
|
89 | 97 | ).stdout.strip()
|
90 | 98 |
|
| 99 | + |
| 100 | +@cache |
| 101 | +def check_git_is_dirty() -> bool: |
| 102 | + """ |
| 103 | + Checks if the git repo is dirty. |
| 104 | +
|
| 105 | + Returns: |
| 106 | + True if the repo is dirty. |
| 107 | + """ |
91 | 108 | # This command returns the list of modified files, so any output means dirty
|
92 |
| - is_dirty = any( |
| 109 | + return any( |
93 | 110 | subprocess.run(
|
94 | 111 | ["git", "status", "--porcelain"],
|
| 112 | + cwd=Path(__file__).parent, |
95 | 113 | check=True,
|
96 | 114 | stdout=subprocess.PIPE,
|
97 | 115 | ).stdout,
|
98 | 116 | )
|
99 | 117 |
|
100 |
| - return commit_hash[:8] + (", dirty" if is_dirty else "") |
| 118 | + |
| 119 | +def get_git_repo_version() -> str: |
| 120 | + """ |
| 121 | + Gets a version string representing the current state of the git repo. |
| 122 | +
|
| 123 | + Returns: |
| 124 | + The version string. |
| 125 | + """ |
| 126 | + return get_git_commit_hash()[:8] + (", dirty" if check_git_is_dirty() else "") |
101 | 127 |
|
102 | 128 |
|
103 | 129 | def iter_mod_files(mod_folder: Path, debug: bool) -> Iterator[Path]:
|
@@ -132,21 +158,31 @@ def iter_mod_files(mod_folder: Path, debug: bool) -> Iterator[Path]:
|
132 | 158 |
|
133 | 159 |
|
134 | 160 | def _zip_init_script(zip_file: ZipFile) -> None:
|
135 |
| - output_init_script = ZIP_MODS_FOLDER / INIT_SCRIPT.name |
136 |
| - zip_file.write(INIT_SCRIPT, output_init_script) |
137 |
| - unrealsdk_env = ( |
138 |
| - # Path.relative_to doesn't work when where's no common base, need to use os.path |
139 |
| - # While the file goes in the plugins folder, this path is relative to *the executable* |
140 |
| - f"PYUNREALSDK_INIT_SCRIPT={path.relpath(output_init_script, ZIP_EXECUTABLE_FOLDER)}\n" |
141 |
| - f"PYUNREALSDK_PYEXEC_ROOT={path.relpath(ZIP_MODS_FOLDER, ZIP_EXECUTABLE_FOLDER)}\n" |
142 |
| - ) |
| 161 | + zip_file.write(INIT_SCRIPT, ZIP_MODS_FOLDER / INIT_SCRIPT.name) |
| 162 | + |
| 163 | + |
| 164 | +def _zip_config_file(zip_file: ZipFile) -> None: |
| 165 | + # Path.relative_to doesn't work when where's no common base, need to use os.path |
| 166 | + # While the file goes in the plugins folder, this path is relative to *the executable* |
| 167 | + init_script_path = path.relpath(ZIP_MODS_FOLDER / INIT_SCRIPT.name, ZIP_EXECUTABLE_FOLDER) |
| 168 | + pyexec_root = path.relpath(ZIP_MODS_FOLDER, ZIP_EXECUTABLE_FOLDER) |
143 | 169 |
|
144 |
| - # We also define the display version via an env var, do that here too |
145 | 170 | version_number = tomllib.loads(PYPROJECT_FILE.read_text())["project"]["version"]
|
146 | 171 | git_version = get_git_repo_version()
|
147 |
| - unrealsdk_env += f"MOD_MANAGER_DISPLAY_VERSION={version_number} ({git_version})\n" |
| 172 | + display_version = f"{version_number} ({git_version})" |
| 173 | + |
| 174 | + # Tomllib doesn't support dumping yet, so we have to create it as a string |
| 175 | + # Using `json.dumps` to escape strings, since it should be mostly compatible |
| 176 | + config = ( |
| 177 | + f"[pyunrealsdk]\n" |
| 178 | + f"init_script = {json.dumps(init_script_path)}\n" |
| 179 | + f"pyexec_root = {json.dumps(pyexec_root)}\n" |
| 180 | + f"\n" |
| 181 | + f"[mod_manager]\n" |
| 182 | + f"display_version = {json.dumps(display_version)}\n" |
| 183 | + ) |
148 | 184 |
|
149 |
| - zip_file.writestr(str(ZIP_PLUGINS_FOLDER / "unrealsdk.env"), unrealsdk_env) |
| 185 | + zip_file.writestr(str(ZIP_PLUGINS_FOLDER / "unrealsdk.toml"), config) |
150 | 186 |
|
151 | 187 |
|
152 | 188 | def _zip_mod_folders(zip_file: ZipFile, mod_folders: Sequence[Path], debug: bool) -> None:
|
@@ -247,6 +283,7 @@ def zip_release(
|
247 | 283 |
|
248 | 284 | with ZipFile(output, "w", ZIP_DEFLATED, compresslevel=9) as zip_file:
|
249 | 285 | _zip_init_script(zip_file)
|
| 286 | + _zip_config_file(zip_file) |
250 | 287 | _zip_mod_folders(zip_file, mod_folders, debug)
|
251 | 288 | _zip_stubs(zip_file)
|
252 | 289 | _zip_settings(zip_file)
|
@@ -287,21 +324,23 @@ def zip_release(
|
287 | 324 | )
|
288 | 325 | args = parser.parse_args()
|
289 | 326 |
|
| 327 | + if check_git_is_dirty(): |
| 328 | + print("WARNING: git repo is dirty") |
| 329 | + |
290 | 330 | install_dir = INSTALL_DIR_BASE / str(args.preset)
|
291 | 331 |
|
292 | 332 | if not args.skip_install:
|
293 | 333 | shutil.rmtree(install_dir, ignore_errors=True)
|
294 | 334 | cmake_install(BUILD_DIR_BASE / args.preset)
|
295 | 335 |
|
296 | 336 | assert install_dir.exists() and install_dir.is_dir(), "install dir doesn't exist"
|
297 |
| - |
298 | 337 | # Zip up all the requested files
|
299 |
| - COMMON_FOLDERS = (BASE_MOD, KEYBINDS, UI_UTILS) |
| 338 | + COMMON_FOLDERS = (BASE_MOD, CONSOLE_MENU, KEYBINDS, UI_UTILS) |
300 | 339 |
|
301 | 340 | for prefix, arg, mods in (
|
302 |
| - ("bl3", args.bl3, (*COMMON_FOLDERS, BL3_MENU, CONSOLE_MENU)), |
303 |
| - ("wl", args.wl, (*COMMON_FOLDERS, CONSOLE_MENU)), |
304 |
| - ("unified", args.unified, (*COMMON_FOLDERS, BL3_MENU, CONSOLE_MENU)), |
| 341 | + ("bl3", args.bl3, (*COMMON_FOLDERS, BL3_MENU)), |
| 342 | + ("wl", args.wl, COMMON_FOLDERS), |
| 343 | + ("unified", args.unified, (*COMMON_FOLDERS, BL3_MENU)), |
305 | 344 | ):
|
306 | 345 | if not arg:
|
307 | 346 | continue
|
|
0 commit comments