Skip to content

Save profile data in home directory #1577

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jan 5, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 32 additions & 23 deletions src/CLU/Microsoft.CLU.Common/CLUEnvironment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,27 @@ public static string CLRName
}
}

/// <summary>
/// Get the home path according to the platform...
/// </summary>
/// <returns></returns>
private static string GetHomePath()
{
if (Platform.IsWindows)
{
string[] items = {
GetEnvironmentVariable("USERPROFILE"),
GetEnvironmentVariable("HOMEDRIVE") + GetEnvironmentVariable("HOMEPATH")
};

return items.First((s) => !String.IsNullOrEmpty(s));
}
else
{
return GetEnvironmentVariable("HOME");
}
}

/// <summary>
/// Keeps the script name used to invoke a command, e.g. 'azure'.
/// </summary>
Expand Down Expand Up @@ -169,7 +190,7 @@ internal static void SetEnvironmentVariable(string envVariableName, string value
{
if (IsThreadSafe)
{
lock(_cacheLock)
lock (_cacheLock)
{
_cache[envVariableName] = value;
}
Expand Down Expand Up @@ -281,25 +302,27 @@ public static class Session
/// <summary>
/// Gets the current session ID.
/// </summary>
public static string ID { get; private set; }
private static string ID { get; set; }

/// <summary>
/// The backing field for Directory property.
/// The backing field for SessionPath property.
/// </summary>
private static string _directory;
private static string _path;
/// <summary>
/// The directory to store session specific resources.
/// </summary>
public static string Directory
public static string SessionPath
{
get
{
if (_directory == null)
if (_path == null)
{
_directory = System.IO.Directory.CreateDirectory(Path.Combine(CLUEnvironment.GetRootPath(), "sessions", $"session_{ID}")).FullName;
var directory = Path.Combine(CLUEnvironment.GetHomePath(), ".azure");
System.IO.Directory.CreateDirectory(directory);
_path = Path.Combine(directory, $"{ID}.profile.json");
}

return _directory;
return _path;
}
}

Expand All @@ -313,21 +336,7 @@ static Session()
ID = Environment.GetEnvironmentVariable(Constants.SessionID);
if (string.IsNullOrEmpty(ID))
{
if (Platform.IsWindows)
{
var ancestorProcessId = Windows.Process.GetPPID();
ID = Convert.ToString(ancestorProcessId);
}
else if (Platform.IsUnixOrMacOSX)
{
var processGroupId = Unix.Process.GetPGRP();
ID = Convert.ToString(processGroupId);
}
else
{
// Unknown platform - use global session
ID = Constants.GlobalSessionID;
}
ID = Constants.DefaultSessionID;

Environment.SetEnvironmentVariable(Constants.SessionID, ID);
}
Expand Down
6 changes: 3 additions & 3 deletions src/CLU/Microsoft.CLU.Common/Constants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -212,12 +212,12 @@ public static class Constants
/// <summary>
/// The environment variable holding current session ID.
/// </summary>
internal const string SessionID = "CmdletSessionID";
internal const string SessionID = "AzureProfile";

/// <summary>
/// The global session ID.
/// The default session ID.
/// </summary>
internal const string GlobalSessionID = "global";
internal const string DefaultSessionID = "default";

#endregion
}
Expand Down
4 changes: 0 additions & 4 deletions src/CLU/Microsoft.CLU.Run/PackageManagementMode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -543,10 +543,6 @@ private static void GenerateScript(string cfgPath)
File.WriteAllLines(scriptPath, new string[]
{
"#!/bin/bash",
"if [ -z ${CmdletSessionID} ]",
"then",
" export CmdletSessionID=$PPID",
"fi",
"SCRIPTPATH=$(dirname \"$0\")",
$"$SCRIPTPATH/clurun -s {scriptName} -r $SCRIPTPATH/{Path.GetFileName(cfgPath)} \"$@\""
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public sealed class SessionState
public SessionState()
{
PSVariable = new PSVariableIntrinsics();
_stateFileFullPath = System.IO.Path.Combine(CLUEnvironment.Session.Directory, "session.state.json");
_stateFileFullPath = CLUEnvironment.Session.SessionPath;
}

/// <summary>
Expand Down