|
| 1 | +#!/usr/bin/env python3 |
| 2 | +"""Bump the plugin version in every place it lives. |
| 3 | +
|
| 4 | +Usage: python3 scripts/bump_version.py 0.4.0 |
| 5 | +
|
| 6 | +Updates .claude-plugin/plugin.json, .claude-plugin/marketplace.json, and |
| 7 | +stamps the CHANGELOG.md `## [Unreleased]` heading with the version and |
| 8 | +today's date. Refuses to run if there is no [Unreleased] section or the |
| 9 | +version is not newer than the current one. |
| 10 | +""" |
| 11 | + |
| 12 | +import datetime |
| 13 | +import json |
| 14 | +import os |
| 15 | +import re |
| 16 | +import sys |
| 17 | + |
| 18 | +ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) |
| 19 | + |
| 20 | + |
| 21 | +def fail(msg: str) -> None: |
| 22 | + print(f"error: {msg}", file=sys.stderr) |
| 23 | + sys.exit(1) |
| 24 | + |
| 25 | + |
| 26 | +def main() -> None: |
| 27 | + if len(sys.argv) != 2 or not re.fullmatch(r"\d+\.\d+\.\d+", sys.argv[1]): |
| 28 | + fail(f"usage: {sys.argv[0]} <major.minor.patch>") |
| 29 | + new = sys.argv[1] |
| 30 | + |
| 31 | + plugin_path = os.path.join(ROOT, ".claude-plugin", "plugin.json") |
| 32 | + market_path = os.path.join(ROOT, ".claude-plugin", "marketplace.json") |
| 33 | + changelog_path = os.path.join(ROOT, "CHANGELOG.md") |
| 34 | + |
| 35 | + plugin = json.load(open(plugin_path)) |
| 36 | + current = plugin["version"] |
| 37 | + if tuple(map(int, new.split("."))) <= tuple(map(int, current.split("."))): |
| 38 | + fail(f"new version {new} is not newer than current {current}") |
| 39 | + |
| 40 | + changelog = open(changelog_path).read() |
| 41 | + if "## [Unreleased]" not in changelog: |
| 42 | + fail("CHANGELOG.md has no ## [Unreleased] section to release") |
| 43 | + |
| 44 | + # plugin.json — rewrite via json to keep it canonical |
| 45 | + plugin["version"] = new |
| 46 | + with open(plugin_path, "w") as f: |
| 47 | + json.dump(plugin, f, indent=2, ensure_ascii=False) |
| 48 | + f.write("\n") |
| 49 | + |
| 50 | + market = json.load(open(market_path)) |
| 51 | + market["metadata"]["version"] = new |
| 52 | + with open(market_path, "w") as f: |
| 53 | + json.dump(market, f, indent=2, ensure_ascii=False) |
| 54 | + f.write("\n") |
| 55 | + |
| 56 | + today = datetime.date.today().isoformat() |
| 57 | + changelog = changelog.replace("## [Unreleased]", f"## [{new}] — {today}", 1) |
| 58 | + open(changelog_path, "w").write(changelog) |
| 59 | + |
| 60 | + print(f"bumped {current} -> {new} in plugin.json, marketplace.json, CHANGELOG.md") |
| 61 | + print("review the diff, then commit with e.g.:") |
| 62 | + print(f' git commit -am "release: {new}"') |
| 63 | + |
| 64 | + |
| 65 | +if __name__ == "__main__": |
| 66 | + main() |
0 commit comments