44import re
55import sys
66from pathlib import Path
7+ from typing import Optional
78
89
9- def update_version (version_type = "dev" , custom_suffix = None ):
10+ def update_version (version_type : str = "dev" , custom_suffix : Optional [ str ] = None ) -> str :
1011 """
11- Update version in pyproject.toml based on version type.
12-
13- Args:
14- version_type: 'dev', 'release', 'rc', or 'custom'
15- custom_suffix: Custom suffix for version (used with 'custom' type)
12+ Update the `version` field in `pyproject.toml` for supported manual build types.
13+
14+ - dev: sets suffix to `.devYYMMDD` based on today's date.
15+ - custom: appends `custom_suffix` verbatim to the base X.Y.Z (e.g., rc1, .post2, -alpha).
16+ - Other version types are no-ops and keep the current version unchanged.
17+
18+ Returns the new version string that was written.
1619 """
1720 # Read pyproject.toml
1821 pyproject_path = Path ("pyproject.toml" )
1922 if not pyproject_path .exists ():
2023 raise FileNotFoundError ("pyproject.toml not found" )
2124
22- content = pyproject_path .read_text ()
25+ content : str = pyproject_path .read_text ()
2326
2427 # Extract current version
25- version_match = re .search (r'^version\s*=\s*"([^"]+)"' , content , re .MULTILINE )
28+ version_match : Optional [ re . Match [ str ]] = re .search (r'^version\s*=\s*"([^"]+)"' , content , re .MULTILINE )
2629 if not version_match :
2730 raise ValueError ("Could not find version in pyproject.toml" )
2831
29- current_version = version_match .group (1 )
32+ current_version : str = version_match .group (1 )
3033
3134 # Parse the base version (remove any existing suffixes)
32- base_version_match = re .match (r'^(\d+\.\d+\.\d+)' , current_version )
35+ base_version_match : Optional [ re . Match [ str ]] = re .match (r'^(\d+\.\d+\.\d+)' , current_version )
3336 if not base_version_match :
3437 raise ValueError (f"Invalid version format: { current_version } " )
3538
36- base_version = base_version_match .group (1 )
39+ base_version : str = base_version_match .group (1 )
3740
38- # Generate new version based on type
41+ # Only dev/custom builds mutate the version; all others keep current version
3942 if version_type == "dev" :
40- date_suffix = datetime .datetime .now ().strftime ("%y%m%d" )
41- new_version = f"{ base_version } .dev{ date_suffix } "
42- elif version_type == "release" :
43- # For release, we keep the current version as-is
44- new_version = current_version
45- elif version_type == "rc" :
46- # For release candidate, extract rc number from custom_suffix or default to rc1
47- if custom_suffix and custom_suffix .startswith ("rc" ):
48- new_version = f"{ base_version } { custom_suffix } "
49- else :
50- rc_num = custom_suffix if custom_suffix else "1"
51- new_version = f"{ base_version } rc{ rc_num } "
52- elif version_type == "post" :
53- # For post-release, extract post number from custom_suffix or increment existing
54- if custom_suffix and custom_suffix .startswith (".post" ):
55- new_version = f"{ base_version } { custom_suffix } "
56- else :
57- post_num = custom_suffix if custom_suffix else "1"
58- new_version = f"{ base_version } .post{ post_num } "
43+ # Generate a date-based dev suffix like .dev250101
44+ date_suffix : str = datetime .datetime .now ().strftime ("%y%m%d" )
45+ new_version : str = f"{ base_version } .dev{ date_suffix } "
5946 elif version_type == "custom" :
60- if custom_suffix :
61- new_version = f"{ base_version } { custom_suffix } "
62- else :
63- raise ValueError ("Custom suffix required for custom version type" )
47+ if not custom_suffix :
48+ raise ValueError ("custom_suffix is required when version_type is 'custom'" )
49+ new_version = f"{ base_version } { custom_suffix } "
6450 else :
65- raise ValueError (f"Unknown version type: { version_type } " )
51+ print (f"No change: leaving version as { current_version } " )
52+ return current_version
6653
6754 # Update version in content
68- updated_content = re .sub (
55+ updated_content : str = re .sub (
6956 r'^version\s*=\s*"[^"]+"' ,
7057 f'version = "{ new_version } "' ,
7158 content ,
@@ -79,24 +66,11 @@ def update_version(version_type="dev", custom_suffix=None):
7966
8067
8168if __name__ == "__main__" :
82- # Parse command line arguments
83- if len (sys .argv ) < 2 :
84- # Default to dev version
85- update_version ("dev" )
86- elif len (sys .argv ) == 2 :
87- # First argument could be version_type or custom_suffix for backward compatibility
88- arg = sys .argv [1 ]
89- if arg in ["dev" , "release" , "rc" , "post" , "custom" ]:
90- update_version (arg )
91- else :
92- # Treat as custom suffix for backward compatibility
93- update_version ("custom" , arg )
94- elif len (sys .argv ) == 3 :
95- # version_type and custom_suffix
96- version_type = sys .argv [1 ]
97- custom_suffix = sys .argv [2 ]
98- update_version (version_type , custom_suffix )
99- else :
100- print ("Usage: update_version.py [version_type] [custom_suffix]" )
101- print ("Version types: dev, release, rc, post, custom" )
102- sys .exit (1 )
69+ # Accept optional args for compatibility; act on 'dev' or 'custom'
70+ version_type_arg : str = "dev"
71+ if len (sys .argv ) >= 2 :
72+ version_type_arg = sys .argv [1 ]
73+ custom_suffix_arg : Optional [str ] = None
74+ if len (sys .argv ) >= 3 :
75+ custom_suffix_arg = sys .argv [2 ]
76+ update_version (version_type_arg , custom_suffix_arg )
0 commit comments