|
| 1 | +#!/usr/bin/env python3 |
| 2 | +""" |
| 3 | +Helper script to bump version and create git tag consistently. |
| 4 | +
|
| 5 | +Usage: |
| 6 | + python scripts/bump_version.py patch # 0.1.1 -> 0.1.2 |
| 7 | + python scripts/bump_version.py minor # 0.1.1 -> 0.2.0 |
| 8 | + python scripts/bump_version.py major # 0.1.1 -> 1.0.0 |
| 9 | +""" |
| 10 | + |
| 11 | +import argparse |
| 12 | +import re |
| 13 | +import subprocess |
| 14 | +from pathlib import Path |
| 15 | + |
| 16 | + |
| 17 | +def get_current_version(pyproject_path: Path) -> str: |
| 18 | + """Extract version from pyproject.toml.""" |
| 19 | + content = pyproject_path.read_text() |
| 20 | + match = re.search(r'^version\s*=\s*["\']([^"\']+)["\']', content, re.MULTILINE) |
| 21 | + if not match: |
| 22 | + raise ValueError("Could not find version in pyproject.toml") |
| 23 | + return match.group(1) |
| 24 | + |
| 25 | + |
| 26 | +def bump_version(version: str, part: str) -> str: |
| 27 | + """Bump version according to semver.""" |
| 28 | + major, minor, patch = map(int, version.split(".")) |
| 29 | + |
| 30 | + if part == "major": |
| 31 | + return f"{major + 1}.0.0" |
| 32 | + elif part == "minor": |
| 33 | + return f"{major}.{minor + 1}.0" |
| 34 | + elif part == "patch": |
| 35 | + return f"{major}.{minor}.{patch + 1}" |
| 36 | + else: |
| 37 | + raise ValueError(f"Invalid version part: {part}") |
| 38 | + |
| 39 | + |
| 40 | +def update_pyproject(pyproject_path: Path, old_version: str, new_version: str): |
| 41 | + """Update version in pyproject.toml.""" |
| 42 | + content = pyproject_path.read_text() |
| 43 | + new_content = re.sub(rf'^version\s*=\s*["\']({re.escape(old_version)})["\']', f'version = "{new_version}"', content, flags=re.MULTILINE) |
| 44 | + pyproject_path.write_text(new_content) |
| 45 | + |
| 46 | + |
| 47 | +def main(): |
| 48 | + parser = argparse.ArgumentParser(description="Bump version and create git tag") |
| 49 | + parser.add_argument("part", choices=["major", "minor", "patch"], help="Version part to bump") |
| 50 | + parser.add_argument("--dry-run", action="store_true", help="Show what would be done") |
| 51 | + args = parser.parse_args() |
| 52 | + |
| 53 | + # Get paths |
| 54 | + repo_root = Path(__file__).parent.parent |
| 55 | + pyproject_path = repo_root / "pyproject.toml" |
| 56 | + |
| 57 | + # Get current version and calculate new version |
| 58 | + current_version = get_current_version(pyproject_path) |
| 59 | + new_version = bump_version(current_version, args.part) |
| 60 | + |
| 61 | + print(f"Current version: {current_version}") |
| 62 | + print(f"New version: {new_version}") |
| 63 | + |
| 64 | + if args.dry_run: |
| 65 | + print("\nDry run - no changes made") |
| 66 | + return |
| 67 | + |
| 68 | + # Update pyproject.toml |
| 69 | + print(f"\nUpdating {pyproject_path}...") |
| 70 | + update_pyproject(pyproject_path, current_version, new_version) |
| 71 | + |
| 72 | + # Commit and tag |
| 73 | + print("\nCreating git commit and tag...") |
| 74 | + subprocess.run(["git", "add", str(pyproject_path)], check=True) |
| 75 | + subprocess.run(["git", "commit", "-m", f"Bump version to {new_version}"], check=True) |
| 76 | + subprocess.run(["git", "tag", "-a", f"v{new_version}", "-m", f"Release v{new_version}"], check=True) |
| 77 | + |
| 78 | + print(f"\nDone! Version bumped to {new_version}") |
| 79 | + print("\nNext steps:") |
| 80 | + print(" 1. Review the changes: git show") |
| 81 | + print(" 2. Push to GitHub: git push && git push --tags") |
| 82 | + print(" 3. GitHub Actions will handle PyPI release automatically") |
| 83 | + |
| 84 | + |
| 85 | +if __name__ == "__main__": |
| 86 | + main() |
0 commit comments