1+ import argparse
2+ import subprocess
3+ import re
4+
5+ # The script is in the 'Build' folder, so the path to the version file is one level up.
6+ VERSION_FILE = '../Src/MasterVersionInfo.txt'
7+
8+ def run_command (command , check = True ):
9+ """Runs a shell command and returns the output."""
10+ try :
11+ result = subprocess .run (command , shell = True , check = check , capture_output = True , text = True )
12+ return result .stdout .strip ()
13+ except subprocess .CalledProcessError as e :
14+ print (f"Error executing command: { command } " )
15+ print (e .stderr )
16+ return None
17+
18+ def read_version_file ():
19+ """Reads the version file and returns a dictionary."""
20+ versions = {}
21+ try :
22+ with open (VERSION_FILE , 'r' ) as f :
23+ for line in f :
24+ if '=' in line :
25+ key , value = line .strip ().split ('=' , 1 )
26+ versions [key ] = value
27+ except FileNotFoundError :
28+ print (f"Error: Version file not found at { VERSION_FILE } " )
29+ return None
30+ return versions
31+
32+ def write_version_file (versions ):
33+ """Writes the new version to the file."""
34+ try :
35+ with open (VERSION_FILE , 'w' ) as f :
36+ for key , value in versions .items ():
37+ f .write (f"{ key } ={ value } \n " )
38+ except Exception as e :
39+ print (f"Error writing to version file: { e } " )
40+ return False
41+ return True
42+
43+ def main ():
44+ parser = argparse .ArgumentParser (description = "Bump software version for release." )
45+ group = parser .add_mutually_exclusive_group (required = True )
46+ group .add_argument ('-m' , '--major' , action = 'store_true' , help = 'Bump the major version.' )
47+ group .add_argument ('-i' , '--minor' , action = 'store_true' , help = 'Bump the minor version.' )
48+ group .add_argument ('-r' , '--revision' , action = 'store_true' , help = 'Bump the revision version.' )
49+ parser .add_argument ('-s' , '--stability' , choices = ['Alpha' , 'Beta' , '' ], default = None , help = 'Set the stability level.' )
50+ args = parser .parse_args ()
51+
52+ # Read current version
53+ current_versions = read_version_file ()
54+ if not current_versions :
55+ return
56+
57+ fw_major = int (current_versions .get ('FWMAJOR' , 0 ))
58+ fw_minor = int (current_versions .get ('FWMINOR' , 0 ))
59+ fw_revision = int (current_versions .get ('FWREVISION' , 0 ))
60+ fw_beta = current_versions .get ('FWBETAVERSION' , '' )
61+
62+ # Get current branch
63+ current_branch = run_command ("git rev-parse --abbrev-ref HEAD" )
64+ if not current_branch :
65+ return
66+
67+ # Validate branch name
68+ branch_pattern = rf"^(release/{ fw_major } \.{ fw_minor } |hotfix/{ fw_major } \.{ fw_minor } \.{ fw_revision } )$"
69+ if not re .match (branch_pattern , current_branch ):
70+ print (f"Error: Current branch '{ current_branch } ' does not match the expected format." )
71+ return
72+
73+ # Git stash
74+ print ("Stashing any local changes..." )
75+ if run_command ("git stash" ) is None :
76+ print ("Git stash failed. Aborting." )
77+ return
78+
79+ # Git pull
80+ print ("Performing git pull..." )
81+ if run_command ("git pull" ) is None :
82+ print ("Git pull failed. Please resolve issues manually and re-run." )
83+ return
84+
85+ # Calculate new version
86+ new_versions = current_versions .copy ()
87+ if args .major :
88+ new_versions ['FWMAJOR' ] = str (fw_major + 1 )
89+ new_versions ['FWMINOR' ] = '0'
90+ new_versions ['FWREVISION' ] = '0'
91+ elif args .minor :
92+ new_versions ['FWMINOR' ] = str (fw_minor + 1 )
93+ new_versions ['FWREVISION' ] = '0'
94+ elif args .revision :
95+ new_versions ['FWREVISION' ] = str (fw_revision + 1 )
96+
97+ # Handle stability only if the flag was provided
98+ if args .stability is not None :
99+ new_versions ['FWBETAVERSION' ] = args .stability
100+
101+ # Display changes
102+ print ("--- Version Bump ---" )
103+ print (f"Old Version: { fw_major } .{ fw_minor } .{ fw_revision } { fw_beta } " )
104+ print (f"New Version: { new_versions ['FWMAJOR' ]} .{ new_versions ['FWMINOR' ]} .{ new_versions ['FWREVISION' ]} { new_versions ['FWBETAVERSION' ]} " )
105+
106+ # Write the new version file
107+ if not write_version_file (new_versions ):
108+ return
109+
110+ # Git commit
111+ new_version_string = f"{ new_versions ['FWMAJOR' ]} .{ new_versions ['FWMINOR' ]} .{ new_versions ['FWREVISION' ]} "
112+ if new_versions ['FWBETAVERSION' ]:
113+ new_version_string += f" { new_versions ['FWBETAVERSION' ]} "
114+
115+ print ("Committing version bump..." )
116+ run_command (f"git add { VERSION_FILE } " )
117+ if run_command (f"git commit -m \" Bump version to { new_version_string } \" " ) is None :
118+ print ("Git commit failed. Aborting." )
119+ return
120+
121+ print ("Version bump successful. Ready to push." )
122+
123+ if __name__ == "__main__" :
124+ main ()
0 commit comments