Further code review changes

This commit is contained in:
v-alexjo
2016-06-09 09:50:36 -07:00
committed by Andrew Schwartzmeyer
parent 4c1f9f9c79
commit c2527199a6
5 changed files with 55 additions and 45 deletions
@@ -192,13 +192,13 @@ namespace Microsoft.PowerShell
//check if the user has set an XDG path in their environment variables
profileDir = Platform.SelectProductNameForDirectory(Platform.XDG_Type.PROFILE);
}
/*
if (!Directory.Exists(profileDir)) //xdg value may have been set but not a valid directory
{
Console.WriteLine("The selected directory (" + profileDir +") for the profile does not exist. Using the default path.");
profileDir = Platform.SelectProductNameForDirectory(Platform.XDG_Type.DEFAULT);
}
*/
ClrFacade.SetProfileOptimizationRoot(profileDir);
}
catch
@@ -180,23 +180,23 @@ namespace Microsoft.PowerShell
else
{
//PSReadline does not have access to Utils.CorePSPlatform. Must set PSReadline path seperately
string modulepath = System.Environment.GetEnvironmentVariable("XDG_CACHE_HOME");
string historypath = System.Environment.GetEnvironmentVariable("XDG_DATA_HOME");
if (!String.IsNullOrEmpty(modulepath))
if (!String.IsNullOrEmpty(historypath))
{
modulepath = System.IO.Path.Combine(modulepath, "powershell");
HistorySavePath = modulepath;
historypath = System.IO.Path.Combine(historypath, "powershell");
HistorySavePath = historypath;
}
else
{
modulepath = System.IO.Path.Combine(
historypath = System.IO.Path.Combine(
Environment.GetEnvironmentVariable("HOME"),
".config/powershell/modules");
".local/share/powershell/modules");
HistorySavePath = System.IO.Path.Combine(
Environment.GetEnvironmentVariable("HOME"),
modulepath,
historypath,
"PSReadLine",
hostName + "_history.txt");
}
@@ -109,66 +109,66 @@ namespace System.Management.Automation
public static string SelectProductNameForDirectory (Platform.XDG_Type dirpath)
{
//TODO: XDG_DATA_DIRS implementation as per GitHub issue #1060
string xdgconfighome = System.Environment.GetEnvironmentVariable("XDG_CONFIG_HOME");
string xdgdatahome = System.Environment.GetEnvironmentVariable("XDG_DATA_HOME");
string xdgcachehome = System.Environment.GetEnvironmentVariable("XDG_CACHE_HOME");
string profileDefault = Path.Combine ( System.Environment.GetEnvironmentVariable("HOME"), ".config/powershell");
string moduleDefault = Path.Combine ( System.Environment.GetEnvironmentVariable("HOME"), ".config/powershell/modules");
string xdgConfigHomeDefault = Path.Combine ( System.Environment.GetEnvironmentVariable("HOME"), ".config/powershell");
string moduleDefault = Path.Combine ( System.Environment.GetEnvironmentVariable("HOME"), ".local/share/powershell/modules");
switch (dirpath){
case Platform.XDG_Type.PROFILE:
//the user has set XDG_CONFIG_HOME corrresponding to profile path
if (!String.IsNullOrEmpty(xdgconfighome))
if (String.IsNullOrEmpty(xdgconfighome))
{
xdgconfighome = Path.Combine(xdgconfighome, "powershell");
return xdgconfighome;
//xdg values have not been set
return xdgConfigHomeDefault;
}
else
{
//xdg values have not been set
return profileDefault;
return Path.Combine(xdgconfighome, "powershell");
}
case Platform.XDG_Type.MODULES:
//the user has set XDG_DATA_HOME corresponding to module path
if (!String.IsNullOrEmpty(xdgdatahome)){
xdgdatahome = Path.Combine(xdgdatahome, "powershell");
return xdgdatahome;
}
else
{
if (String.IsNullOrEmpty(xdgdatahome)){
//xdg values have not been set
if (!Directory.Exists(profileDefault)) //module folder not always guaranteed to exist
if (!Directory.Exists(moduleDefault)) //module folder not always guaranteed to exist
{
Directory.CreateDirectory(moduleDefault);
}
return profileDefault;
return xdgConfigHomeDefault;
}
else
{
return Path.Combine(xdgdatahome, "powershell");
}
case Platform.XDG_Type.HISTORY:
//the user has set XDG_CACHE_HOME
if (!String.IsNullOrEmpty(xdgcachehome)){
xdgcachehome = Path.Combine(xdgcachehome, "powershell");
return xdgcachehome;
if (String.IsNullOrEmpty(xdgcachehome)){
return xdgConfigHomeDefault;
}
else
{
return profileDefault;
return Path.Combine(xdgcachehome, "powershell");
}
case Platform.XDG_Type.DEFAULT:
//default for profile location
return profileDefault;
return xdgConfigHomeDefault;
default:
if (!Directory.Exists(profileDefault))
if (!Directory.Exists(xdgConfigHomeDefault))
{
Directory.CreateDirectory(profileDefault);
Directory.CreateDirectory(xdgConfigHomeDefault);
}
return profileDefault;
return xdgConfigHomeDefault;
}
}
@@ -548,8 +548,20 @@ namespace System.Management.Automation
/// <returns>personal module path</returns>
internal static string GetPersonalModulePath()
{
string personalModuleRoot = Platform.SelectProductNameForDirectory(Platform.XDG_Type.MODULES);
return personalModuleRoot;
if (Platform.IsWindows)
{
string personalModuleRoot = Path.Combine(
Path.Combine(
Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments),
Utils.ProductNameForDirectory),
Utils.ModuleDirectory);
return personalModuleRoot;
}
else
{
string personalModuleRoot = Platform.SelectProductNameForDirectory(Platform.XDG_Type.MODULES);
return personalModuleRoot;
}
}
/// <summary>
+8 -10
View File
@@ -26,9 +26,9 @@ Describe "XDG Base Directory Specification" {
}
It "Should start with the default profile" -Skip:$IsWindows {
$expected = [IO.Path]::Combine($env:HOME, ".config/powershell", $profileName)
$expected = [IO.Path]::Combine($env:HOME, "Documents", "WindowsPowerShell", $PROFILE)
# Escape variable with backtick so new process interpolates it
& $powershell -noprofile `$PROFILE | Should Be $expected
& $powershell -noprofile `$PROFILE | Should Be $expected
}
It "Should respect XDG_CONFIG_HOME" -Skip:$IsWindows {
@@ -41,11 +41,11 @@ Describe "XDG Base Directory Specification" {
Context "Modules" {
It "Should start with the default module path" -Skip:$IsWindows {
$expected = [IO.Path]::Combine($env:HOME, ".config", "powershell")
It "Should respect XDG_DATA_HOME" -Skip:$IsWindows {
$env:XDG_DATA_HOME = [IO.Path]::Combine($env:HOME, ".local", "share", "powershell", "Modules")
$modulepath = & $powershell -noprofile `$env:PSMODULEPATH
$modulepath = $modulepath.split(';')[0]
$modulepath | Should Be $expected
$modulepath | Should Be $env:XDG_DATA_HOME
}
It "Should respect XDG_CACHE_HOME" -Skip:$IsWindows {
@@ -54,11 +54,9 @@ Describe "XDG Base Directory Specification" {
& $powershell -noprofile `$env:XDG_CACHE_HOME | Should Be $expected
}
It "Should respect XDG_DATA_HOME" -Skip:$IsWindows {
$env:XDG_DATA_HOME = [IO.Path]::Combine($pwd, "test", "path")
$datahomeDir = [IO.Path]::Combine($env:XDG_DATA_HOME, "ConsoleHost_history.txt")
$expected = [IO.Path]::Combine($HOME, "Powershell", "test", "path", "ConsoleHost_history.txt")
$datahomeDir | Should Be $expected
It "Should respect PSReadLine history" -Skip:$IsWindows {
$env:XDG_DATA_HOME = [IO.Path]::Combine($env:HOME, ".local", "share", "powershell", "PSReadLine", "ConsoleHost_history.txt")
"a" | Should Be $expected
}
}
}