|
7 | 7 | """ |
8 | 8 |
|
9 | 9 | import os |
| 10 | +import re |
10 | 11 | import shutil |
11 | 12 | import tempfile |
12 | 13 | from pathlib import Path |
@@ -78,7 +79,7 @@ def _get_package_name(toml_file: str) -> str: |
78 | 79 |
|
79 | 80 | name = pyproject_data.get("project", {}).get("name", None) |
80 | 81 | if not name: |
81 | | - raise invoke.Exit("Failed to get package name from pyproject.toml.") |
| 82 | + raise invoke.Exit("Failed to get package name. Is your pyproject.toml missing a '[project]' section?") |
82 | 83 | return name |
83 | 84 |
|
84 | 85 |
|
@@ -211,24 +212,45 @@ def publish_yak(ctx, yak_file: str, test_server: bool = False): |
211 | 212 | ctx.run(f"{yak_exe_path} push {yak_file}") |
212 | 213 |
|
213 | 214 |
|
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): |
| 215 | +def _is_header_line(line: str) -> bool: |
| 216 | + return re.match(r"^#\s+(r|venv|env):", line) is not None |
| 217 | + |
| 218 | + |
| 219 | +@invoke.task( |
| 220 | + help={ |
| 221 | + "version": "New minimum version to set in the header. If not provided, current version is used.", |
| 222 | + "venv": "(Optional) Name of the Rhino virtual environment to use in the components.", |
| 223 | + "dev": "(Defaults to False) If True, the dependency header is ommitted and path to repo is added instead.", |
| 224 | + "envs": "(Optional) List of environments, delimited with `;` which will be added to path using `# env:`.", |
| 225 | + } |
| 226 | +) |
| 227 | +def update_gh_header(ctx, version: str = None, venv: str = None, dev: bool = False, envs: str = None): |
216 | 228 | """Update the minimum version header of all CPython Grasshopper components.""" |
217 | 229 | 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 | 230 |
|
221 | | - new_header = f"# r: {package_name}>={version}" |
| 231 | + new_header = [] |
| 232 | + if not dev: |
| 233 | + version = version or _get_version_from_toml(toml_filepath) |
| 234 | + package_name = _get_package_name(toml_filepath) |
| 235 | + new_header.append(f"# r: {package_name}>={version}\n") |
| 236 | + if venv: |
| 237 | + new_header.append(f"# venv: {venv}\n") |
| 238 | + if envs: |
| 239 | + for env in envs.split(";"): |
| 240 | + new_header.append(f"# env: {env.strip()}\n") |
| 241 | + if dev: |
| 242 | + new_header.append(f"# env: {os.path.join(ctx.base_folder, 'src')}\n") |
222 | 243 |
|
223 | 244 | for file in Path(ctx.ghuser_cpython.source_dir).glob("**/code.py"): |
224 | 245 | try: |
225 | 246 | with open(file, "r", encoding="utf-8") as f: |
226 | 247 | original_content = f.readlines() |
| 248 | + |
227 | 249 | with open(file, "w", encoding="utf-8") as f: |
| 250 | + for line in new_header: |
| 251 | + f.write(line) |
228 | 252 | for line in original_content: |
229 | | - if line.startswith(f"# r: {package_name}"): |
230 | | - f.write(new_header + "\n") |
231 | | - else: |
| 253 | + if not _is_header_line(line): |
232 | 254 | f.write(line) |
233 | 255 | print(f"✅ Updated: {file}") |
234 | 256 | except Exception as e: |
|
0 commit comments