Skip to content

Commit 856ffc2

Browse files
authored
Merge pull request #8 from compas-dev/dev_header
Dev header
2 parents c4f6390 + 92d4f92 commit 856ffc2

File tree

2 files changed

+35
-9
lines changed

2 files changed

+35
-9
lines changed

CHANGELOG.md

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

1010
### Added
1111

12+
* Added flag `dev` to `update-gh-header`.
13+
* Added argument `venv` to `update-gh-header`.
14+
* Added argument `envs` to `update-gh-header`.
15+
1216
### Changed
1317

1418
### Removed

src/compas_invocations2/grasshopper.py

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
"""
88

99
import os
10+
import re
1011
import shutil
1112
import tempfile
1213
from pathlib import Path
@@ -78,7 +79,7 @@ def _get_package_name(toml_file: str) -> str:
7879

7980
name = pyproject_data.get("project", {}).get("name", None)
8081
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?")
8283
return name
8384

8485

@@ -211,24 +212,45 @@ def publish_yak(ctx, yak_file: str, test_server: bool = False):
211212
ctx.run(f"{yak_exe_path} push {yak_file}")
212213

213214

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):
216228
"""Update the minimum version header of all CPython Grasshopper components."""
217229
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)
220230

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")
222243

223244
for file in Path(ctx.ghuser_cpython.source_dir).glob("**/code.py"):
224245
try:
225246
with open(file, "r", encoding="utf-8") as f:
226247
original_content = f.readlines()
248+
227249
with open(file, "w", encoding="utf-8") as f:
250+
for line in new_header:
251+
f.write(line)
228252
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):
232254
f.write(line)
233255
print(f"✅ Updated: {file}")
234256
except Exception as e:

0 commit comments

Comments
 (0)