diff --git a/.ndf/release.json b/.ndf/release.json index 34ab2e197..f8a0f99ca 100644 --- a/.ndf/release.json +++ b/.ndf/release.json @@ -6,7 +6,7 @@ "name": "版数の記載をそろえる", "stage": "production", "command": ["python3", "scripts/sync-version-mentions.py", "--root", "."], - "writes": ["README.md", "docs/versioning-and-distribution.md", "plugins/ndf/README.md", ".claude-plugin/marketplace.json"], + "writes": ["README.md", "docs/versioning-and-distribution.md", "plugins/ndf/README.md", ".claude-plugin/marketplace.json", "plugins/playwright-kit/plugin.json"], "timeout_seconds": 300 }, { diff --git a/scripts/sync-version-mentions.py b/scripts/sync-version-mentions.py index 6a6cfe3ce..6e6672677 100644 --- a/scripts/sync-version-mentions.py +++ b/scripts/sync-version-mentions.py @@ -4,7 +4,8 @@ リリースの設定(`.ndf/release.json`)の手順として、版上げの PR の組み立て(`ndf release-steps assemble`)が `plugin.json` の `version` を上げた後に打つ。 -- 各プラグインの `.claude-plugin/plugin.json`・`.codex-plugin/plugin.json` と、`.claude-plugin/marketplace.json` の +- 各プラグインの `.claude-plugin/plugin.json`・`.codex-plugin/plugin.json`・ルートの `plugin.json`(`version` も。 + 書き換わるものがリリースの設定のこの手順の `writes` の外なら、何も書く前に止める)と、`.claude-plugin/marketplace.json` の そのプラグインの description の `(vX.Y.Z)` を、Claude 版の `plugin.json` の `version` にそろえる (`scripts/lib/validate_manifests.py` が確かめる規則) - `scripts/check-doc-staleness.py` が「記載: <古い版>(L<行>) / <正本>: <新しい版>」と報告した行の版数だけを置き換える。 @@ -17,6 +18,7 @@ import argparse import json +import posixpath import re import subprocess import sys @@ -28,11 +30,16 @@ r"(?P\d+\.\d+\.\d+(?:-[0-9A-Za-z.-]+)?))" ) MAX_ROUNDS = 3 +RELEASE_CONFIG = ".ndf/release.json" +SCRIPT_NAME = "sync-version-mentions.py" def sync_descriptions(root: Path) -> list[str]: - """description の版数を Claude 版の plugin.json の version にそろえ、書き換えたファイルを返す。""" - changed = set() + """description の版数を Claude 版の plugin.json の version にそろえ、書き換えたファイルを返す。 + + 書き換え後の本文をすべて作ってから、書き換わるルートの plugin.json が `writes` に入るかを確かめ、入らなければ何も書かずに止める + (書いた後に組み立てが writes の外として止まる前に止める)。""" + planned: dict[Path, str] = {} versions = {} for claude in sorted(root.glob("plugins/**/.claude-plugin/plugin.json")): manifest = json.loads(claude.read_text(encoding="utf-8")) @@ -42,32 +49,83 @@ def sync_descriptions(root: Path) -> list[str]: if isinstance(manifest.get("name"), str): versions[manifest["name"]] = version for path in (claude, claude.parent.parent / ".codex-plugin" / "plugin.json"): - if path.is_file() and replace_description_version(path, version, json.loads(path.read_text(encoding="utf-8"))): - changed.add(path.relative_to(root).as_posix()) + if path.is_file(): + plan_manifest(planned, path, version, follow_version=False) + package = package_of(claude) # Agent Plugins package(ルートの plugin.json)は version も追う + if package.is_file(): + plan_manifest(planned, package, version, follow_version=True) market = root / ".claude-plugin" / "marketplace.json" if market.is_file(): - for entry in json.loads(market.read_text(encoding="utf-8")).get("plugins", []): + text = market.read_text(encoding="utf-8") + fixed = text + for entry in json.loads(text).get("plugins", []): version = versions.get(entry.get("name")) if isinstance(entry, dict) else None - if version is not None and replace_description_version(market, version, entry): - changed.add(market.relative_to(root).as_posix()) + if version is not None: + fixed = replace_description_version(market, fixed, version, entry) + if fixed != text: + planned[market] = fixed + changed = {path.relative_to(root).as_posix(): text for path, text in planned.items()} + packages = {package_of(c).relative_to(root).as_posix() for c in root.glob("plugins/**/.claude-plugin/plugin.json")} + writes = declared_writes(root) + undeclared = sorted(rel for rel in changed if rel in packages and not covered(rel, writes)) + if undeclared: + raise SystemExit(f"ルートの plugin.json が {RELEASE_CONFIG} のこの手順の writes に無い: {', '.join(undeclared)}") + for path, text in planned.items(): + path.write_text(text, encoding="utf-8") return sorted(changed) -def replace_description_version(path: Path, version: str, owner: dict) -> bool: - """`owner`(`path` の中のオブジェクト)の `description` の `(vX.Y.Z)` だけを置き換える(ファイルの整形はそのまま保つ)。""" +def package_of(claude: Path) -> Path: + return claude.parent.parent / "plugin.json" + + +def declared_writes(root: Path) -> list[str]: + """リリースの設定のうちこのスクリプトを打つ手順の `writes` を、組み立て(`assemble`)と同じ表記(`posixpath.normpath`)で返す。 + + 組み立てが書き換えてよいパスとして照らすのと同じ宣言を使う(同期の対象をここで別に持たない)。""" + path = root / RELEASE_CONFIG + if not path.is_file(): + return [] + steps = json.loads(path.read_text(encoding="utf-8")).get("steps", []) + mine = [s for s in steps if isinstance(s, dict) and any(str(a).endswith(SCRIPT_NAME) for a in s.get("command", []))] + return [posixpath.normpath(str(w)) for s in mine for w in s.get("writes", [])] + + +def covered(rel: str, writes: list[str]) -> bool: + """組み立ての `_outside` と同じく、宣言のパスそのものか、その下のパスなら真(ディレクトリの宣言も受ける)。""" + return any(rel == w or rel.startswith(f"{w.rstrip('/')}/") for w in writes) + + +def plan_manifest(planned: dict[Path, str], path: Path, version: str, *, follow_version: bool) -> None: + """1 回読んで、description の版数(`follow_version` なら `version` も)をそろえた本文を、変わるときだけ `planned` へ入れる + (整形はそのまま保つ。書くのは検査の後)。""" text = path.read_text(encoding="utf-8") + manifest = json.loads(text) + fixed = text + old = manifest.get("version") + if follow_version and isinstance(old, str) and old != version: + literal = f'"version": {json.dumps(old)}' + if literal not in fixed: + raise SystemExit(f"{path}: version の値を本文から見つけられない(整形の形が違う)") + fixed = fixed.replace(literal, f'"version": {json.dumps(version)}', 1) + fixed = replace_description_version(path, fixed, version, manifest) + if fixed != text: + planned[path] = fixed + + +def replace_description_version(path: Path, text: str, version: str, owner: dict) -> str: + """`owner`(`text` の中のオブジェクト)の `description` の `(vX.Y.Z)` だけを置き換えた本文を返す(整形はそのまま保つ)。""" description = owner.get("description") if not isinstance(description, str): - return False + return text found = DESCRIPTION_VERSION.search(description) if found is None or found.group(1) == version: - return False + return text fixed = description.replace(found.group(0), f"(v{version})", 1) old_literal = json.dumps(description, ensure_ascii=False) if old_literal not in text: raise SystemExit(f"{path}: description の値を本文から見つけられない(エスケープの形が違う)") - path.write_text(text.replace(old_literal, json.dumps(fixed, ensure_ascii=False), 1), encoding="utf-8") - return True + return text.replace(old_literal, json.dumps(fixed, ensure_ascii=False), 1) def stale_lines(output: str) -> list[tuple[str, int, str, str]]: diff --git a/scripts/tests/test_sync_version_mentions.py b/scripts/tests/test_sync_version_mentions.py index efe98a3b0..c849aa1df 100644 --- a/scripts/tests/test_sync_version_mentions.py +++ b/scripts/tests/test_sync_version_mentions.py @@ -65,6 +65,61 @@ def test_bumped_tree_becomes_consistent(tmp_path: Path) -> None: assert "そろえた: README.md" in got.stdout +def test_root_plugin_manifest_follows_the_claude_version(tmp_path: Path) -> None: + # 2026-11-22 の 11.0.14 の組み立て: 依存を更新した playwright-kit の Claude 版だけが 4.0.5 へ上がり、 + # ルートの plugin.json(Agent Plugins package)が 4.0.4 のまま残って validate_manifests が落ちた + tree = _export(tmp_path) + new = _bump(tree, "playwright-kit") + got = _run(tree) + assert got.returncode == 0, got.stderr + root = json.loads((tree / "plugins/playwright-kit/plugin.json").read_text(encoding="utf-8")) + assert root["version"] == new + assert f"(v{new})" in root["description"] + assert "そろえた: plugins/playwright-kit/plugin.json" in got.stdout + + +def _mini_tree(tmp_path: Path, writes: list[str], *, root_version: str = "1.0.0") -> Path: + """設定と 1 つのプラグインのマニフェストだけの木(Claude 版は 1.0.1 へ上がった後、ルートの plugin.json は `root_version`)。""" + tree = tmp_path / "repo" + command = ["python3", "scripts/sync-version-mentions.py", "--root", "."] + files = { + ".ndf/release.json": {"steps": [{"name": "s", "stage": "production", "command": command, "writes": writes}]}, + "plugins/kit/.claude-plugin/plugin.json": {"name": "kit", "version": "1.0.1", "description": "説明 (v1.0.0)"}, + "plugins/kit/plugin.json": {"name": "kit", "version": root_version, "description": f"説明 (v{root_version})"}, + } + for rel, body in files.items(): + (tree / rel).parent.mkdir(parents=True, exist_ok=True) + (tree / rel).write_text(json.dumps(body, ensure_ascii=False, indent=2) + "\n", encoding="utf-8") + return tree + + +def _texts(tree: Path) -> dict[str, str]: + return {p.relative_to(tree).as_posix(): p.read_text(encoding="utf-8") for p in sorted(tree.rglob("*.json"))} + + +def test_undeclared_root_plugin_manifest_stops_before_writing(tmp_path: Path) -> None: + # 同期の対象はリリースの設定の writes から読む。宣言に無いルートの plugin.json を書けば組み立てが writes の外として止まるので、 + # どのファイルも書く前に止める + tree = _mini_tree(tmp_path, ["README.md"]) + before = _texts(tree) + with pytest.raises(SystemExit, match="plugins/kit/plugin.json"): + _load().sync_descriptions(tree) + assert _texts(tree) == before + + +@pytest.mark.parametrize("writes", [["./plugins/kit/plugin.json"], ["plugins/kit/"], ["plugins"]]) +def test_root_plugin_manifest_declared_as_path_or_directory_is_synced(tmp_path: Path, writes: list[str]) -> None: + # 組み立てと同じく、表記を畳んだパスそのものか、宣言したディレクトリの下なら書いてよい + tree = _mini_tree(tmp_path, writes) + assert _load().sync_descriptions(tree) == ["plugins/kit/.claude-plugin/plugin.json", "plugins/kit/plugin.json"] + assert json.loads((tree / "plugins/kit/plugin.json").read_text(encoding="utf-8"))["version"] == "1.0.1" + + +def test_root_plugin_manifest_already_in_sync_needs_no_declaration(tmp_path: Path) -> None: + tree = _mini_tree(tmp_path, [], root_version="1.0.1") + assert _load().sync_descriptions(tree) == ["plugins/kit/.claude-plugin/plugin.json"] + + def test_consistent_tree_is_left_alone(tmp_path: Path) -> None: tree = _export(tmp_path) got = _run(tree)