Skip to content

Commit

Permalink
Update unix local user paths (#1464)
Browse files Browse the repository at this point in the history
  • Loading branch information
alerickson authored Oct 27, 2023
1 parent 72e4675 commit ece1872
Showing 1 changed file with 43 additions and 6 deletions.
49 changes: 43 additions & 6 deletions src/code/Utils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
using Microsoft.PowerShell.Commands;
using Microsoft.PowerShell.PSResourceGet.Cmdlets;
using System.Net.Http;
using System.Globalization;

namespace Microsoft.PowerShell.PSResourceGet.UtilClasses
{
Expand Down Expand Up @@ -83,6 +84,12 @@ function ConvertToHash

#endregion

#region Path fields

private static string s_tempHome = null;

#endregion

#region String methods

public static string TrimQuotes(string name)
Expand Down Expand Up @@ -976,23 +983,53 @@ public static List<string> GetAllInstallationPaths(
return installationPaths;
}

private static string GetHomeOrCreateTempHome()
{
const string tempHomeFolderName = "psresourceget-{0}-98288ff9-5712-4a14-9a11-23693b9cd91a";

string envHome = Environment.GetEnvironmentVariable("HOME") ?? s_tempHome;
if (envHome is not null)
{
return envHome;
}

try
{
var s_tempHome = Path.Combine(Path.GetTempPath(), string.Format(CultureInfo.CurrentCulture, tempHomeFolderName, Environment.UserName));
Directory.CreateDirectory(s_tempHome);
}
catch (UnauthorizedAccessException)
{
// Directory creation may fail if the account doesn't have filesystem permission such as some service accounts.
// Return an empty string in this case so the process working directory will be used.
s_tempHome = string.Empty;
}

return s_tempHome;
}

private readonly static Version PSVersion6 = new Version(6, 0);
private static void GetStandardPlatformPaths(
PSCmdlet psCmdlet,
out string myDocumentsPath,
out string programFilesPath)
out string localUserDir,
out string allUsersDir)
{
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
string powerShellType = (psCmdlet.Host.Version >= PSVersion6) ? "PowerShell" : "WindowsPowerShell";
myDocumentsPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), powerShellType);
programFilesPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles), powerShellType);
localUserDir = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), powerShellType);
allUsersDir = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles), powerShellType);
}
else
{
// paths are the same for both Linux and macOS
myDocumentsPath = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "powershell");
programFilesPath = System.IO.Path.Combine("/usr", "local", "share", "powershell");
localUserDir = Path.Combine(GetHomeOrCreateTempHome(), ".local", "share", "powershell");
// Create the default data directory if it doesn't exist.
if (!Directory.Exists(localUserDir)) {
Directory.CreateDirectory(localUserDir);
}

allUsersDir = System.IO.Path.Combine("/usr", "local", "share", "powershell");
}
}

Expand Down

0 comments on commit ece1872

Please sign in to comment.