Skip to content

Commit 84fb5b9

Browse files
authored
Merge pull request #7 from compas-dev/gh_header
added script to update gh component header
2 parents 9169531 + 75973dc commit 84fb5b9

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
99

1010
### Added
1111

12+
* Added task `update-gh-header` to `grasshopper` tasks.
13+
1214
### Changed
1315

1416
### Removed

src/compas_invocations2/grasshopper.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import os
1010
import shutil
1111
import tempfile
12+
from pathlib import Path
1213

1314
import invoke
1415
import requests
@@ -69,6 +70,18 @@ def _get_version_from_toml(toml_file: str) -> str:
6970
return version
7071

7172

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+
7285
def _get_user_object_path(context):
7386
if hasattr(context, "ghuser_cpython"):
7487
print("checking ghuser_cpython")
@@ -196,3 +209,27 @@ def publish_yak(ctx, yak_file: str, test_server: bool = False):
196209
ctx.run(f"{yak_exe_path} push --source https://test.yak.rhino3d.com {yak_file}")
197210
else:
198211
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

Comments
 (0)