|
| 1 | +#!/usr/bin/env python3 |
| 2 | +"""Synchronize release manifest versions.""" |
| 3 | + |
| 4 | +from __future__ import annotations |
| 5 | + |
| 6 | +import argparse |
| 7 | +import json |
| 8 | +import re |
| 9 | +import sys |
| 10 | +from dataclasses import dataclass |
| 11 | +from pathlib import Path |
| 12 | +from typing import Any |
| 13 | + |
| 14 | + |
| 15 | +ROOT = Path(__file__).resolve().parents[2] |
| 16 | +MAP_PATH = ROOT / ".version-bump.json" |
| 17 | +SEMVER_RE = re.compile(r"^(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)$") |
| 18 | + |
| 19 | + |
| 20 | +@dataclass(frozen=True) |
| 21 | +class ManifestTarget: |
| 22 | + path: Path |
| 23 | + field: str |
| 24 | + version: str |
| 25 | + data: Any |
| 26 | + |
| 27 | + |
| 28 | +def parse_version(version: str) -> tuple[int, int, int]: |
| 29 | + match = SEMVER_RE.match(version) |
| 30 | + if not match: |
| 31 | + raise ValueError(f"invalid semver: {version}") |
| 32 | + return tuple(int(part) for part in match.groups()) |
| 33 | + |
| 34 | + |
| 35 | +def bump_semver(version: str, level: str) -> str: |
| 36 | + major, minor, patch = parse_version(version) |
| 37 | + if level == "major": |
| 38 | + return f"{major + 1}.0.0" |
| 39 | + if level == "minor": |
| 40 | + return f"{major}.{minor + 1}.0" |
| 41 | + if level == "patch": |
| 42 | + return f"{major}.{minor}.{patch + 1}" |
| 43 | + raise ValueError(f"invalid bump level: {level}") |
| 44 | + |
| 45 | + |
| 46 | +def resolve_field(data: Any, field: str) -> Any: |
| 47 | + current = data |
| 48 | + for part in field.split("."): |
| 49 | + if isinstance(current, list): |
| 50 | + if not part.isdigit(): |
| 51 | + raise KeyError(f"expected list index at {part!r} in {field}") |
| 52 | + index = int(part) |
| 53 | + current = current[index] |
| 54 | + continue |
| 55 | + if not isinstance(current, dict): |
| 56 | + raise KeyError(f"cannot resolve {part!r} in {field}") |
| 57 | + current = current[part] |
| 58 | + return current |
| 59 | + |
| 60 | + |
| 61 | +def set_field(data: Any, field: str, value: str) -> None: |
| 62 | + current = data |
| 63 | + parts = field.split(".") |
| 64 | + for part in parts[:-1]: |
| 65 | + if isinstance(current, list): |
| 66 | + if not part.isdigit(): |
| 67 | + raise KeyError(f"expected list index at {part!r} in {field}") |
| 68 | + current = current[int(part)] |
| 69 | + continue |
| 70 | + if not isinstance(current, dict): |
| 71 | + raise KeyError(f"cannot resolve {part!r} in {field}") |
| 72 | + current = current[part] |
| 73 | + |
| 74 | + last = parts[-1] |
| 75 | + if isinstance(current, list): |
| 76 | + if not last.isdigit(): |
| 77 | + raise KeyError(f"expected list index at {last!r} in {field}") |
| 78 | + current[int(last)] = value |
| 79 | + elif isinstance(current, dict): |
| 80 | + current[last] = value |
| 81 | + else: |
| 82 | + raise KeyError(f"cannot set {last!r} in {field}") |
| 83 | + |
| 84 | + |
| 85 | +def read_json(path: Path) -> Any: |
| 86 | + with path.open("r", encoding="utf-8") as handle: |
| 87 | + return json.load(handle) |
| 88 | + |
| 89 | + |
| 90 | +def write_json(path: Path, data: Any) -> None: |
| 91 | + with path.open("w", encoding="utf-8") as handle: |
| 92 | + json.dump(data, handle, ensure_ascii=False, indent=2) |
| 93 | + handle.write("\n") |
| 94 | + |
| 95 | + |
| 96 | +def load_targets(root: Path = ROOT) -> list[ManifestTarget]: |
| 97 | + mapping = read_json(root / ".version-bump.json") |
| 98 | + targets: list[ManifestTarget] = [] |
| 99 | + for item in mapping["files"]: |
| 100 | + path = root / item["path"] |
| 101 | + field = item["field"] |
| 102 | + data = read_json(path) |
| 103 | + version = resolve_field(data, field) |
| 104 | + if not isinstance(version, str): |
| 105 | + raise ValueError(f"{item['path']}:{field} is not a string") |
| 106 | + parse_version(version) |
| 107 | + targets.append(ManifestTarget(path=path, field=field, version=version, data=data)) |
| 108 | + return targets |
| 109 | + |
| 110 | + |
| 111 | +# Refactor (iter3/skill-release-pipeline): Old: 无机械 release 管道 New: bump_version + release.yml minimal option A(#32 minimal 共识) |
| 112 | +def assert_versions_sync(targets: list[ManifestTarget]) -> str: |
| 113 | + versions = {target.version for target in targets} |
| 114 | + if len(versions) != 1: |
| 115 | + lines = ["manifest versions are not synchronized:"] |
| 116 | + lines.extend(f"- {target.path.relative_to(ROOT)}:{target.field} = {target.version}" for target in targets) |
| 117 | + raise ValueError("\n".join(lines)) |
| 118 | + return targets[0].version |
| 119 | + |
| 120 | + |
| 121 | +# Refactor (iter3/skill-release-pipeline): Old: 无机械 release 管道 New: bump_version + release.yml minimal option A(#32 minimal 共识) |
| 122 | +def write_version(targets: list[ManifestTarget], version: str, dry_run: bool) -> None: |
| 123 | + parse_version(version) |
| 124 | + for target in targets: |
| 125 | + set_field(target.data, target.field, version) |
| 126 | + if dry_run: |
| 127 | + return |
| 128 | + for target in targets: |
| 129 | + write_json(target.path, target.data) |
| 130 | + |
| 131 | + |
| 132 | +# Refactor (iter3/skill-release-pipeline): Old: 无机械 release 管道 New: bump_version + release.yml minimal option A(#32 minimal 共识) |
| 133 | +def main(argv: list[str] | None = None) -> int: |
| 134 | + parser = argparse.ArgumentParser(description=__doc__) |
| 135 | + parser.add_argument("--check", action="store_true", help="validate mapped manifest versions are synchronized") |
| 136 | + parser.add_argument("--read-version", action="store_true", help="print the synchronized manifest version") |
| 137 | + parser.add_argument("--dry-run", action="store_true", help="compute changes without writing files") |
| 138 | + parser.add_argument("--level", choices=("patch", "minor", "major"), help="semver bump level") |
| 139 | + parser.add_argument("--version", help="exact semver version to write") |
| 140 | + args = parser.parse_args(argv) |
| 141 | + |
| 142 | + try: |
| 143 | + if args.level and args.version: |
| 144 | + raise ValueError("--level and --version are mutually exclusive") |
| 145 | + targets = load_targets() |
| 146 | + current = assert_versions_sync(targets) |
| 147 | + next_version = args.version or (bump_semver(current, args.level) if args.level else None) |
| 148 | + if next_version is not None: |
| 149 | + write_version(targets, next_version, args.dry_run) |
| 150 | + current = next_version |
| 151 | + if args.read_version: |
| 152 | + print(current) |
| 153 | + except Exception as exc: |
| 154 | + print(f"bump_version.py: {exc}", file=sys.stderr) |
| 155 | + return 1 |
| 156 | + return 0 |
| 157 | + |
| 158 | + |
| 159 | +if __name__ == "__main__": |
| 160 | + raise SystemExit(main()) |
0 commit comments