21
21
using Microsoft . Azure . Commands . Common . Authentication . Properties ;
22
22
using Newtonsoft . Json ;
23
23
using TraceLevel = System . Diagnostics . TraceLevel ;
24
+ using System . Linq ;
24
25
25
26
namespace Microsoft . Azure . Commands . Common . Authentication
26
27
{
@@ -29,6 +30,9 @@ namespace Microsoft.Azure.Commands.Common.Authentication
29
30
/// </summary>
30
31
public static class AzureSessionInitializer
31
32
{
33
+ private const string ContextAutosaveSettingFileName = ContextAutosaveSettings . AutoSaveSettingsFile ;
34
+ private const string DataCollectionFileName = AzurePSDataCollectionProfile . DefaultFileName ;
35
+
32
36
/// <summary>
33
37
/// Initialize the azure session if it is not already initialized
34
38
/// </summary>
@@ -73,7 +77,43 @@ static IAzureTokenCache InitializeTokenCache(IDataStore store, string cacheDirec
73
77
return result ;
74
78
}
75
79
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 )
77
117
{
78
118
var result = new ContextAutosaveSettings
79
119
{
@@ -92,11 +132,16 @@ static ContextAutosaveSettings InitializeSessionSettings(IDataStore store, strin
92
132
{
93
133
var settingsText = store . ReadFileAsText ( settingsPath ) ;
94
134
ContextAutosaveSettings settings = JsonConvert . DeserializeObject < ContextAutosaveSettings > ( settingsText ) ;
95
- result . CacheDirectory = settings . CacheDirectory ?? result . CacheDirectory ;
135
+ result . CacheDirectory = migrated ? profileDirectory : settings . CacheDirectory ?? result . CacheDirectory ;
96
136
result . CacheFile = settings . CacheFile ?? result . CacheFile ;
97
- result . ContextDirectory = settings . ContextDirectory ?? result . ContextDirectory ;
137
+ result . ContextDirectory = migrated ? profileDirectory : settings . ContextDirectory ?? result . ContextDirectory ;
98
138
result . Mode = settings . Mode ;
99
139
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
+ }
100
145
}
101
146
else
102
147
{
@@ -127,8 +172,13 @@ static void InitializeDataCollection(IAzureSession session)
127
172
static IAzureSession CreateInstance ( IDataStore dataStore = null )
128
173
{
129
174
string profilePath = Path . Combine (
130
- Environment . GetFolderPath ( Environment . SpecialFolder . ApplicationData ) ,
175
+ #if NETSTANDARD
176
+ Environment . GetFolderPath ( Environment . SpecialFolder . UserProfile ) ,
131
177
Resources . AzureDirectoryName ) ;
178
+ string oldProfilePath = Path . Combine (
179
+ #endif
180
+ Environment . GetFolderPath ( Environment . SpecialFolder . ApplicationData ) ,
181
+ Resources . OldAzureDirectoryName ) ;
132
182
dataStore = dataStore ?? new DiskDataStore ( ) ;
133
183
134
184
var session = new AdalSession
@@ -142,7 +192,13 @@ static IAzureSession CreateInstance(IDataStore dataStore = null)
142
192
ProfileFile = "AzureProfile.json" ,
143
193
} ;
144
194
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 ) ;
146
202
session . ARMContextSaveMode = autoSave . Mode ;
147
203
session . ARMProfileDirectory = autoSave . ContextDirectory ;
148
204
session . ARMProfileFile = autoSave . ContextFile ;
0 commit comments