|
16 | 16 | using Microsoft.PowerShell.Commands;
|
17 | 17 | using Microsoft.PowerShell.PSResourceGet.Cmdlets;
|
18 | 18 | using System.Net.Http;
|
| 19 | +using System.Globalization; |
19 | 20 |
|
20 | 21 | namespace Microsoft.PowerShell.PSResourceGet.UtilClasses
|
21 | 22 | {
|
@@ -83,6 +84,12 @@ function ConvertToHash
|
83 | 84 |
|
84 | 85 | #endregion
|
85 | 86 |
|
| 87 | + #region Path fields |
| 88 | + |
| 89 | + private static string s_tempHome = null; |
| 90 | + |
| 91 | + #endregion |
| 92 | + |
86 | 93 | #region String methods
|
87 | 94 |
|
88 | 95 | public static string TrimQuotes(string name)
|
@@ -976,23 +983,53 @@ public static List<string> GetAllInstallationPaths(
|
976 | 983 | return installationPaths;
|
977 | 984 | }
|
978 | 985 |
|
| 986 | + private static string GetHomeOrCreateTempHome() |
| 987 | + { |
| 988 | + const string tempHomeFolderName = "psresourceget-{0}-98288ff9-5712-4a14-9a11-23693b9cd91a"; |
| 989 | + |
| 990 | + string envHome = Environment.GetEnvironmentVariable("HOME") ?? s_tempHome; |
| 991 | + if (envHome is not null) |
| 992 | + { |
| 993 | + return envHome; |
| 994 | + } |
| 995 | + |
| 996 | + try |
| 997 | + { |
| 998 | + var s_tempHome = Path.Combine(Path.GetTempPath(), string.Format(CultureInfo.CurrentCulture, tempHomeFolderName, Environment.UserName)); |
| 999 | + Directory.CreateDirectory(s_tempHome); |
| 1000 | + } |
| 1001 | + catch (UnauthorizedAccessException) |
| 1002 | + { |
| 1003 | + // Directory creation may fail if the account doesn't have filesystem permission such as some service accounts. |
| 1004 | + // Return an empty string in this case so the process working directory will be used. |
| 1005 | + s_tempHome = string.Empty; |
| 1006 | + } |
| 1007 | + |
| 1008 | + return s_tempHome; |
| 1009 | + } |
| 1010 | + |
979 | 1011 | private readonly static Version PSVersion6 = new Version(6, 0);
|
980 | 1012 | private static void GetStandardPlatformPaths(
|
981 | 1013 | PSCmdlet psCmdlet,
|
982 |
| - out string myDocumentsPath, |
983 |
| - out string programFilesPath) |
| 1014 | + out string localUserDir, |
| 1015 | + out string allUsersDir) |
984 | 1016 | {
|
985 | 1017 | if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
|
986 | 1018 | {
|
987 | 1019 | string powerShellType = (psCmdlet.Host.Version >= PSVersion6) ? "PowerShell" : "WindowsPowerShell";
|
988 |
| - myDocumentsPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), powerShellType); |
989 |
| - programFilesPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles), powerShellType); |
| 1020 | + localUserDir = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), powerShellType); |
| 1021 | + allUsersDir = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles), powerShellType); |
990 | 1022 | }
|
991 | 1023 | else
|
992 | 1024 | {
|
993 | 1025 | // paths are the same for both Linux and macOS
|
994 |
| - myDocumentsPath = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "powershell"); |
995 |
| - programFilesPath = System.IO.Path.Combine("/usr", "local", "share", "powershell"); |
| 1026 | + localUserDir = Path.Combine(GetHomeOrCreateTempHome(), ".local", "share", "powershell"); |
| 1027 | + // Create the default data directory if it doesn't exist. |
| 1028 | + if (!Directory.Exists(localUserDir)) { |
| 1029 | + Directory.CreateDirectory(localUserDir); |
| 1030 | + } |
| 1031 | + |
| 1032 | + allUsersDir = System.IO.Path.Combine("/usr", "local", "share", "powershell"); |
996 | 1033 | }
|
997 | 1034 | }
|
998 | 1035 |
|
|
0 commit comments