11# /// script
2- # requires-python = ">=3.10 "
2+ # requires-python = ">=3.11 "
33# dependencies = [
44# "towncrier>=24.8",
55# "uv>=0.8.6",
1212from __future__ import annotations
1313
1414import argparse
15+ import enum
1516import json
1617import subprocess
1718import sys
3132)
3233
3334
35+ class Bump (enum .StrEnum ):
36+ """The enumeration of supported version bump semantics."""
37+
38+ MAJOR = enum .auto ()
39+ MINOR = enum .auto ()
40+ PATCH = enum .auto ()
41+ STABLE = enum .auto ()
42+ ALPHA = enum .auto ()
43+ BETA = enum .auto ()
44+ RC = enum .auto ()
45+ POST = enum .auto ()
46+ DEV = enum .auto ()
47+
48+
3449def create_parser () -> argparse .ArgumentParser :
3550 """Create the argument parser."""
3651 parser = argparse .ArgumentParser (description = "make a release" )
37- parser .add_argument (
52+ mutex = parser .add_mutually_exclusive_group (required = True )
53+ mutex .add_argument (
54+ "--version" ,
55+ help = "set the version" ,
56+ )
57+ mutex .add_argument (
3858 "--bump" ,
39- required = True ,
59+ type = Bump ,
60+ choices = Bump ,
4061 help = "update the version using the given semantics" ,
4162 )
4263 parser .add_argument (
@@ -57,20 +78,18 @@ def check_repository() -> None:
5778 raise SystemExit (1 ) from None
5879
5980
60- def bump_version (bump : str ) -> str :
61- """Bump the version and return the new version."""
81+ def update_version (version : str | None = None , bump : str | None = None ) -> str :
82+ """Update the version and return the new version."""
83+ args = ["uv" , "version" , "--no-sync" , "--output-format" , "json" ]
84+
85+ if version :
86+ args .append (version )
87+
88+ if bump :
89+ args .extend (("--bump" , bump ))
90+
6291 try :
63- version_json = subprocess .check_output (
64- (
65- "uv" ,
66- "version" ,
67- "--no-sync" ,
68- "--output-format" ,
69- "json" ,
70- "--bump" ,
71- bump ,
72- )
73- )
92+ version_json = subprocess .check_output (args )
7493
7594 except subprocess .CalledProcessError :
7695 print_error ("An error occurred while bumping the version." )
@@ -80,17 +99,16 @@ def bump_version(bump: str) -> str:
8099 return version ["version" ]
81100
82101
83- @contextmanager
84- def switch_to_branch (branch : str ) -> Generator [None ]:
85- """Create a new branch and switch to it.
86-
87- It is removed on exit.
88- """
89- base_branch = subprocess .check_output (
102+ def get_branch () -> str :
103+ """Get the current branch."""
104+ return subprocess .check_output (
90105 ("git" , "rev-parse" , "--abbrev-ref" , "HEAD" ),
91106 text = True ,
92107 ).rstrip ()
93108
109+
110+ def create_branch (branch : str ) -> None :
111+ """Create a new branch."""
94112 try :
95113 run (("git" , "branch" , branch ))
96114
@@ -100,6 +118,16 @@ def switch_to_branch(branch: str) -> Generator[None]:
100118
101119 print (f"Created new branch { branch !r} ." )
102120
121+
122+ @contextmanager
123+ def switch_to_branch (branch : str ) -> Generator [None ]:
124+ """Create a new branch and switch to it.
125+
126+ It is removed on exit.
127+ """
128+ base_branch = get_branch ()
129+ create_branch (branch )
130+
103131 try :
104132 run (("git" , "checkout" , branch ))
105133 print (f"Switched from branch { base_branch !r} to branch { branch !r} ." )
@@ -143,14 +171,14 @@ def push_changes(branch: str, remote_branch: str = "main") -> None:
143171 print (f"Pushed changes from { branch !r} to 'origin/{ remote_branch } '." )
144172
145173
146- def main (argv : Sequence [str ] | None = None ) -> None :
174+ def main (argv : Sequence [str ] | None = None ) -> int :
147175 """Prepare a new release."""
148176 parser = create_parser ()
149177 args = parser .parse_args (argv )
150178
151179 check_repository ()
152180
153- version = bump_version ( args .bump )
181+ version = update_version ( args . version , args .bump )
154182
155183 release_branch = f"release/{ version } "
156184
@@ -171,14 +199,15 @@ def main(argv: Sequence[str] | None = None) -> None:
171199 print ("Dry run success!" )
172200 run (("git" , "tag" , "-d" , release_tag ))
173201 print (f"Removed release tag { release_tag !r} ." )
174- return
202+ return 0
175203
176204 push_changes (release_branch , "main" )
177205
178206 # Switch to the main branch.
179207 run (("git" , "checkout" , "main" ))
180208 run (("git" , "fetch" ))
181209 run (("git" , "reset" , "--hard" , "origin/main" ))
210+ return 0
182211
183212
184213if __name__ == "__main__" :
0 commit comments