|
| 1 | +#!/usr/bin/env python3 |
| 2 | +"""Prepare a release bump across the CPA workspace.""" |
| 3 | + |
| 4 | +from __future__ import annotations |
| 5 | + |
| 6 | +import argparse |
| 7 | +import re |
| 8 | +from datetime import UTC, datetime |
| 9 | +from pathlib import Path |
| 10 | + |
| 11 | +ROOT = Path(__file__).resolve().parents[1] |
| 12 | + |
| 13 | +VERSION_RE = re.compile(r"^[0-9]+\.[0-9]+\.[0-9]+(?:[a-zA-Z0-9.-]+)?$") |
| 14 | +PROJECT_VERSION_RE = re.compile(r'(?m)^version = "[^"]+"$') |
| 15 | +CORE_DEP_RE = re.compile(r'"create-python-app-core>=[^"]+"') |
| 16 | +PY_VERSION_RE = re.compile(r'__version__ = "[^"]+"') |
| 17 | + |
| 18 | +VERSION_FILES = [ |
| 19 | + ROOT / "packages/create-python-app-core/pyproject.toml", |
| 20 | + ROOT / "packages/create-awesome-python-app/pyproject.toml", |
| 21 | + ROOT / "packages/create-python-app-core/src/create_python_app_core/_version.py", |
| 22 | + ROOT |
| 23 | + / "packages/create-awesome-python-app/src" |
| 24 | + / "create_awesome_python_app/__init__.py", |
| 25 | +] |
| 26 | + |
| 27 | + |
| 28 | +def replace_once(path: Path, pattern: re.Pattern[str], replacement: str) -> None: |
| 29 | + text = path.read_text() |
| 30 | + updated, count = pattern.subn(replacement, text, count=1) |
| 31 | + if count != 1: |
| 32 | + raise SystemExit(f"Expected one replacement in {path}, got {count}") |
| 33 | + path.write_text(updated) |
| 34 | + |
| 35 | + |
| 36 | +def update_versions(version: str) -> None: |
| 37 | + replace_once( |
| 38 | + ROOT / "packages/create-python-app-core/pyproject.toml", |
| 39 | + PROJECT_VERSION_RE, |
| 40 | + f'version = "{version}"', |
| 41 | + ) |
| 42 | + replace_once( |
| 43 | + ROOT / "packages/create-awesome-python-app/pyproject.toml", |
| 44 | + PROJECT_VERSION_RE, |
| 45 | + f'version = "{version}"', |
| 46 | + ) |
| 47 | + replace_once( |
| 48 | + ROOT / "packages/create-awesome-python-app/pyproject.toml", |
| 49 | + CORE_DEP_RE, |
| 50 | + f'"create-python-app-core>={version}"', |
| 51 | + ) |
| 52 | + replace_once( |
| 53 | + ROOT / "packages/create-python-app-core/src/create_python_app_core/_version.py", |
| 54 | + PY_VERSION_RE, |
| 55 | + f'__version__ = "{version}"', |
| 56 | + ) |
| 57 | + replace_once( |
| 58 | + ROOT |
| 59 | + / "packages/create-awesome-python-app/src" |
| 60 | + / "create_awesome_python_app/__init__.py", |
| 61 | + PY_VERSION_RE, |
| 62 | + f'__version__ = "{version}"', |
| 63 | + ) |
| 64 | + |
| 65 | + |
| 66 | +def update_changelog(version: str, notes: str) -> None: |
| 67 | + changelog = ROOT / "CHANGELOG.md" |
| 68 | + text = changelog.read_text() |
| 69 | + heading = f"## {version}" |
| 70 | + if heading in text: |
| 71 | + raise SystemExit(f"CHANGELOG.md already contains {heading}") |
| 72 | + |
| 73 | + today = datetime.now(UTC).date().isoformat() |
| 74 | + body = notes.strip() or "- Maintenance release." |
| 75 | + section = f"## {version} - {today}\n\n{body}\n\n" |
| 76 | + if not text.startswith("# Changelog\n\n"): |
| 77 | + raise SystemExit("CHANGELOG.md must start with '# Changelog'") |
| 78 | + updated = text.replace("# Changelog\n\n", f"# Changelog\n\n{section}", 1) |
| 79 | + changelog.write_text(updated) |
| 80 | + |
| 81 | + |
| 82 | +def main() -> None: |
| 83 | + parser = argparse.ArgumentParser() |
| 84 | + parser.add_argument("version", help="Release version, e.g. 0.1.1") |
| 85 | + parser.add_argument( |
| 86 | + "--notes", |
| 87 | + default="- Maintenance release.", |
| 88 | + help="Markdown notes to insert into CHANGELOG.md", |
| 89 | + ) |
| 90 | + args = parser.parse_args() |
| 91 | + |
| 92 | + if not VERSION_RE.match(args.version): |
| 93 | + raise SystemExit(f"Invalid version: {args.version}") |
| 94 | + |
| 95 | + for path in VERSION_FILES: |
| 96 | + if not path.is_file(): |
| 97 | + raise SystemExit(f"Missing expected version file: {path}") |
| 98 | + |
| 99 | + update_versions(args.version) |
| 100 | + update_changelog(args.version, args.notes) |
| 101 | + print(f"Prepared release {args.version}") |
| 102 | + |
| 103 | + |
| 104 | +if __name__ == "__main__": |
| 105 | + main() |
0 commit comments