Skip to content
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
15 changes: 8 additions & 7 deletions src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHost.cs
Original file line number Diff line number Diff line change
Expand Up @@ -185,15 +185,17 @@ internal static int Start(
{
profileDir = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) +
@"\Microsoft\Windows\PowerShell";
} else
{
profileDir = System.IO.Path.Combine(Environment.GetEnvironmentVariable("HOME"), ".powershell");
}

if (!Directory.Exists(profileDir))
if (!Directory.Exists(profileDir))
{
Directory.CreateDirectory(profileDir);
}
}
else
{
Directory.CreateDirectory(profileDir);
profileDir = Platform.SelectProductNameForDirectory(Platform.XDG_Type.CACHE);
}

ClrFacade.SetProfileOptimizationRoot(profileDir);
}
catch
Expand Down Expand Up @@ -268,7 +270,6 @@ internal static int Start(
: "StartupProfileData-NonInteractive");
exitCode = theConsoleHost.Run(cpp, !string.IsNullOrEmpty(preStartWarning));
}

}
finally
{
Expand Down
33 changes: 24 additions & 9 deletions src/Microsoft.PowerShell.PSReadLine/Cmdlets.cs
Original file line number Diff line number Diff line change
Expand Up @@ -169,24 +169,39 @@ public PSConsoleReadlineOptions(string hostName)
WordDelimiters = DefaultWordDelimiters;
HistorySearchCaseSensitive = DefaultHistorySearchCaseSensitive;
HistorySaveStyle = DefaultHistorySaveStyle;

string historyFileName = hostName + "_history.txt";
#if CORECLR
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) // MS Windows
{
HistorySavePath = System.IO.Path.Combine(Environment.ExpandEnvironmentVariables("%AppData%"),
@"\Microsoft\Windows\PowerShell\PSReadline\",
hostName + "_history.txt");
HistorySavePath = System.IO.Path.Combine(Environment.GetEnvironmentVariable("APPDATA"),
@"Microsoft\Windows\PowerShell\PSReadline\",
historyFileName);
}
else
{
HistorySavePath = System.IO.Path.Combine(
Environment.GetEnvironmentVariable("HOME"),
".powershell",
"PSReadLine",
hostName + "_history.txt");
// PSReadline does not have access to Utils.CorePSPlatform. Must set PSReadline path separately
string historyPath = System.Environment.GetEnvironmentVariable("XDG_DATA_HOME");

if (!String.IsNullOrEmpty(historyPath))
{
historyPath = System.IO.Path.Combine(historyPath, "powershell", "PSReadLine", historyFileName);
HistorySavePath = historyPath;
}
else
{
// History is data, so it goes into .local/share/powershell folder
HistorySavePath = System.IO.Path.Combine(Environment.GetEnvironmentVariable("HOME"),
".local",
"share",
"powershell",
"PSReadLine",
historyFileName);
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. You have $HOME twice in your path.
  2. Do we really need to go this deep to find a place for the history file?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. Fixed
  2. This is being changed to match the xdg spec on locations

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't get why history file goes in the "modules" directory.

}
#else
HistorySavePath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)
+ @"\Microsoft\Windows\PowerShell\PSReadline\" + hostName + "_history.txt";
+ @"\Microsoft\Windows\PowerShell\PSReadline\" + historyFileName;
#endif
CommandValidationHandler = null;
CommandsToValidateScriptBlockArguments = new HashSet<string>(StringComparer.OrdinalIgnoreCase)
Expand Down
111 changes: 103 additions & 8 deletions src/System.Management.Automation/CoreCLR/CorePsPlatform.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,15 @@ public static bool IsLinux
}
}

//enum for selecting the xdgpaths
public enum XDG_Type
{
PROFILE,
MODULES,
CACHE,
DEFAULT
}

public static bool IsOSX
{
get
Expand Down Expand Up @@ -96,8 +105,94 @@ public static bool IsCore
"WSMan.format.ps1xml"
};

// directory location of PowerShell for profile loading
public static string ProductNameForDirectory = ".powershell";
// function for choosing directory location of PowerShell for profile loading
public static string SelectProductNameForDirectory (Platform.XDG_Type dirpath)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Traditionally, we've named Linux functions in this file in the format "NonWindows...()". You may want to consider doing so, and add check to make sure this is not called from Windows code.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Take a look at LinuxPlatform.GetFolderPath() in this same file. Can you somehow integrate what you have with that?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need to get away from NonWindows, this is just fine.

GetFolderPath() is completely different from this.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, I'll change it back again.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Whether NonWindows is good or not, there's something to be said for consistency.

GetFolderPath() is slightly different, but kind of related. Should default value for XDG_DATA_HOME be same as SpecialPath.LocalApplicationData?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

George Fleming (@palladia) this functionality is not splitting an existing function, where we would use Windows and NonWindows, it's adding additional functionality.

The SpecialPath.LocalApplicationData should not be depended on; that code is a workaround that should be slated for removal once all uses of it are fixed.

{

//TODO: XDG_DATA_DIRS implementation as per GitHub issue #1060

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Based on our conversation, changing the logic based on the input value will be efficient, easy to read and maintain for the future.

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 xdgConfigHomeDefault = Path.Combine ( System.Environment.GetEnvironmentVariable("HOME"), ".config", "powershell");
string xdgModuleDefault = Path.Combine ( System.Environment.GetEnvironmentVariable("HOME"), ".local", "share", "powershell", "Modules");
string xdgCacheDefault = Path.Combine (System.Environment.GetEnvironmentVariable("HOME"), ".cache", "powershell");

switch (dirpath){
case Platform.XDG_Type.PROFILE:
//the user has set XDG_CONFIG_HOME corrresponding to profile path
if (String.IsNullOrEmpty(xdgconfighome))
{
//xdg values have not been set
return xdgConfigHomeDefault;
}

else
{
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)){

//xdg values have not been set
if (!Directory.Exists(xdgModuleDefault)) //module folder not always guaranteed to exist
{
Directory.CreateDirectory(xdgModuleDefault);
}
return xdgModuleDefault;
}
else
{
return Path.Combine(xdgdatahome, "powershell", "Modules");
}

case Platform.XDG_Type.CACHE:
//the user has set XDG_CACHE_HOME
if (String.IsNullOrEmpty(xdgcachehome))
{
//xdg values have not been set
if (!Directory.Exists(xdgCacheDefault)) //module folder not always guaranteed to exist
{
Directory.CreateDirectory(xdgCacheDefault);
}

return xdgCacheDefault;
}

else
{
if (!Directory.Exists(Path.Combine(xdgcachehome, "powershell")))
{
Directory.CreateDirectory(Path.Combine(xdgcachehome, "powershell"));
}

return Path.Combine(xdgcachehome, "powershell");
}

case Platform.XDG_Type.DEFAULT:
//default for profile location
return xdgConfigHomeDefault;

default:
//xdgConfigHomeDefault needs to be created in the edge case that we do not have the folder or it was deleted
//This folder is the default in the event of all other failures for data storage
if (!Directory.Exists(xdgConfigHomeDefault))
{
try {
Directory.CreateDirectory(xdgConfigHomeDefault);
}
catch{

Console.Error.WriteLine("Failed to create default data directory: " + xdgConfigHomeDefault);
}
}

return xdgConfigHomeDefault;
}

}

// ComObjectType is null on CoreCLR for Linux since there is
// no COM support on Linux
Expand Down Expand Up @@ -363,7 +458,7 @@ internal static string NonWindowsGetHostName()
throw new InvalidOperationException("LinuxPlatform.NonWindowsHostName error: " + lastError);
}
return hostName;

}
else
{
Expand All @@ -386,7 +481,7 @@ internal static uint NonWindowsGetThreadId()
// TODO:PSL clean this up
return 0;
}

/// <summary>
/// This exception is meant to be thrown if a code path is not supported due
/// to platform restrictions
Expand Down Expand Up @@ -570,7 +665,7 @@ public static bool IsSymLink(FileSystemInfo fs)
int ret = Native.IsSymLink(filePath);
switch(ret)
{
case 1:
case 1:
return true;
case 0:
return false;
Expand All @@ -590,7 +685,7 @@ public static bool IsExecutable(string filePath)
int ret = Native.IsExecutable(filePath);
switch(ret)
{
case 1:
case 1:
return true;
case 0:
return false;
Expand Down Expand Up @@ -685,11 +780,11 @@ internal static class Native
internal static extern int SetDate(SetDateInfoInternal info);

[DllImport(psLib, CharSet = CharSet.Ansi, SetLastError = true)]
internal static extern int CreateSymLink([MarshalAs(UnmanagedType.LPStr)]string filePath,
internal static extern int CreateSymLink([MarshalAs(UnmanagedType.LPStr)]string filePath,
[MarshalAs(UnmanagedType.LPStr)]string target);

[DllImport(psLib, CharSet = CharSet.Ansi, SetLastError = true)]
internal static extern int CreateHardLink([MarshalAs(UnmanagedType.LPStr)]string filePath,
internal static extern int CreateHardLink([MarshalAs(UnmanagedType.LPStr)]string filePath,
[MarshalAs(UnmanagedType.LPStr)]string target);

[DllImport(psLib, CharSet = CharSet.Ansi, SetLastError = true)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1015,8 +1015,10 @@ static AnalysisCacheData()
{
cacheStoreLocation =
Environment.GetEnvironmentVariable("PSModuleAnalysisCachePath") ??
Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData),
@"Microsoft\Windows\PowerShell\ModuleAnalysisCache");
(Platform.IsWindows
? Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData),
@"Microsoft\Windows\PowerShell\ModuleAnalysisCache")
: Path.Combine(Platform.SelectProductNameForDirectory(Platform.XDG_Type.CACHE), "ModuleAnalysisCache"));
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These changes cannot be tested automatically, but I've manually verified it worked.

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -548,13 +548,20 @@ internal static string GetModuleName(string path)
/// <returns>personal module path</returns>
internal static string GetPersonalModulePath()
{
string personalModuleRoot = Path.Combine(
Path.Combine(
Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments),
Utils.ProductNameForDirectory),
Utils.ModuleDirectory);

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>
Expand Down
2 changes: 1 addition & 1 deletion src/System.Management.Automation/engine/Utils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -606,7 +606,7 @@ internal static bool IsNetFrameworkVersionSupported(Version checkVersion, out bo
/// Profile uses this to control profile loading.
/// </remarks>
internal static string ProductNameForDirectory =
Platform.IsWindows ? "WindowsPowerShell" : Platform.ProductNameForDirectory;
Platform.IsWindows ? "WindowsPowerShell" : Platform.SelectProductNameForDirectory(Platform.XDG_Type.PROFILE);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If this is receiving an XDGBDS path, that path is absolute, which is inconsistent with the Windows behavior. Something needs to be changed here.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So I can change this, but Path.Combine has been why this has been okay. Path.Combine has some pretty flexible behaviors around this:
path1 should be an absolute path (for example, "d:\archives" or "\archives\public"). If path2 or path3 is also an absolute path, the combine operation discards all previously combined paths and resets to that absolute path.
https://msdn.microsoft.com/en-us/library/dd784047(v=vs.110).aspx

I could see not wanting to rely on this behaivor, but it is pretty useful for our needs in this.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, that's awesome. I didn't know that.


/// <summary>
/// The name of the subdirectory that contains packages.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,6 @@ internal static string GetFullProfileFileName(string shellId, bool forCurrentUse

string profileName = useTestProfile ? "profile_test.ps1" : "profile.ps1";


if (!string.IsNullOrEmpty(shellId))
{
profileName = shellId + "_" + profileName;
Expand Down
Loading