#!/usr/bin/env python3 """ hyperstitionstud_io.version_sync — Single-Source-of-Truth Version Synchronizer & Verifier (`hs-sync-version`). Ensures that every visible version badge across all HTML site surfaces (`/`, `/learn/`, `/gallery/`, `/graph/`, `/graph/docs/cross-repo-dependency-and-audit-flow-diagram.html`, `/calc/`), JS bundles, MCP/Quant servers, and package manifests updates in lockstep on every version change. """ from __future__ import annotations import argparse import json import re import sys from pathlib import Path from typing import Any _SRC_DIR = Path(__file__).resolve().parents[1] if _SRC_DIR.is_dir() and str(_SRC_DIR) not in sys.path: sys.path.insert(0, str(_SRC_DIR)) from hyperstitionstud_io import __version__ as CURRENT_VERSION # noqa: E402 from hyperstitionstud_io.config import load_workspace_config # noqa: E402 SEMVER_RE = r"\d+\.\d+\.\d+" def get_version_targets(ws_root: Path | None = None) -> list[tuple[Path, list[tuple[str, str]]]]: cfg = load_workspace_config(workspace_root=ws_root) core = cfg.core_dir kg = cfg.knowledge_graph_dir calc = cfg.paths["gematria_js_dir"] return [ ( core / "src" / "hyperstitionstud_io" / "__init__.py", [ (rf'\(v{SEMVER_RE}\)', "(v{V})"), (rf'__version__\s*=\s*"{SEMVER_RE}"', '__version__ = "{V}"'), ], ), ( core / "pyproject.toml", [(rf'^version\s*=\s*"{SEMVER_RE}"', 'version = "{V}"')], ), ( core / "package.json", [(rf'"version":\s*"{SEMVER_RE}"', '"version": "{V}"')], ), ( core / "packages-js" / "ciphers-aq" / "package.json", [(rf'"version":\s*"{SEMVER_RE}"', '"version": "{V}"')], ), ( core / "workspace.example.toml", [(rf'schema_version\s*=\s*"{SEMVER_RE}"', 'schema_version = "{V}"')], ), ( core / "site" / "index.html", [ (rf'urn:io:hyperstitionstud \(v{SEMVER_RE}\)', "urn:io:hyperstitionstud (v{V})"), (rf'urn:io:hyperstitionstud v{SEMVER_RE}\)', "urn:io:hyperstitionstud v{V})"), ], ), ( core / "site" / "learn" / "index.html", [ (rf'learn-progress-state v{SEMVER_RE}\)', "learn-progress-state v{V})"), ], ), ( core / "site" / "learn" / "learn-app.js", [ (rf'version:\s*"{SEMVER_RE}"', 'version: "{V}"'), ], ), ( core / "site" / "quant" / "index.html", [ (rf'urn:io:hyperstitionstud:quant-timeseries:v{SEMVER_RE}\)', "urn:io:hyperstitionstud:quant-timeseries:v{V})"), (rf'hyperstitionstud\.io/quant v{SEMVER_RE}', "hyperstitionstud.io/quant v{V}"), ], ), ( core / "site" / "quant" / "quant-app.js", [ (rf'Time-Series Workbench \(v{SEMVER_RE}\)', "Time-Series Workbench (v{V})"), ], ), ( core / "site" / "quant" / "quant-tutorials.js", [ (rf'Deep-Link Choreographer \(v{SEMVER_RE}\)', "Deep-Link Choreographer (v{V})"), (rf'version:\s*"{SEMVER_RE}"', 'version: "{V}"'), ], ), ( core / "site" / "donate" / "index.html", [ (rf'urn:io:hyperstitionstud:donate:v{SEMVER_RE}', "urn:io:hyperstitionstud:donate:v{V}"), (rf'hyperstitionstud\.io/donate v{SEMVER_RE}', "hyperstitionstud.io/donate v{V}"), ], ), ( core / "site" / "gallery" / "index.html", [ (rf'urn:io:hyperstitionstud:gallery:{SEMVER_RE}\)', "urn:io:hyperstitionstud:gallery:{V})"), ], ), ( core / "scripts" / "build_gallery_manifest.py", [ (rf'"version":\s*"{SEMVER_RE}"', '"version": "{V}"'), (rf'urn:io:hyperstitionstud:gallery:{SEMVER_RE}', "urn:io:hyperstitionstud:gallery:{V}"), ], ), ( core / "src" / "hyperstitionstud_io" / "mcp" / "server.py", [ (rf'"version":\s*"{SEMVER_RE}"', '"version": "{V}"'), (rf'urn:io:hyperstitionstud:mcp:{SEMVER_RE}', "urn:io:hyperstitionstud:mcp:{V}"), ], ), ( core / "src" / "hyperstitionstud_io" / "quant" / "collector.py", [ (rf'"version":\s*"{SEMVER_RE}"', '"version": "{V}"'), ], ), ( kg / "index.html", [ (rf'Interactive Knowledge Graph \(v{SEMVER_RE}\)', "Interactive Knowledge Graph (v{V})"), (rf'Zero Glossary Leak(?: • v{SEMVER_RE})?
', "Zero Glossary Leak • v{V}"), (rf'(?:hyperstitionstud\.io/graph v{SEMVER_RE} • )?Made with dict[str, Any]: v = target_version.lstrip("v").strip() if not re.match(rf"^{SEMVER_RE}$", v): raise ValueError(f"Invalid semantic version '{target_version}' (expected X.Y.Z)") cfg = load_workspace_config(workspace_root=ws_root) targets = get_version_targets(ws_root=ws_root) modified_files: list[str] = [] mismatched_files: list[str] = [] for path, patterns in targets: if not path.is_file(): continue orig = path.read_text(encoding="utf-8") updated = orig for pat_re, repl_tmpl in patterns: repl = repl_tmpl.replace("{V}", v) updated = re.sub(pat_re, repl, updated, flags=re.M) if f"v{v}" not in updated and f'"{v}"' not in updated and f":{v}" not in updated: mismatched_files.append(str(path)) elif updated != orig: if check_only: mismatched_files.append(str(path)) else: path.write_text(updated, encoding="utf-8") modified_files.append(str(path)) if not check_only: ws_toml = cfg.workspace_root / "workspace.toml" if ws_toml.is_file(): orig_ws = ws_toml.read_text(encoding="utf-8") upd_ws = re.sub(rf'schema_version\s*=\s*"{SEMVER_RE}"', f'schema_version = "{v}"', orig_ws, flags=re.M) if upd_ws != orig_ws: ws_toml.write_text(upd_ws, encoding="utf-8") modified_files.append(str(ws_toml)) return { "target_version": v, "check_only": check_only, "all_in_sync": len(mismatched_files) == 0, "modified_files": modified_files, "mismatched_files": mismatched_files, } def main(argv: list[str] | None = None) -> int: ap = argparse.ArgumentParser(prog="hs-sync-version", description="Synchronize or verify version numbers across all site elements and manifests.") ap.add_argument("--set", dest="set_version", default=None, help="Set new version X.Y.Z across all site HTML elements, JS bundles, and manifests.") ap.add_argument("--check", action="store_true", help="Verify that all site elements match hyperstitionstud_io.__version__ without modifying files.") args = ap.parse_args(argv) target_v = args.set_version.lstrip("v") if args.set_version else CURRENT_VERSION res = sync_workspace_version(target_v, check_only=args.check) print(json.dumps(res, indent=2)) return 0 if res["all_in_sync"] else 1 if __name__ == "__main__": raise SystemExit(main())