-
Notifications
You must be signed in to change notification settings - Fork 102
Add PSContentPath Standard Platform Paths #1912
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
jshigetomi
wants to merge
6
commits into
PowerShell:master
Choose a base branch
from
jshigetomi:PSContentPath
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
5f00798
Add experimental feature check and add PSContentPath to standard plat…
jshigetomi 7d03376
Use Json.net to parse instead
jshigetomi bbb7148
Add support in Linux, MacOS and refactor
jshigetomi d53dd08
Use PowerShell GetPSModulePath API instead
jshigetomi a454cb3
Use runspace instead of reflection
jshigetomi 94a1563
Use current runspace and only get PSVersion once
jshigetomi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -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; | ||||||
|
|
||||||
|
|
@@ -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) | ||||||
|
|
@@ -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); | ||||||
|
|
||||||
| /// <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) | ||||||
| { | ||||||
| // 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 | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. do we need to write a verbose message or warning? |
||||||
| } | ||||||
|
|
||||||
| 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"); | ||||||
anamnavi marked this conversation as resolved.
Show resolved
Hide resolved
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| } | ||||||
| } | ||||||
|
|
||||||
|
|
||||||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.