|
| 1 | +#!/usr/bin/env python3 |
| 2 | +"""Version management helper for ontoenv-rs. |
| 3 | +
|
| 4 | +- Without arguments: print the current workspace version. |
| 5 | +- With a version string: update all manifests and rebuild lockfiles. |
| 6 | +""" |
| 7 | + |
| 8 | +from __future__ import annotations |
| 9 | + |
| 10 | +import argparse |
| 11 | +import os |
| 12 | +import subprocess |
| 13 | +import sys |
| 14 | +import re |
| 15 | +from pathlib import Path |
| 16 | + |
| 17 | +ROOT = Path(__file__).resolve().parent |
| 18 | +CARGO_TOML = ROOT / "Cargo.toml" |
| 19 | +PYONTOENV_PYPROJECT = ROOT / "pyontoenv-shim" / "pyproject.toml" |
| 20 | + |
| 21 | + |
| 22 | +def read_current_version() -> str: |
| 23 | + text = (ROOT / "Cargo.toml").read_text(encoding="utf-8").splitlines() |
| 24 | + in_workspace = False |
| 25 | + for raw in text: |
| 26 | + line = raw.strip() |
| 27 | + if line.startswith("[") and line.endswith("]"): |
| 28 | + in_workspace = line == "[workspace.package]" |
| 29 | + continue |
| 30 | + if not in_workspace: |
| 31 | + continue |
| 32 | + if line.startswith("version"): |
| 33 | + match = re.search(r'version\s*=\s*"([^"]+)"', line) |
| 34 | + if match: |
| 35 | + return match.group(1) |
| 36 | + raise SystemExit("Unable to find workspace.package.version in Cargo.toml") |
| 37 | + |
| 38 | + |
| 39 | +def write_if_contains(path: Path, old: str, new: str) -> bool: |
| 40 | + text = path.read_text(encoding="utf-8") |
| 41 | + if old not in text: |
| 42 | + return False |
| 43 | + path.write_text(text.replace(old, new), encoding="utf-8") |
| 44 | + return True |
| 45 | + |
| 46 | + |
| 47 | +def update_versions(old: str, new: str) -> None: |
| 48 | + touched = [] |
| 49 | + for path in (CARGO_TOML, PYONTOENV_PYPROJECT): |
| 50 | + if write_if_contains(path, old, new): |
| 51 | + touched.append(path) |
| 52 | + if not touched: |
| 53 | + print("warning: no files updated; version string may already be current", file=sys.stderr) |
| 54 | + |
| 55 | + |
| 56 | +def run_command(cmd: list[str], cwd: Path | None = None) -> None: |
| 57 | + display = " ".join(cmd) |
| 58 | + print(f"+ {display}", flush=True) |
| 59 | + env = os.environ.copy() |
| 60 | + env.setdefault("UV_CACHE_DIR", str(ROOT / ".uv-cache")) |
| 61 | + env.setdefault("UV_SYSTEM_RESOLVE", "0") |
| 62 | + try: |
| 63 | + subprocess.run(cmd, cwd=cwd, check=True, env=env) |
| 64 | + except subprocess.CalledProcessError as exc: |
| 65 | + raise SystemExit(f"{display} failed with exit code {exc.returncode}") from exc |
| 66 | + |
| 67 | + |
| 68 | +def rebuild() -> None: |
| 69 | + run_command(["cargo", "check"]) |
| 70 | + run_command(["cargo", "build"]) |
| 71 | + run_command(["uv", "lock"], cwd=ROOT / "python") |
| 72 | + run_command(["uv", "lock"], cwd=ROOT / "pyontoenv-shim") |
| 73 | + |
| 74 | + |
| 75 | +def main() -> None: |
| 76 | + parser = argparse.ArgumentParser(description="Manage workspace version.") |
| 77 | + parser.add_argument("version", nargs="?", help="New version string to apply.") |
| 78 | + args = parser.parse_args() |
| 79 | + |
| 80 | + current = read_current_version() |
| 81 | + if not args.version: |
| 82 | + print(current) |
| 83 | + return |
| 84 | + |
| 85 | + new = args.version |
| 86 | + if new == current: |
| 87 | + print(f"version already {current}") |
| 88 | + rebuild() |
| 89 | + return |
| 90 | + |
| 91 | + update_versions(current, new) |
| 92 | + rebuild() |
| 93 | + |
| 94 | + |
| 95 | +if __name__ == "__main__": |
| 96 | + main() |
0 commit comments