Skip to content
92 changes: 84 additions & 8 deletions src/code/Utils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
using Azure.Identity;
using System.Text.RegularExpressions;
using System.Threading;
using System.Text.Json;
using System.Threading.Tasks;
using System.Xml;

Expand Down Expand Up @@ -1049,14 +1050,14 @@ public static List<string> GetPathsFromEnvVarAndScope(
{
GetStandardPlatformPaths(
psCmdlet,
out string myDocumentsPath,
out string psUserContentPath,
out string programFilesPath);

List<string> resourcePaths = new List<string>();
if (scope is null || scope.Value is ScopeType.CurrentUser)
{
resourcePaths.Add(Path.Combine(myDocumentsPath, "Modules"));
resourcePaths.Add(Path.Combine(myDocumentsPath, "Scripts"));
resourcePaths.Add(Path.Combine(psUserContentPath, "Modules"));
resourcePaths.Add(Path.Combine(psUserContentPath, "Scripts"));
}

if (scope.Value is ScopeType.AllUsers)
Expand Down Expand Up @@ -1156,28 +1157,103 @@ private static string GetHomeOrCreateTempHome()
}

private readonly static Version PSVersion6 = new Version(6, 0);
private readonly static Version PSVersion7_7 = new Version(7, 7);
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

As mentioned in the PR in the PowerShell repo I & the community really do not want this dragging out until 7.7.0

We can get this in much sooner than that as I mentioned in that PR @jshigetomi

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.

@kilasuit This feature is coming out in 7.7.0-preivew.1 which shouldn't be too far away from release.


/// <summary>
/// Gets the user content directory path using PowerShell's Get-PSContentPath cmdlet.
/// Falls back to legacy path if the cmdlet is not available or PowerShell version is below 7.7.0.
/// </summary>
private static string GetUserContentPath(PSCmdlet psCmdlet, Version psVersion, string legacyPath)
{

// Only use Get-PSContentPath cmdlet if PowerShell version is 7.7.0 or greater (when PSContentPath feature is available)
if (psVersion >= PSVersion7_7)
{
// Try to use PowerShell's Get-PSContentPath cmdlet in the current runspace
// This cmdlet is only available if experimental feature PSContentPath is enabled
try
{
var results = psCmdlet.InvokeCommand.InvokeScript("Get-PSContentPath");

if (results != null && results.Count > 0)
{
Comment thread
alerickson marked this conversation as resolved.
// Get-PSContentPath returns a PSObject, extract the path string
string userContentPath = results[0]?.ToString();
if (!string.IsNullOrEmpty(userContentPath))
{
psCmdlet.WriteVerbose($"User content path from Get-PSContentPath: {userContentPath}");
return userContentPath;
}
}
}
catch (Exception ex)
{
psCmdlet.WriteVerbose($"Get-PSContentPath cmdlet not available: {ex.Message}");
}
}
else
{
psCmdlet.WriteVerbose($"PowerShell version {psVersion} is below 7.7.0, using legacy location");
}

// Fallback to legacy location
psCmdlet.WriteVerbose($"Using legacy location: {legacyPath}");
return legacyPath;
}

private static void GetStandardPlatformPaths(
PSCmdlet psCmdlet,
out string localUserDir,
out string allUsersDir)
{
// Get PowerShell engine version from $PSVersionTable.PSVersion
Version psVersion = new Version(5, 1);
try
{
dynamic psVersionObj = (psCmdlet.SessionState.PSVariable.GetValue("PSVersionTable") as Hashtable)?["PSVersion"];
if (psVersionObj != null) psVersion = new Version((int)psVersionObj.Major, (int)psVersionObj.Minor);
}
catch {
// Fallback if dynamic access fails
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.

do we need to write a verbose message or warning?

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.

If $PSVersionTable doesn't work, what could the user do differently after knowing that this is where it breaks?
I'm not sure there is much they can do, but could be helpful knowing where it went wrong.
I will add a warning for now.

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.

Could we add a debug message here, for dev debugging purposes?

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Is there any cases where this variable won't be available?
I don't think so with it being an Automatic Variable - $PSVersionTable Automatic Variable Documentation

}

if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
string powerShellType = (psCmdlet.Host.Version >= PSVersion6) ? "PowerShell" : "WindowsPowerShell";
localUserDir = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), powerShellType);
string powerShellType = (psVersion >= PSVersion6) ? "PowerShell" : "WindowsPowerShell";

// Windows PowerShell doesn't support experimental features or PSContentPath
if (powerShellType == "WindowsPowerShell")
{
// Use legacy Documents folder for Windows PowerShell
localUserDir = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), powerShellType);
psCmdlet.WriteVerbose($"Using Windows PowerShell Documents folder: {localUserDir}");
}
else
{
string legacyPath = Path.Combine(
Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments),
powerShellType
);

localUserDir = GetUserContentPath(psCmdlet, psVersion, legacyPath);
}

allUsersDir = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles), powerShellType);
}
else
{
// paths are the same for both Linux and macOS
localUserDir = Path.Combine(GetHomeOrCreateTempHome(), ".local", "share", "powershell");
// Create the default data directory if it doesn't exist.
string legacyPath = Path.Combine(GetHomeOrCreateTempHome(), ".local", "share", "powershell");

localUserDir = GetUserContentPath(psCmdlet, psVersion, legacyPath);

// 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");
allUsersDir = Path.Combine("/usr", "local", "share", "powershell");
Comment thread
anamnavi marked this conversation as resolved.
Outdated
Comment thread
jshigetomi marked this conversation as resolved.
Outdated
}
}

Expand Down
Loading