|
| 1 | +#!/usr/bin/env python3 |
| 2 | +"""Regenerate every checked-in generated artifact, atomically. |
| 3 | +
|
| 4 | +Run from anywhere: |
| 5 | +
|
| 6 | + uv run --project ~/Development/vgi-python python scripts/regen_generated.py |
| 7 | + uv run --project ~/Development/vgi-python python scripts/regen_generated.py --check |
| 8 | +
|
| 9 | +Why this exists rather than a shell one-liner per artifact: the obvious form, |
| 10 | +``python -m vgi.codegen.X > path/to/generated.file``, truncates the destination |
| 11 | +*before* the generator runs, so any failure -- a syntax error, a missing |
| 12 | +sibling checkout, a typo in the module name -- silently destroys the file it |
| 13 | +was meant to update, and the loss is invisible until a test or a build fails |
| 14 | +somewhere else. Each artifact here is rendered into memory, checked non-empty, |
| 15 | +and only then written. |
| 16 | +
|
| 17 | +Targets in sibling repos that are not checked out are skipped, not failed: |
| 18 | +nobody has all of them. |
| 19 | +""" |
| 20 | + |
| 21 | +from __future__ import annotations |
| 22 | + |
| 23 | +import argparse |
| 24 | +import importlib |
| 25 | +import io |
| 26 | +import sys |
| 27 | +from pathlib import Path |
| 28 | + |
| 29 | +#: (codegen module, destination relative to the sibling-repo root, repo dir name) |
| 30 | +#: ``vgi-java`` lives at ``~/vgi-java`` for some checkouts and beside the other |
| 31 | +#: SDKs for others; both are probed. |
| 32 | +_TARGETS: list[tuple[str, str, str]] = [ |
| 33 | + ("vgi.codegen.cpp_schemas", "src/generated/vgi_protocol_schemas.hpp", "vgi"), |
| 34 | + ("vgi.codegen.cpp_request_builders", "src/generated/vgi_request_builders.hpp", "vgi"), |
| 35 | + ("vgi.codegen.cpp_constants", "src/generated/vgi_protocol_constants.hpp", "vgi"), |
| 36 | + ("vgi.codegen.cpp_protocol_version", "src/generated/vgi_protocol_version.hpp", "vgi"), |
| 37 | + ("vgi.codegen.go_schemas", "vgi/generated/protocol_schemas.go", "vgi-go"), |
| 38 | + ("vgi.codegen.rust_schemas", "vgi-protocol/src/generated/protocol_schemas.rs", "vgi-rust"), |
| 39 | + ("vgi.codegen.rust_request_builders", "vgi-protocol/src/generated/request_params.rs", "vgi-rust"), |
| 40 | + ("vgi.codegen.ts_schemas", "src/generated/vgi-protocol-schemas.ts", "vgi-typescript"), |
| 41 | + ("vgi.codegen.ts_client", "src/generated/vgi-client.ts", "vgi-typescript"), |
| 42 | + ( |
| 43 | + "vgi.codegen.java_schemas", |
| 44 | + "vgi/src/test/java/farm/query/vgi/generated/VgiProtocolSchemas.java", |
| 45 | + "vgi-java", |
| 46 | + ), |
| 47 | +] |
| 48 | + |
| 49 | + |
| 50 | +def _repo_root(name: str) -> Path | None: |
| 51 | + """Locate a sibling checkout, or ``None`` when it is not present.""" |
| 52 | + siblings = Path(__file__).resolve().parents[2] |
| 53 | + for candidate in (siblings / name, Path.home() / name): |
| 54 | + if candidate.is_dir(): |
| 55 | + return candidate |
| 56 | + return None |
| 57 | + |
| 58 | + |
| 59 | +def _render(module_name: str) -> str: |
| 60 | + """Render one artifact into memory. Raises if the generator fails.""" |
| 61 | + module = importlib.import_module(module_name) |
| 62 | + buf = io.StringIO() |
| 63 | + module.emit(buf) |
| 64 | + text = buf.getvalue() |
| 65 | + if not text.strip(): |
| 66 | + raise RuntimeError(f"{module_name}.emit() produced no output") |
| 67 | + return text |
| 68 | + |
| 69 | + |
| 70 | +def main() -> int: |
| 71 | + """Regenerate (or check) every artifact; return a process exit code.""" |
| 72 | + parser = argparse.ArgumentParser(description=__doc__) |
| 73 | + parser.add_argument( |
| 74 | + "--check", |
| 75 | + action="store_true", |
| 76 | + help="report drift without writing anything (exit 1 if any file is stale)", |
| 77 | + ) |
| 78 | + args = parser.parse_args() |
| 79 | + |
| 80 | + stale, wrote, skipped, failed = [], [], [], [] |
| 81 | + |
| 82 | + for module_name, relative, repo in _TARGETS: |
| 83 | + root = _repo_root(repo) |
| 84 | + if root is None: |
| 85 | + skipped.append(f"{repo} (not checked out)") |
| 86 | + continue |
| 87 | + dest = root / relative |
| 88 | + try: |
| 89 | + text = _render(module_name) |
| 90 | + except Exception as exc: # noqa: BLE001 - report every failure, don't stop |
| 91 | + failed.append(f"{module_name}: {exc}") |
| 92 | + continue |
| 93 | + |
| 94 | + current = dest.read_text() if dest.exists() else None |
| 95 | + if current == text: |
| 96 | + continue |
| 97 | + if args.check: |
| 98 | + stale.append(str(dest)) |
| 99 | + continue |
| 100 | + dest.parent.mkdir(parents=True, exist_ok=True) |
| 101 | + # Write beside the target, then replace: a crash mid-write leaves the |
| 102 | + # previous version intact rather than a truncated file. |
| 103 | + tmp = dest.with_suffix(dest.suffix + ".tmp") |
| 104 | + tmp.write_text(text) |
| 105 | + tmp.replace(dest) |
| 106 | + wrote.append(str(dest)) |
| 107 | + |
| 108 | + for line in skipped: |
| 109 | + print(f"skip {line}") |
| 110 | + for line in wrote: |
| 111 | + print(f"wrote {line}") |
| 112 | + for line in stale: |
| 113 | + print(f"STALE {line}") |
| 114 | + for line in failed: |
| 115 | + print(f"FAIL {line}", file=sys.stderr) |
| 116 | + |
| 117 | + if failed: |
| 118 | + return 2 |
| 119 | + if stale: |
| 120 | + print(f"\n{len(stale)} file(s) stale. Run without --check to regenerate.", file=sys.stderr) |
| 121 | + return 1 |
| 122 | + if not args.check and not wrote: |
| 123 | + print("everything already up to date") |
| 124 | + return 0 |
| 125 | + |
| 126 | + |
| 127 | +if __name__ == "__main__": |
| 128 | + raise SystemExit(main()) |
0 commit comments