Initial XDG support and folder default changes

Conflicts:
	src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHost.cs
This commit is contained in:
v-alexjo 2016-05-24 14:29:00 -07:00 committed by Andrew Schwartzmeyer
parent fd649c0f77
commit 54c64c9db6
6 changed files with 88 additions and 22 deletions

View file

@ -185,14 +185,18 @@ namespace Microsoft.PowerShell
{ {
profileDir = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) + profileDir = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) +
@"\Microsoft\Windows\PowerShell"; @"\Microsoft\Windows\PowerShell";
} else }
else
{ {
profileDir = System.IO.Path.Combine(Environment.GetEnvironmentVariable("HOME"), ".powershell"); //check if the user has set an XDG path in their environment variables
profileDir = Platform.SelectProductNameForDirectory("profile");
} }
if (!Directory.Exists(profileDir)) if (!Directory.Exists(profileDir)) //xdg value may have been set but not a valid directory
{ {
Directory.CreateDirectory(profileDir); Console.WriteLine("The selected directory for the profile does not exist. Using the default path.");
profileDir = Platform.SelectProductNameForDirectory("default");
} }
ClrFacade.SetProfileOptimizationRoot(profileDir); ClrFacade.SetProfileOptimizationRoot(profileDir);
} }
@ -268,7 +272,6 @@ namespace Microsoft.PowerShell
: "StartupProfileData-NonInteractive"); : "StartupProfileData-NonInteractive");
exitCode = theConsoleHost.Run(cpp, !string.IsNullOrEmpty(preStartWarning)); exitCode = theConsoleHost.Run(cpp, !string.IsNullOrEmpty(preStartWarning));
} }
} }
finally finally
{ {

View file

@ -178,14 +178,25 @@ namespace Microsoft.PowerShell
} }
else else
{ {
//PSReadline does not have access to Utils.CorePSPlatform. Must set PSReadline path seperately
string modulepath = System.Environment.GetEnvironmentVariable("XDG_CACHE_HOME");
if (String.IsNullOrEmpty(modulepath))
{
modulepath = System.IO.Path.Combine(
Environment.GetEnvironmentVariable("HOME"),
".config/powershell/modules");
}
HistorySavePath = System.IO.Path.Combine( HistorySavePath = System.IO.Path.Combine(
Environment.GetEnvironmentVariable("HOME"), Environment.GetEnvironmentVariable("HOME"),
".powershell", modulepath,
"PSReadLine", "PSReadLine",
hostName + "_history.txt"); hostName + "_history.txt");
}
#else }
HistorySavePath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) #else
HistorySavePath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)
+ @"\Microsoft\Windows\PowerShell\PSReadline\" + hostName + "_history.txt"; + @"\Microsoft\Windows\PowerShell\PSReadline\" + hostName + "_history.txt";
#endif #endif
CommandValidationHandler = null; CommandValidationHandler = null;
@ -205,7 +216,7 @@ namespace Microsoft.PowerShell
"Where-Object", "?", "where", "Where-Object", "?", "where",
}; };
} }
public EditMode EditMode { get; set; } public EditMode EditMode { get; set; }
public string ContinuationPrompt { get; set; } public string ContinuationPrompt { get; set; }

View file

@ -96,8 +96,57 @@ namespace System.Management.Automation
"WSMan.format.ps1xml" "WSMan.format.ps1xml"
}; };
// directory location of PowerShell for profile loading // function for choosing directory location of PowerShell for profile loading
public static string ProductNameForDirectory = ".powershell"; public static string SelectProductNameForDirectory (string dir){
string xdgconfighome = System.Environment.GetEnvironmentVariable("XDG_CONFIG_HOME");
string xdgdatahome = System.Environment.GetEnvironmentVariable("XDG_DATA_HOME");
string xdgcachehome = System.Environment.GetEnvironmentVariable("XDG_CACHE_HOME");
//the user has set XDG_CONFIG_HOME corrresponding to profile path
if (!String.IsNullOrEmpty(xdgconfighome) && dir == "profile")
{
return xdgconfighome;
}
//the user has set XDG_DATA_HOME corresponding to module path
if (!String.IsNullOrEmpty(xdgdatahome) && dir == "modules")
{
return xdgdatahome;
}
//the user has set XDG_CACHE_HOME
if (!String.IsNullOrEmpty(xdgcachehome) && dir == "history")
{
return xdgcachehome;
}
if (dir == "default")
{
return @".config/powershell";
}
//the user has set XDG_DATA_HOME
else //xdg values have not been set
{
if (dir == "profile" || dir == "history")
{
return @".config/powershell"; //default on Linux
}
if (dir == "modules")
{
if (!Directory.Exists(@".config/powershell/modules"))
{
Directory.CreateDirectory(@".config/powershell/modules");
}
return @".config/powershell/modules"; //default on Linux
}
return @".config/powershell";
}
}
// ComObjectType is null on CoreCLR for Linux since there is // ComObjectType is null on CoreCLR for Linux since there is
// no COM support on Linux // no COM support on Linux

View file

@ -548,12 +548,7 @@ namespace System.Management.Automation
/// <returns>personal module path</returns> /// <returns>personal module path</returns>
internal static string GetPersonalModulePath() internal static string GetPersonalModulePath()
{ {
string personalModuleRoot = Path.Combine( string personalModuleRoot = Platform.SelectProductNameForDirectory("modules");
Path.Combine(
Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments),
Utils.ProductNameForDirectory),
Utils.ModuleDirectory);
return personalModuleRoot; return personalModuleRoot;
} }
@ -597,6 +592,7 @@ namespace System.Management.Automation
psHome = psHome.ToLowerInvariant().Replace("\\syswow64\\", "\\system32\\"); psHome = psHome.ToLowerInvariant().Replace("\\syswow64\\", "\\system32\\");
} }
Interlocked.CompareExchange(ref SystemWideModulePath, Path.Combine(psHome, Utils.ModuleDirectory), null); Interlocked.CompareExchange(ref SystemWideModulePath, Path.Combine(psHome, Utils.ModuleDirectory), null);
} }
return SystemWideModulePath; return SystemWideModulePath;

View file

@ -606,13 +606,13 @@ namespace System.Management.Automation
/// Profile uses this to control profile loading. /// Profile uses this to control profile loading.
/// </remarks> /// </remarks>
internal static string ProductNameForDirectory = internal static string ProductNameForDirectory =
Platform.IsWindows ? "WindowsPowerShell" : Platform.ProductNameForDirectory; Platform.IsWindows ? "WindowsPowerShell" : Platform.SelectProductNameForDirectory("profile");
/// <summary> /// <summary>
/// The name of the subdirectory that contains packages. /// The name of the subdirectory that contains packages.
/// </summary> /// </summary>
internal static string ModuleDirectory = "Modules"; internal static string ModuleDirectory = "Modules";
/// <summary> /// <summary>
/// The partial path to the DSC module directory /// The partial path to the DSC module directory
/// </summary> /// </summary>

View file

@ -152,6 +152,7 @@ namespace System.Management.Automation
{ {
continue; continue;
} }
command = new PSCommand(); command = new PSCommand();
command.AddCommand(profilePath, false); command.AddCommand(profilePath, false);
commands.Add(command); commands.Add(command);
@ -186,7 +187,14 @@ namespace System.Management.Automation
{ {
basePath = Environment.GetFolderPath(Environment.SpecialFolder.Personal); basePath = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
basePath = IO.Path.Combine(basePath, Utils.ProductNameForDirectory); basePath = IO.Path.Combine(basePath, Utils.ProductNameForDirectory);
//If the profile path doesn't exist, create it.
if (!System.IO.Directory.Exists(basePath))
{
System.IO.Directory.CreateDirectory(basePath);
}
} }
else else
{ {
basePath = GetAllUsersFolderPath(shellId); basePath = GetAllUsersFolderPath(shellId);
@ -198,7 +206,6 @@ namespace System.Management.Automation
string profileName = useTestProfile ? "profile_test.ps1" : "profile.ps1"; string profileName = useTestProfile ? "profile_test.ps1" : "profile.ps1";
if (!string.IsNullOrEmpty(shellId)) if (!string.IsNullOrEmpty(shellId))
{ {
profileName = shellId + "_" + profileName; profileName = shellId + "_" + profileName;