@@ -118,6 +118,102 @@ def get_current_branch():
118118 return 'develop'
119119
120120
121+ def update_version_json (version_string ):
122+ """Update the version.json file with the new version string."""
123+ try :
124+ version_file = 'EmbeddedProto/version.json'
125+ with open (version_file , 'r' ) as f :
126+ version_data = json .load (f )
127+
128+ # Update the version
129+ version_data ['version' ] = version_string
130+
131+ # Write back to the file
132+ with open (version_file , 'w' ) as f :
133+ json .dump (version_data , f , indent = 2 )
134+
135+ print (f"Debug: Updated version.json with version: '{ version_string } '" )
136+ return True
137+ except Exception as e :
138+ print (f"Warning: Failed to update version.json: { e } " )
139+ return False
140+
141+ def update_version_h (version_string ):
142+ """Update the Version.h file with the new version string."""
143+ try :
144+ version_h_file = 'src/Version.h'
145+
146+ # Parse the version string to extract components
147+ version_parts = re .match (r'(\d+)\.(\d+)\.(\d+)(.*)' , version_string )
148+ if not version_parts :
149+ print (f"Warning: Could not parse version string: '{ version_string } '" )
150+ return False
151+
152+ major = version_parts .group (1 )
153+ minor = version_parts .group (2 )
154+ patch = version_parts .group (3 )
155+ suffix = version_parts .group (4 ) or "" # This will be empty, rc{N}, or .dev{N}
156+
157+ # Extract RC number if present
158+ rc_match = re .match (r'rc(\d+)' , suffix )
159+ rc_number = rc_match .group (1 ) if rc_match else "0"
160+ has_rc = bool (rc_match )
161+
162+ # Extract DEV number if present
163+ dev_match = re .match (r'\.dev(\d+)' , suffix )
164+ dev_number = dev_match .group (1 ) if dev_match else "0"
165+ has_dev = bool (dev_match )
166+
167+ # Read the current file
168+ with open (version_h_file , 'r' ) as f :
169+ content = f .read ()
170+
171+ # Replace the version macros
172+ content = re .sub (r'#define EMBEDDEDPROTO_VERSION_MAJOR \d+' ,
173+ f'#define EMBEDDEDPROTO_VERSION_MAJOR { major } ' , content )
174+ content = re .sub (r'#define EMBEDDEDPROTO_VERSION_MINOR \d+' ,
175+ f'#define EMBEDDEDPROTO_VERSION_MINOR { minor } ' , content )
176+ content = re .sub (r'#define EMBEDDEDPROTO_VERSION_PATCH \d+' ,
177+ f'#define EMBEDDEDPROTO_VERSION_PATCH { patch } ' , content )
178+
179+ # Add or update RC macro if needed
180+ rc_define = f'//! The release candidate number (0 if not a release candidate).\n #define EMBEDDEDPROTO_VERSION_RC { rc_number } \n '
181+ if '#define EMBEDDEDPROTO_VERSION_RC' in content :
182+ content = re .sub (r'//!.*\n#define EMBEDDEDPROTO_VERSION_RC \d+' ,
183+ rc_define .strip (), content )
184+ else :
185+ # Add after PATCH definition
186+ patch_pos = content .find ('#define EMBEDDEDPROTO_VERSION_PATCH' )
187+ if patch_pos != - 1 :
188+ end_line_pos = content .find ('\n ' , patch_pos ) + 1
189+ content = content [:end_line_pos ] + '\n ' + rc_define + content [end_line_pos :]
190+
191+ # Add or update DEV macro if needed
192+ dev_define = f'//! The development build number (0 if not a development build).\n #define EMBEDDEDPROTO_VERSION_DEV { dev_number } \n '
193+ if '#define EMBEDDEDPROTO_VERSION_DEV' in content :
194+ content = re .sub (r'//!.*\n#define EMBEDDEDPROTO_VERSION_DEV \d+' ,
195+ dev_define .strip (), content )
196+ else :
197+ # Add after RC definition
198+ rc_pos = content .find ('#define EMBEDDEDPROTO_VERSION_RC' )
199+ if rc_pos != - 1 :
200+ end_line_pos = content .find ('\n ' , rc_pos ) + 1
201+ content = content [:end_line_pos ] + '\n ' + dev_define + content [end_line_pos :]
202+
203+ # Update version string
204+ content = re .sub (r'#define EMBEDDEDPROTO_VERSION_STRING "[^"]+"' ,
205+ f'#define EMBEDDEDPROTO_VERSION_STRING "{ version_string } "' , content )
206+
207+ # Write back to the file
208+ with open (version_h_file , 'w' ) as f :
209+ f .write (content )
210+
211+ print (f"Debug: Updated Version.h with version: '{ version_string } '" )
212+ return True
213+ except Exception as e :
214+ print (f"Warning: Failed to update Version.h: { e } " )
215+ return False
216+
121217def get_version ():
122218 try :
123219 version_file = 'EmbeddedProto/version.json'
@@ -129,8 +225,11 @@ def get_version():
129225 try :
130226 with open (version_file , 'r' ) as f :
131227 version_data = json .load (f )
132- base_version = version_data .get ('version' , '0.0.0' )
133- print (f"Debug: Base version from version.json: '{ base_version } '" )
228+ version_from_file = version_data .get ('version' , '0.0.0' )
229+
230+ # Strip any existing rc or dev suffix to get the base version
231+ base_version = re .sub (r'(rc\d+|\.dev\d+)$' , '' , version_from_file )
232+ print (f"Debug: Base version (stripped): '{ base_version } '" )
134233 except (FileNotFoundError , json .JSONDecodeError ) as e :
135234 print (f"Debug: Error reading version.json: { e } " )
136235 # Fallback if version file is missing or invalid
@@ -150,11 +249,23 @@ def get_version():
150249 print (f"Debug: Detected release branch, using GITHUB_RUN_NUMBER '{ build_number } ' for rc suffix" )
151250 full_version = f"{ base_version } rc{ build_number } "
152251 print (f"Debug: Using release candidate version: '{ full_version } '" )
252+
253+ # Only update files if the version has changed
254+ if full_version != version_from_file :
255+ print (f"Debug: Updating version files from '{ version_from_file } ' to '{ full_version } '" )
256+ update_version_json (full_version )
257+ update_version_h (full_version )
153258
154259 else :
155260 # Development or feature branch
156261 full_version = f"{ base_version } .dev{ build_number } "
157262 print (f"Debug: Using development version: '{ full_version } '" )
263+
264+ # Only update files if the version has changed
265+ if full_version != version_from_file :
266+ print (f"Debug: Updating version files from '{ version_from_file } ' to '{ full_version } '" )
267+ update_version_json (full_version )
268+ update_version_h (full_version )
158269
159270 return full_version
160271 except Exception as e :
0 commit comments