@@ -26,6 +26,7 @@ internal class FileBasedInstaller : IInstaller
2626 private const string InstalledWorkloadSetsDir = "InstalledWorkloadSets" ;
2727 protected readonly string _dotnetDir ;
2828 protected readonly string _userProfileDir ;
29+ protected readonly string _workloadRootDir ;
2930 protected readonly DirectoryPath _tempPackagesDir ;
3031 private readonly INuGetPackageDownloader _nugetPackageDownloader ;
3132 private IWorkloadResolver _workloadResolver ;
@@ -57,7 +58,8 @@ public FileBasedInstaller(IReporter reporter,
5758 new FirstPartyNuGetPackageSigningVerifier ( ) , logger ,
5859 restoreActionConfig : _restoreActionConfig ) ;
5960 bool userLocal = WorkloadFileBasedInstall . IsUserLocal ( _dotnetDir , sdkFeatureBand . ToString ( ) ) ;
60- _workloadMetadataDir = Path . Combine ( userLocal ? _userProfileDir : _dotnetDir , "metadata" , "workloads" ) ;
61+ _workloadRootDir = userLocal ? _userProfileDir : _dotnetDir ;
62+ _workloadMetadataDir = Path . Combine ( _workloadRootDir , "metadata" , "workloads" ) ;
6163 _reporter = reporter ;
6264 _sdkFeatureBand = sdkFeatureBand ;
6365 _workloadResolver = workloadResolver ;
@@ -92,7 +94,7 @@ public WorkloadSet InstallWorkloadSet(ITransactionContext context, string worklo
9294 string workloadSetPackageVersion = WorkloadSet . WorkloadSetVersionToWorkloadSetPackageVersion ( workloadSetVersion , out workloadSetFeatureBand ) ;
9395 var workloadSetPackageId = GetManifestPackageId ( new ManifestId ( "Microsoft.NET.Workloads" ) , workloadSetFeatureBand ) ;
9496
95- var workloadSetPath = Path . Combine ( _dotnetDir , "sdk-manifests" , _sdkFeatureBand . ToString ( ) , "workloadsets" , workloadSetVersion ) ;
97+ var workloadSetPath = Path . Combine ( _workloadRootDir , "sdk-manifests" , _sdkFeatureBand . ToString ( ) , "workloadsets" , workloadSetVersion ) ;
9698
9799 try
98100 {
@@ -226,9 +228,7 @@ public void RepairWorkloads(IEnumerable<WorkloadId> workloadIds, SdkFeatureBand
226228
227229 string GetManifestInstallDirForFeatureBand ( string sdkFeatureBand )
228230 {
229- string rootInstallDir = WorkloadFileBasedInstall . IsUserLocal ( _dotnetDir , _sdkFeatureBand . ToString ( ) ) ? _userProfileDir : _dotnetDir ;
230- var manifestInstallDir = Path . Combine ( rootInstallDir , "sdk-manifests" , sdkFeatureBand ) ;
231- return manifestInstallDir ;
231+ return Path . Combine ( _workloadRootDir , "sdk-manifests" , sdkFeatureBand ) ;
232232 }
233233
234234 public void InstallWorkloadManifest ( ManifestVersionUpdate manifestUpdate , ITransactionContext transactionContext , DirectoryPath ? offlineCache = null )
@@ -341,7 +341,7 @@ public IEnumerable<WorkloadDownload> GetDownloads(IEnumerable<WorkloadId> worklo
341341
342342 public void GarbageCollect ( Func < string , IWorkloadResolver > getResolverForWorkloadSet , DirectoryPath ? offlineCache = null , bool cleanAllPacks = false )
343343 {
344- var garbageCollector = new WorkloadGarbageCollector ( _dotnetDir , _sdkFeatureBand , _installationRecordRepository . GetInstalledWorkloads ( _sdkFeatureBand ) , getResolverForWorkloadSet , Reporter . Verbose ) ;
344+ var garbageCollector = new WorkloadGarbageCollector ( _workloadRootDir , _sdkFeatureBand , _installationRecordRepository . GetInstalledWorkloads ( _sdkFeatureBand ) , getResolverForWorkloadSet , Reporter . Verbose ) ;
345345 garbageCollector . Collect ( ) ;
346346
347347 var featureBandsWithWorkloadInstallRecords = _installationRecordRepository . GetFeatureBandsWithInstallationRecords ( ) ;
@@ -479,46 +479,36 @@ public void GarbageCollect(Func<string, IWorkloadResolver> getResolverForWorkloa
479479
480480 public void AdjustWorkloadSetInInstallState ( SdkFeatureBand sdkFeatureBand , string workloadVersion )
481481 {
482- string path = Path . Combine ( WorkloadInstallType . GetInstallStateFolder ( _sdkFeatureBand , _dotnetDir ) , "default.json" ) ;
483- Directory . CreateDirectory ( Path . GetDirectoryName ( path ) ) ;
484- var installStateContents = InstallStateContents . FromPath ( path ) ;
485- installStateContents . WorkloadVersion = workloadVersion ;
486- File . WriteAllText ( path , installStateContents . ToString ( ) ) ;
482+ UpdateInstallState ( sdkFeatureBand , contents => contents . WorkloadVersion = workloadVersion ) ;
487483 }
488484
489485 public void RemoveManifestsFromInstallState ( SdkFeatureBand sdkFeatureBand )
490486 {
491- string path = Path . Combine ( WorkloadInstallType . GetInstallStateFolder ( _sdkFeatureBand , _dotnetDir ) , "default.json" ) ;
492-
493- if ( File . Exists ( path ) )
494- {
495- var installStateContents = InstallStateContents . FromString ( File . ReadAllText ( path ) ) ;
496- installStateContents . Manifests = null ;
497- File . WriteAllText ( path , installStateContents . ToString ( ) ) ;
498- }
487+ UpdateInstallState ( sdkFeatureBand , contents => contents . Manifests = null ) ;
499488 }
500489
501490 public void SaveInstallStateManifestVersions ( SdkFeatureBand sdkFeatureBand , Dictionary < string , string > manifestContents )
502491 {
503- string path = Path . Combine ( WorkloadInstallType . GetInstallStateFolder ( _sdkFeatureBand , _dotnetDir ) , "default.json" ) ;
504- Directory . CreateDirectory ( Path . GetDirectoryName ( path ) ) ;
505- var installStateContents = InstallStateContents . FromPath ( path ) ;
506- installStateContents . Manifests = manifestContents ;
507- File . WriteAllText ( path , installStateContents . ToString ( ) ) ;
492+ UpdateInstallState ( sdkFeatureBand , contents => contents . Manifests = manifestContents ) ;
508493 }
509494
510495 public void UpdateInstallMode ( SdkFeatureBand sdkFeatureBand , bool ? newMode )
511496 {
512- string path = Path . Combine ( WorkloadInstallType . GetInstallStateFolder ( sdkFeatureBand , _dotnetDir ) , "default.json" ) ;
513- Directory . CreateDirectory ( Path . GetDirectoryName ( path ) ) ;
514- var installStateContents = InstallStateContents . FromPath ( path ) ;
515- installStateContents . UseWorkloadSets = newMode ;
516- File . WriteAllText ( path , installStateContents . ToString ( ) ) ;
497+ UpdateInstallState ( sdkFeatureBand , contents => contents . UseWorkloadSets = newMode ) ;
517498
518499 var newModeString = newMode == null ? "<null>" : ( newMode . Value ? WorkloadConfigCommandParser . UpdateMode_WorkloadSet : WorkloadConfigCommandParser . UpdateMode_Manifests ) ;
519500 _reporter . WriteLine ( string . Format ( LocalizableStrings . UpdatedWorkloadMode , newModeString ) ) ;
520501 }
521502
503+ private void UpdateInstallState ( SdkFeatureBand sdkFeatureBand , Action < InstallStateContents > update )
504+ {
505+ string path = Path . Combine ( WorkloadInstallType . GetInstallStateFolder ( sdkFeatureBand , _workloadRootDir ) , "default.json" ) ;
506+ Directory . CreateDirectory ( Path . GetDirectoryName ( path ) ) ;
507+ var installStateContents = InstallStateContents . FromPath ( path ) ;
508+ update ( installStateContents ) ;
509+ File . WriteAllText ( path , installStateContents . ToString ( ) ) ;
510+ }
511+
522512 /// <summary>
523513 /// Remove all workload installation records that aren't from Visual Studio.
524514 /// </summary>
0 commit comments