|
| 1 | +#!/usr/bin/env python |
| 2 | +from __future__ import annotations |
| 3 | + |
| 4 | +import re |
| 5 | +from pathlib import Path |
| 6 | + |
| 7 | +from graphdatascience.semantic_version.semantic_version import SemanticVersion |
| 8 | + |
| 9 | +REPO_ROOT = Path(__file__).resolve().parent.parent.parent |
| 10 | +VERSION_FILE = REPO_ROOT / "src" / "graphdatascience" / "version.py" |
| 11 | + |
| 12 | +VERSION_REGEX = r'__version__ = "([^"]*)"' |
| 13 | + |
| 14 | + |
| 15 | +class PythonLibraryVersion: |
| 16 | + def __init__(self, major: int, minor: int, patch: int, suffix: str = ""): |
| 17 | + self.major = major |
| 18 | + self.minor = minor |
| 19 | + self.patch = patch |
| 20 | + self.suffix = suffix |
| 21 | + |
| 22 | + @classmethod |
| 23 | + def from_string(cls, version: str) -> PythonLibraryVersion: |
| 24 | + parts = version.split("a") |
| 25 | + if len(parts) == 2: |
| 26 | + version, alpha_version = parts |
| 27 | + suffix = f"a{alpha_version}" |
| 28 | + else: |
| 29 | + version = parts[0] |
| 30 | + suffix = "" |
| 31 | + |
| 32 | + semantic_version = SemanticVersion.from_string(version) |
| 33 | + return cls(semantic_version.major, semantic_version.minor, semantic_version.patch, suffix) |
| 34 | + |
| 35 | + def is_alpha(self) -> bool: |
| 36 | + return self.suffix.startswith("a") |
| 37 | + |
| 38 | + def alpha(self) -> int | None: |
| 39 | + if self.is_alpha(): |
| 40 | + return int(self.suffix[1:]) |
| 41 | + return None |
| 42 | + |
| 43 | + def major_minor(self) -> str: |
| 44 | + return f"{self.major}.{self.minor}" |
| 45 | + |
| 46 | + def copy(self) -> PythonLibraryVersion: |
| 47 | + return PythonLibraryVersion( |
| 48 | + self.major, |
| 49 | + self.minor, |
| 50 | + self.patch, |
| 51 | + self.suffix, |
| 52 | + ) |
| 53 | + |
| 54 | + def __str__(self) -> str: |
| 55 | + if self.patch > 0: |
| 56 | + return f"{self.major}.{self.minor}.{self.patch}{self.suffix}" |
| 57 | + else: |
| 58 | + return f"{self.major}.{self.minor}{self.suffix}" |
| 59 | + |
| 60 | + |
| 61 | +def read_library_version() -> PythonLibraryVersion: |
| 62 | + version_file = VERSION_FILE.read_text() |
| 63 | + version_regex = r'^__version__\s*=\s*"([^"]*)"' |
| 64 | + |
| 65 | + match = re.search(version_regex, version_file) |
| 66 | + if not match: |
| 67 | + raise ValueError("Could not find version string in version.py") |
| 68 | + |
| 69 | + return PythonLibraryVersion.from_string(match.group(1)) |
| 70 | + |
| 71 | + |
| 72 | +def bump_version(current_version: PythonLibraryVersion) -> PythonLibraryVersion: |
| 73 | + new_version = current_version.copy() |
| 74 | + if alpha_version := current_version.alpha(): |
| 75 | + # Alpha release - bump alpha path |
| 76 | + next_alpha = alpha_version + 1 |
| 77 | + new_version.suffix = f"a{next_alpha}" |
| 78 | + else: |
| 79 | + new_version.minor += 1 |
| 80 | + return new_version |
| 81 | + |
| 82 | + |
| 83 | +def update_version_py(new_version: PythonLibraryVersion) -> None: |
| 84 | + content = VERSION_FILE.read_text() |
| 85 | + |
| 86 | + updated = re.sub(VERSION_REGEX, f'__version__ = "{new_version.major_minor()}"', content) |
| 87 | + |
| 88 | + VERSION_FILE.write_text(updated) |
| 89 | + print(f"✅ Updated {VERSION_FILE} to version {new_version}") |
| 90 | + |
| 91 | + |
| 92 | +def update_changelog(new_version: PythonLibraryVersion) -> None: |
| 93 | + changelog_file = REPO_ROOT / "changelog.md" |
| 94 | + |
| 95 | + template = Path(__file__).parent / "changelog.template" |
| 96 | + new_changelog_body = template.read_text().replace("<VERSION>", str(new_version)) |
| 97 | + |
| 98 | + changelog_file.write_text(new_changelog_body) |
| 99 | + |
| 100 | + print(f"✅ Updated {changelog_file} for version {new_version}") |
| 101 | + |
| 102 | + |
| 103 | +def update_publish_yml(repo_root: Path, released_version: PythonLibraryVersion) -> None: |
| 104 | + """Add new release branch to publish.yml.""" |
| 105 | + publish_file = repo_root / "doc" / "publish.yml" |
| 106 | + content = publish_file.read_text() |
| 107 | + |
| 108 | + # Extract major.minor from released version |
| 109 | + new_branch = f"{released_version.major_minor()}" |
| 110 | + |
| 111 | + # Update branches list |
| 112 | + updated = re.sub(r"(branches:\s*\[)([^\]]*)", lambda m: f"{m.group(1)}{m.group(2)}, '{new_branch}'", content) |
| 113 | + |
| 114 | + publish_file.write_text(updated) |
| 115 | + print(f"✓ Updated {publish_file.relative_to(repo_root)} - added branch '{new_branch}'") |
| 116 | + |
| 117 | + |
| 118 | +def update_antora_yml(repo_root: Path, new_version: str) -> None: |
| 119 | + """Update version in antora.yml.""" |
| 120 | + antora_file = repo_root / "doc" / "antora.yml" |
| 121 | + content = antora_file.read_text() |
| 122 | + |
| 123 | + updated = re.sub(r"version: '[^']*'", f"version: '{new_version}'", content) |
| 124 | + updated = re.sub(r"docs-version: '[^']*'", f"docs-version: '{new_version}'", updated) |
| 125 | + |
| 126 | + antora_file.write_text(updated) |
| 127 | + print(f"✓ Updated {antora_file.relative_to(repo_root)} to version {new_version}") |
| 128 | + |
| 129 | + |
| 130 | +def update_package_json(repo_root: Path, new_version: str) -> None: |
| 131 | + """Update version in package.json.""" |
| 132 | + package_file = repo_root / "doc" / "package.json" |
| 133 | + content = package_file.read_text() |
| 134 | + |
| 135 | + # Set to preview version |
| 136 | + preview_version = f"{new_version}-preview" |
| 137 | + updated = re.sub(r'"version":\s*"[^"]*"', f'"version": "{preview_version}"', content) |
| 138 | + |
| 139 | + package_file.write_text(updated) |
| 140 | + print(f"✓ Updated {package_file.relative_to(repo_root)} to version {preview_version}") |
| 141 | + |
| 142 | + |
| 143 | +def main() -> None: |
| 144 | + # Get current version |
| 145 | + current_version = read_library_version() |
| 146 | + |
| 147 | + print(f"Current version: {current_version}") |
| 148 | + |
| 149 | + # Calculate next version |
| 150 | + next_version = bump_version(current_version) |
| 151 | + print(f"Next version: {next_version}") |
| 152 | + |
| 153 | + print("\nStarting post-release tasks...") |
| 154 | + |
| 155 | + update_version_py(next_version) |
| 156 | + |
| 157 | + if not current_version.is_alpha(): |
| 158 | + update_changelog(next_version) |
| 159 | + # update_publish_yml(current_version) |
| 160 | + # update_antora_yml(next_version) |
| 161 | + # TODO update preview.yml |
| 162 | + # update_package_json(next_version) |
| 163 | + |
| 164 | + # update_installation_adoc(current_version, next_version) |
| 165 | + |
| 166 | + print("\n✅ Post-release tasks completed!") |
| 167 | + print("\nNext steps:") |
| 168 | + print("* Review the changes") |
| 169 | + if not current_version.is_alpha(): |
| 170 | + print("* Update installation.adoc") |
| 171 | + print(f"* Commit with message: 'Prepare for {next_version} development'") |
| 172 | + print("* Push to main branch") |
| 173 | + |
| 174 | + |
| 175 | +if __name__ == "__main__": |
| 176 | + main() |
0 commit comments