33import xml .etree .ElementTree as ET
44import re
55import urllib .request
6+ import shutil
67from performance .common import get_repo_root_path
78from shared .precommands import PreCommands
89from logging import getLogger
@@ -137,6 +138,9 @@ def install_latest_maui(
137138 '''
138139 Install the latest maui workload using the provided feed.
139140 This function will create a rollback file and install the maui workload using that file.
141+
142+ If the SDK version from MAUI is earlier than the one in global.json, we temporarily
143+ move global.json out of the way to allow the earlier version to be installed.
140144 '''
141145
142146 if precommands .has_workload :
@@ -154,6 +158,11 @@ def install_latest_maui(
154158
155159 getLogger ().info (f"Installing the latest maui workload from feed { feed } " )
156160
161+ # Get the global.json file path
162+ global_json_path = os .path .join (get_repo_root_path (), "global.json" )
163+ global_json_backup_path = os .path .join (get_repo_root_path (), "global.json.bak" )
164+ global_json_needs_restore = False
165+
157166 # Get the latest published version of the maui workloads
158167 for workload in maui_rollback_dict .keys ():
159168 packages = precommands .get_packages_for_sdk_from_feed (workload , feed )
@@ -203,10 +212,64 @@ def install_latest_maui(
203212
204213 maui_rollback_dict [workload ] = f"{ latest_package ['latestVersion' ]} /{ latest_package ['sdk_version' ]} "
205214
206- # Create the rollback file
207- with open ("rollback_maui.json" , "w" , encoding = "utf-8" ) as f :
208- f .write (json .dumps (maui_rollback_dict , indent = 4 ))
209-
210- # Install the workload using the rollback file
211- getLogger ().info ("Installing maui workload with rollback file" )
212- precommands .install_workload ('maui' , ['--from-rollback-file' , 'rollback_maui.json' ])
215+ # Check if we need to temporarily move global.json out of the way
216+ try :
217+ # Get the SDK version from the first workload
218+ maui_sdk_version = ""
219+ for workload , version_info in maui_rollback_dict .items ():
220+ if version_info :
221+ maui_sdk_version = version_info .split ("/" )[1 ]
222+ break
223+
224+ if maui_sdk_version :
225+ # Read the current SDK version from global.json
226+ import json as json_lib
227+ current_sdk_version = ""
228+ try :
229+ with open (global_json_path , "r" ) as f :
230+ global_json_data = json_lib .load (f )
231+ current_sdk_version = global_json_data .get ("tools" , {}).get ("dotnet" , "" )
232+ getLogger ().info (f"Current SDK version in global.json: { current_sdk_version } " )
233+ except Exception as e :
234+ getLogger ().warning (f"Failed to read SDK version from global.json: { e } " )
235+
236+ # Compare versions to see if the MAUI SDK version is earlier
237+ if current_sdk_version and maui_sdk_version < current_sdk_version :
238+ getLogger ().info (f"MAUI SDK version { maui_sdk_version } is earlier than global.json version { current_sdk_version } . Temporarily moving global.json." )
239+ if os .path .exists (global_json_path ):
240+ try :
241+ # Backup global.json
242+ shutil .copy2 (global_json_path , global_json_backup_path )
243+ # Remove global.json
244+ os .rename (global_json_path , f"{ global_json_path } .tmp" )
245+ global_json_needs_restore = True
246+ except Exception as e :
247+ getLogger ().warning (f"Failed to temporarily move global.json: { e } " )
248+
249+ # Create the rollback file
250+ with open ("rollback_maui.json" , "w" , encoding = "utf-8" ) as f :
251+ f .write (json .dumps (maui_rollback_dict , indent = 4 ))
252+
253+ # Install the workload using the rollback file
254+ getLogger ().info ("Installing maui workload with rollback file" )
255+ precommands .install_workload ('maui' , ['--from-rollback-file' , 'rollback_maui.json' ])
256+
257+ finally :
258+ # Restore global.json if needed
259+ if global_json_needs_restore :
260+ getLogger ().info ("Restoring global.json" )
261+ try :
262+ if os .path .exists (f"{ global_json_path } .tmp" ):
263+ os .rename (f"{ global_json_path } .tmp" , global_json_path )
264+ elif os .path .exists (global_json_backup_path ):
265+ shutil .copy2 (global_json_backup_path , global_json_path )
266+ getLogger ().info ("Successfully restored global.json" )
267+ except Exception as e :
268+ getLogger ().error (f"Failed to restore global.json: { e } " )
269+ # Try one more time with the backup
270+ try :
271+ if os .path .exists (global_json_backup_path ):
272+ shutil .copy2 (global_json_backup_path , global_json_path )
273+ getLogger ().info ("Successfully restored global.json from backup" )
274+ except Exception as e2 :
275+ getLogger ().error (f"Failed to restore global.json from backup: { e2 } " )
0 commit comments