Skip to content

Commit 4ee4d39

Browse files
committed
release.py: accept optional new version from command line
1 parent 577a764 commit 4ee4d39

File tree

1 file changed

+72
-7
lines changed

1 file changed

+72
-7
lines changed

tools/release.py

Lines changed: 72 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,18 @@
66
Release automation script for the TypeAgent Python package.
77
88
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
1010
2. Commits the change
1111
3. Creates a git tag in the format v{major}.{minor}.{patch}-py
1212
4. Pushes the tags to trigger the GitHub Actions release workflow
1313
1414
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
1621
"""
1722

1823
import argparse
@@ -82,6 +87,29 @@ def format_version(major: int, minor: int, patch: int) -> str:
8287
return f"{major}.{minor}.{patch}"
8388

8489

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+
85113
def get_current_version(pyproject_path: Path) -> str:
86114
"""
87115
Extract the current version from pyproject.toml.
@@ -167,16 +195,28 @@ def main():
167195
formatter_class=argparse.RawDescriptionHelpFormatter,
168196
epilog="""
169197
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)
171199
2. Commit the change with message "Bump version to X.Y.Z"
172200
3. Create a git tag "vX.Y.Z-py"
173201
4. Push the tags to trigger the release workflow
174202
175203
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
176209
""",
177210
)
178211

179212
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",
180220
"--dry-run",
181221
action="store_true",
182222
help="Show what would be done without making changes",
@@ -211,10 +251,35 @@ def main():
211251
current_version = get_current_version(pyproject_path)
212252
print(f"Current version: {current_version}")
213253

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)
218283

219284
print(f"New version: {new_version}")
220285

0 commit comments

Comments
 (0)