Debian Codegraph @

scripts/bump_versions.py

raw
1 """Set the versions `uv version` cannot reach, the way a release does.
2
3 Two of them, and each is a literal because the thing that reads it cannot
4 afford to ask the package metadata: the plugin manifest is JSON that a harness
5 reads without Python at all, and `harlequin.hsql.protocol` is on the
6 warm-session client's path. So `release.yml` runs this beside the two versions
7 `uv version` does bump.
8
9 Usage:
10 python scripts/bump_versions.py 2.12.0
11
12 `tests/unit_tests/test_plugin.py` and `tests/unit_tests/test_hsql_session.py`
13 are what fail when a committed literal and the installed version disagree,
14 whether that is a version a release left behind or a hand edit.
15 """
16
17 from __future__ import annotations
18
19 import json
20 import re
21 import sys
22 from pathlib import Path
23
24 SOURCE_ROOT = Path(__file__).resolve().parents[1] / "src" / "harlequin"
25
26 PLUGIN_PATH = SOURCE_ROOT / "hsql" / "skill" / ".claude-plugin" / "plugin.json"
27
28 PROTOCOL_PATH = SOURCE_ROOT / "hsql" / "protocol.py"
29
30 _PROTOCOL_VERSION = re.compile(r'^VERSION = "[^"]*"(?=\r?$)', re.MULTILINE)
31 """The one line to rewrite, matched without consuming the newline before it.
32
33 A lookahead rather than `$`, because the file is read untranslated (see
34 `write_protocol_version`): with `\\r\\n` endings, `$` sits after the `\\r`, so a
35 pattern ending in `"` would never match one.
36 """
37
38
39 def write_plugin_version(version: str, path: Path = PLUGIN_PATH) -> None:
40 """Rewrite the manifest with `version`, and nothing else changed.
41
42 Key order survives the round trip, so a release's diff is the one line.
43 """
44 manifest = json.loads(path.read_text(encoding="utf-8"))
45 manifest["version"] = version
46 path.write_text(
47 json.dumps(manifest, indent=2) + "\n", encoding="utf-8", newline="\n"
48 )
49
50
51 def write_protocol_version(version: str, path: Path = PROTOCOL_PATH) -> None:
52 """Rewrite the session protocol's version, and nothing else.
53
54 Read and written with `newline=""`, so the file's own line endings survive:
55 this edits one line of a source file, and a checkout with CRLF endings --
56 which is every Windows one, since `.gitattributes` pins only the artifacts
57 whose bytes are a contract -- would otherwise come back with every other
58 line rewritten too. `open()` rather than `Path.read_text()`, which only
59 takes `newline` on 3.13 and up.
60 """
61 with path.open("r", encoding="utf-8", newline="") as source_file:
62 source = source_file.read()
63 rewritten, replacements = _PROTOCOL_VERSION.subn(f'VERSION = "{version}"', source)
64 if replacements != 1:
65 raise SystemExit(f"{path} declares VERSION {replacements} times, not once")
66 with path.open("w", encoding="utf-8", newline="") as target_file:
67 target_file.write(rewritten)
68
69
70 def main() -> None:
71 if len(sys.argv) != 2:
72 sys.exit("usage: python scripts/bump_versions.py VERSION")
73 write_plugin_version(sys.argv[1])
74 write_protocol_version(sys.argv[1])
75 print(f"wrote {PLUGIN_PATH} and {PROTOCOL_PATH}")
76
77
78 if __name__ == "__main__":
79 main()