|
9 | 9 | import os |
10 | 10 | import shutil |
11 | 11 | import tempfile |
| 12 | +from pathlib import Path |
12 | 13 |
|
13 | 14 | import invoke |
14 | 15 | import requests |
@@ -69,6 +70,18 @@ def _get_version_from_toml(toml_file: str) -> str: |
69 | 70 | return version |
70 | 71 |
|
71 | 72 |
|
| 73 | +def _get_package_name(toml_file: str) -> str: |
| 74 | + with open(toml_file, "r") as f: |
| 75 | + pyproject_data = tomlkit.load(f) |
| 76 | + if not pyproject_data: |
| 77 | + raise invoke.Exit("Failed to load pyproject.toml.") |
| 78 | + |
| 79 | + name = pyproject_data.get("project", {}).get("name", None) |
| 80 | + if not name: |
| 81 | + raise invoke.Exit("Failed to get package name from pyproject.toml.") |
| 82 | + return name |
| 83 | + |
| 84 | + |
72 | 85 | def _get_user_object_path(context): |
73 | 86 | if hasattr(context, "ghuser_cpython"): |
74 | 87 | print("checking ghuser_cpython") |
@@ -196,3 +209,27 @@ def publish_yak(ctx, yak_file: str, test_server: bool = False): |
196 | 209 | ctx.run(f"{yak_exe_path} push --source https://test.yak.rhino3d.com {yak_file}") |
197 | 210 | else: |
198 | 211 | ctx.run(f"{yak_exe_path} push {yak_file}") |
| 212 | + |
| 213 | + |
| 214 | +@invoke.task(help={"version": "New minimum version to set in the header. If not provided, current version is used."}) |
| 215 | +def update_gh_header(ctx, version=None): |
| 216 | + """Update the minimum version header of all CPython Grasshopper components.""" |
| 217 | + toml_filepath = os.path.join(ctx.base_folder, "pyproject.toml") |
| 218 | + version = version or _get_version_from_toml(toml_filepath) |
| 219 | + package_name = _get_package_name(toml_filepath) |
| 220 | + |
| 221 | + new_header = f"# r: {package_name}>={version}" |
| 222 | + |
| 223 | + for file in Path(ctx.ghuser_cpython.source_dir).glob("**/code.py"): |
| 224 | + try: |
| 225 | + with open(file, "r", encoding="utf-8") as f: |
| 226 | + original_content = f.readlines() |
| 227 | + with open(file, "w", encoding="utf-8") as f: |
| 228 | + for line in original_content: |
| 229 | + if line.startswith(f"# r: {package_name}"): |
| 230 | + f.write(new_header + "\n") |
| 231 | + else: |
| 232 | + f.write(line) |
| 233 | + print(f"✅ Updated: {file}") |
| 234 | + except Exception as e: |
| 235 | + print(f"❌ Failed to update {file}: {e}") |
0 commit comments