|
6 | 6 | Release automation script for the TypeAgent Python package. |
7 | 7 |
|
8 | 8 | This script: |
9 | | -1. Bumps the patch version (3rd part) in pyproject.toml |
| 9 | +1. Bumps the patch version (3rd part) in pyproject.toml, or sets it to a specified version |
10 | 10 | 2. Commits the change |
11 | 11 | 3. Creates a git tag in the format v{major}.{minor}.{patch}-py |
12 | 12 | 4. Pushes the tags to trigger the GitHub Actions release workflow |
13 | 13 |
|
14 | 14 | Usage: |
15 | | - python tools/release.py [--dry-run] [--help] |
| 15 | + python tools/release.py [version] [--dry-run] [--help] |
| 16 | +
|
| 17 | +Examples: |
| 18 | + python tools/release.py # Bump patch version |
| 19 | + python tools/release.py 1.0.0 # Set version to 1.0.0 |
| 20 | + python tools/release.py 1.2.3 --dry-run # Test setting version to 1.2.3 |
16 | 21 | """ |
17 | 22 |
|
18 | 23 | import argparse |
@@ -82,6 +87,29 @@ def format_version(major: int, minor: int, patch: int) -> str: |
82 | 87 | return f"{major}.{minor}.{patch}" |
83 | 88 |
|
84 | 89 |
|
| 90 | +def compare_versions( |
| 91 | + version1: Tuple[int, int, int], version2: Tuple[int, int, int] |
| 92 | +) -> int: |
| 93 | + """ |
| 94 | + Compare two versions. |
| 95 | +
|
| 96 | + Args: |
| 97 | + version1: First version tuple (major, minor, patch) |
| 98 | + version2: Second version tuple (major, minor, patch) |
| 99 | +
|
| 100 | + Returns: |
| 101 | + -1 if version1 < version2 |
| 102 | + 0 if version1 == version2 |
| 103 | + 1 if version1 > version2 |
| 104 | + """ |
| 105 | + if version1 < version2: |
| 106 | + return -1 |
| 107 | + elif version1 > version2: |
| 108 | + return 1 |
| 109 | + else: |
| 110 | + return 0 |
| 111 | + |
| 112 | + |
85 | 113 | def get_current_version(pyproject_path: Path) -> str: |
86 | 114 | """ |
87 | 115 | Extract the current version from pyproject.toml. |
@@ -167,16 +195,28 @@ def main(): |
167 | 195 | formatter_class=argparse.RawDescriptionHelpFormatter, |
168 | 196 | epilog=""" |
169 | 197 | This script will: |
170 | | -1. Bump the patch version in pyproject.toml |
| 198 | +1. Bump the patch version in pyproject.toml (or set to specified version) |
171 | 199 | 2. Commit the change with message "Bump version to X.Y.Z" |
172 | 200 | 3. Create a git tag "vX.Y.Z-py" |
173 | 201 | 4. Push the tags to trigger the release workflow |
174 | 202 |
|
175 | 203 | The script must be run from the repository root. |
| 204 | +
|
| 205 | +Examples: |
| 206 | + python tools/release.py # Bump patch version |
| 207 | + python tools/release.py 1.0.0 # Set version to 1.0.0 |
| 208 | + python tools/release.py 1.2.3 --dry-run # Test setting version |
176 | 209 | """, |
177 | 210 | ) |
178 | 211 |
|
179 | 212 | parser.add_argument( |
| 213 | + "version", |
| 214 | + nargs="?", |
| 215 | + help="Optional: Specific version to set (e.g., 1.0.0). If not provided, patch version will be bumped.", |
| 216 | + ) |
| 217 | + |
| 218 | + parser.add_argument( |
| 219 | + "-n", |
180 | 220 | "--dry-run", |
181 | 221 | action="store_true", |
182 | 222 | help="Show what would be done without making changes", |
@@ -211,10 +251,35 @@ def main(): |
211 | 251 | current_version = get_current_version(pyproject_path) |
212 | 252 | print(f"Current version: {current_version}") |
213 | 253 |
|
214 | | - # Parse and bump version |
215 | | - major, minor, patch = parse_version(current_version) |
216 | | - new_patch = patch + 1 |
217 | | - new_version = format_version(major, minor, new_patch) |
| 254 | + # Parse current version |
| 255 | + current_major, current_minor, current_patch = parse_version(current_version) |
| 256 | + |
| 257 | + # Determine new version |
| 258 | + if args.version: |
| 259 | + # Use provided version |
| 260 | + try: |
| 261 | + new_major, new_minor, new_patch = parse_version(args.version) |
| 262 | + except ValueError as e: |
| 263 | + print(f"Error: {e}", file=sys.stderr) |
| 264 | + return 1 |
| 265 | + |
| 266 | + # Validate that new version is higher than current |
| 267 | + current_tuple = (current_major, current_minor, current_patch) |
| 268 | + new_tuple = (new_major, new_minor, new_patch) |
| 269 | + comparison = compare_versions(new_tuple, current_tuple) |
| 270 | + |
| 271 | + if comparison <= 0: |
| 272 | + print( |
| 273 | + f"Error: New version {args.version} must be higher than current version {current_version}", |
| 274 | + file=sys.stderr, |
| 275 | + ) |
| 276 | + return 1 |
| 277 | + |
| 278 | + new_version = args.version |
| 279 | + else: |
| 280 | + # Bump patch version |
| 281 | + new_patch = current_patch + 1 |
| 282 | + new_version = format_version(current_major, current_minor, new_patch) |
218 | 283 |
|
219 | 284 | print(f"New version: {new_version}") |
220 | 285 |
|
|
0 commit comments