Skip to content

Commit c3c21e6

Browse files
committed
Merge pull request #1577 from johanste/SaveProfileDataInHomeDirectory
Save profile data in home directory
2 parents 9a16de0 + d5eac3c commit c3c21e6

File tree

4 files changed

+36
-31
lines changed

4 files changed

+36
-31
lines changed

src/CLU/Microsoft.CLU.Common/CLUEnvironment.cs

Lines changed: 32 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,27 @@ public static string CLRName
107107
}
108108
}
109109

110+
/// <summary>
111+
/// Get the home path according to the platform...
112+
/// </summary>
113+
/// <returns></returns>
114+
private static string GetHomePath()
115+
{
116+
if (Platform.IsWindows)
117+
{
118+
string[] items = {
119+
GetEnvironmentVariable("USERPROFILE"),
120+
GetEnvironmentVariable("HOMEDRIVE") + GetEnvironmentVariable("HOMEPATH")
121+
};
122+
123+
return items.First((s) => !String.IsNullOrEmpty(s));
124+
}
125+
else
126+
{
127+
return GetEnvironmentVariable("HOME");
128+
}
129+
}
130+
110131
/// <summary>
111132
/// Keeps the script name used to invoke a command, e.g. 'azure'.
112133
/// </summary>
@@ -169,7 +190,7 @@ internal static void SetEnvironmentVariable(string envVariableName, string value
169190
{
170191
if (IsThreadSafe)
171192
{
172-
lock(_cacheLock)
193+
lock (_cacheLock)
173194
{
174195
_cache[envVariableName] = value;
175196
}
@@ -281,25 +302,27 @@ public static class Session
281302
/// <summary>
282303
/// Gets the current session ID.
283304
/// </summary>
284-
public static string ID { get; private set; }
305+
private static string ID { get; set; }
285306

286307
/// <summary>
287-
/// The backing field for Directory property.
308+
/// The backing field for SessionPath property.
288309
/// </summary>
289-
private static string _directory;
310+
private static string _path;
290311
/// <summary>
291312
/// The directory to store session specific resources.
292313
/// </summary>
293-
public static string Directory
314+
public static string SessionPath
294315
{
295316
get
296317
{
297-
if (_directory == null)
318+
if (_path == null)
298319
{
299-
_directory = System.IO.Directory.CreateDirectory(Path.Combine(CLUEnvironment.GetRootPath(), "sessions", $"session_{ID}")).FullName;
320+
var directory = Path.Combine(CLUEnvironment.GetHomePath(), ".azure");
321+
System.IO.Directory.CreateDirectory(directory);
322+
_path = Path.Combine(directory, $"{ID}.profile.json");
300323
}
301324

302-
return _directory;
325+
return _path;
303326
}
304327
}
305328

@@ -313,21 +336,7 @@ static Session()
313336
ID = Environment.GetEnvironmentVariable(Constants.SessionID);
314337
if (string.IsNullOrEmpty(ID))
315338
{
316-
if (Platform.IsWindows)
317-
{
318-
var ancestorProcessId = Windows.Process.GetPPID();
319-
ID = Convert.ToString(ancestorProcessId);
320-
}
321-
else if (Platform.IsUnixOrMacOSX)
322-
{
323-
var processGroupId = Unix.Process.GetPGRP();
324-
ID = Convert.ToString(processGroupId);
325-
}
326-
else
327-
{
328-
// Unknown platform - use global session
329-
ID = Constants.GlobalSessionID;
330-
}
339+
ID = Constants.DefaultSessionID;
331340

332341
Environment.SetEnvironmentVariable(Constants.SessionID, ID);
333342
}

src/CLU/Microsoft.CLU.Common/Constants.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -212,12 +212,12 @@ public static class Constants
212212
/// <summary>
213213
/// The environment variable holding current session ID.
214214
/// </summary>
215-
internal const string SessionID = "CmdletSessionID";
215+
internal const string SessionID = "AzureProfile";
216216

217217
/// <summary>
218-
/// The global session ID.
218+
/// The default session ID.
219219
/// </summary>
220-
internal const string GlobalSessionID = "global";
220+
internal const string DefaultSessionID = "default";
221221

222222
#endregion
223223
}

src/CLU/Microsoft.CLU.Run/PackageManagementMode.cs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -543,10 +543,6 @@ private static void GenerateScript(string cfgPath)
543543
File.WriteAllLines(scriptPath, new string[]
544544
{
545545
"#!/bin/bash",
546-
"if [ -z ${CmdletSessionID} ]",
547-
"then",
548-
" export CmdletSessionID=$PPID",
549-
"fi",
550546
"SCRIPTPATH=$(dirname \"$0\")",
551547
$"$SCRIPTPATH/clurun -s {scriptName} -r $SCRIPTPATH/{Path.GetFileName(cfgPath)} \"$@\""
552548
});

src/CLU/Microsoft.CLU/System.Management.Automation/SessionState.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public sealed class SessionState
1717
public SessionState()
1818
{
1919
PSVariable = new PSVariableIntrinsics();
20-
_stateFileFullPath = System.IO.Path.Combine(CLUEnvironment.Session.Directory, "session.state.json");
20+
_stateFileFullPath = CLUEnvironment.Session.SessionPath;
2121
}
2222

2323
/// <summary>

0 commit comments

Comments
 (0)