2121using Microsoft . Azure . Commands . Common . Authentication . Properties ;
2222using Newtonsoft . Json ;
2323using TraceLevel = System . Diagnostics . TraceLevel ;
24+ using System . Linq ;
2425
2526namespace Microsoft . Azure . Commands . Common . Authentication
2627{
@@ -29,6 +30,9 @@ namespace Microsoft.Azure.Commands.Common.Authentication
2930 /// </summary>
3031 public static class AzureSessionInitializer
3132 {
33+ private const string ContextAutosaveSettingFileName = ContextAutosaveSettings . AutoSaveSettingsFile ;
34+ private const string DataCollectionFileName = AzurePSDataCollectionProfile . DefaultFileName ;
35+
3236 /// <summary>
3337 /// Initialize the azure session if it is not already initialized
3438 /// </summary>
@@ -73,7 +77,43 @@ static IAzureTokenCache InitializeTokenCache(IDataStore store, string cacheDirec
7377 return result ;
7478 }
7579
76- static ContextAutosaveSettings InitializeSessionSettings ( IDataStore store , string profileDirectory , string settingsFile )
80+ static bool MigrateSettings ( IDataStore store , string oldProfileDirectory , string newProfileDirectory )
81+ {
82+ var filesToMigrate = new string [ ] { ContextAutosaveSettingFileName ,
83+ DataCollectionFileName } ;
84+ try
85+ {
86+ if ( ! store . DirectoryExists ( newProfileDirectory ) )
87+ {
88+ store . CreateDirectory ( newProfileDirectory ) ;
89+ }
90+
91+ // Only migrate if
92+ // (1) all files to migrate can be found in the old directory, and
93+ // (2) none of the files to migrate can be found in the new directory
94+ var oldFiles = Directory . EnumerateFiles ( oldProfileDirectory ) . Where ( f => filesToMigrate . Contains ( Path . GetFileName ( f ) ) ) ;
95+ var newFiles = Directory . EnumerateFiles ( newProfileDirectory ) . Where ( f => filesToMigrate . Contains ( Path . GetFileName ( f ) ) ) ;
96+ if ( store . DirectoryExists ( oldProfileDirectory ) && oldFiles . Count ( ) == filesToMigrate . Length && ! newFiles . Any ( ) )
97+ {
98+ foreach ( var oldFilePath in oldFiles )
99+ {
100+ var fileName = Path . GetFileName ( oldFilePath ) ;
101+ var newFilePath = Path . Combine ( newProfileDirectory , fileName ) ;
102+ store . CopyFile ( oldFilePath , newFilePath ) ;
103+ }
104+
105+ return true ;
106+ }
107+ }
108+ catch
109+ {
110+ // ignore exceptions in reading settings from disk
111+ }
112+
113+ return false ;
114+ }
115+
116+ static ContextAutosaveSettings InitializeSessionSettings ( IDataStore store , string profileDirectory , string settingsFile , bool migrated = false )
77117 {
78118 var result = new ContextAutosaveSettings
79119 {
@@ -92,11 +132,16 @@ static ContextAutosaveSettings InitializeSessionSettings(IDataStore store, strin
92132 {
93133 var settingsText = store . ReadFileAsText ( settingsPath ) ;
94134 ContextAutosaveSettings settings = JsonConvert . DeserializeObject < ContextAutosaveSettings > ( settingsText ) ;
95- result . CacheDirectory = settings . CacheDirectory ?? result . CacheDirectory ;
135+ result . CacheDirectory = migrated ? profileDirectory : settings . CacheDirectory ?? result . CacheDirectory ;
96136 result . CacheFile = settings . CacheFile ?? result . CacheFile ;
97- result . ContextDirectory = settings . ContextDirectory ?? result . ContextDirectory ;
137+ result . ContextDirectory = migrated ? profileDirectory : settings . ContextDirectory ?? result . ContextDirectory ;
98138 result . Mode = settings . Mode ;
99139 result . ContextFile = settings . ContextFile ?? result . ContextFile ;
140+ if ( migrated )
141+ {
142+ string autoSavePath = Path . Combine ( profileDirectory , settingsFile ) ;
143+ store . WriteFile ( autoSavePath , JsonConvert . SerializeObject ( result ) ) ;
144+ }
100145 }
101146 else
102147 {
@@ -127,8 +172,13 @@ static void InitializeDataCollection(IAzureSession session)
127172 static IAzureSession CreateInstance ( IDataStore dataStore = null )
128173 {
129174 string profilePath = Path . Combine (
130- Environment . GetFolderPath ( Environment . SpecialFolder . ApplicationData ) ,
175+ #if NETSTANDARD
176+ Environment . GetFolderPath ( Environment . SpecialFolder . UserProfile ) ,
131177 Resources . AzureDirectoryName ) ;
178+ string oldProfilePath = Path . Combine (
179+ #endif
180+ Environment . GetFolderPath ( Environment . SpecialFolder . ApplicationData ) ,
181+ Resources . OldAzureDirectoryName ) ;
132182 dataStore = dataStore ?? new DiskDataStore ( ) ;
133183
134184 var session = new AdalSession
@@ -142,7 +192,13 @@ static IAzureSession CreateInstance(IDataStore dataStore = null)
142192 ProfileFile = "AzureProfile.json" ,
143193 } ;
144194
145- var autoSave = InitializeSessionSettings ( dataStore , profilePath , ContextAutosaveSettings . AutoSaveSettingsFile ) ;
195+ var migrated =
196+ #if ! NETSTANDARD
197+ false ;
198+ #else
199+ MigrateSettings ( dataStore , oldProfilePath , profilePath ) ;
200+ #endif
201+ var autoSave = InitializeSessionSettings ( dataStore , profilePath , ContextAutosaveSettings . AutoSaveSettingsFile , migrated ) ;
146202 session . ARMContextSaveMode = autoSave . Mode ;
147203 session . ARMProfileDirectory = autoSave . ContextDirectory ;
148204 session . ARMProfileFile = autoSave . ContextFile ;
0 commit comments