Skip to content

Idea for ability to optionally override default modules and scripts install path - Duscussion and contribution is welcome! #1673

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

Draft
wants to merge 63 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
63 commits
Select commit Hold shift + click to select a range
ce4d559
Return path override if present
o-l-a-v Jul 13, 2024
166bf07
Make more robust
o-l-a-v Jul 13, 2024
37417de
Get empty/non-existent env variable is not expected to throw
o-l-a-v Jul 13, 2024
de08199
More robust
o-l-a-v Jul 13, 2024
acecb75
Expand, not resolve
o-l-a-v Jul 13, 2024
2713e4f
Add verbose message
o-l-a-v Jul 13, 2024
3556f80
Don't get paths twice
o-l-a-v Jul 13, 2024
f3abadd
Add newline before comments
o-l-a-v Jul 13, 2024
49db4bb
Remove empty newline
o-l-a-v Jul 14, 2024
802184e
Added new cmdlet
o-l-a-v Jul 14, 2024
a6352fe
"using System;" was written twice
o-l-a-v Jul 14, 2024
386bcf3
Only for Windows because env variables is very different on Unix + Ma…
o-l-a-v Jul 14, 2024
6ada4c6
Sort using alphabetically
o-l-a-v Jul 14, 2024
30bdf6a
Made code easier to navigate in VSCode
o-l-a-v Jul 14, 2024
3583604
Added ability to set the override env variable
o-l-a-v Jul 14, 2024
69863bb
Finished barebone functionality
o-l-a-v Jul 14, 2024
2d1ed6f
Simplified expression, thanks C# extension
o-l-a-v Jul 14, 2024
f451010
Treat -Path more like in Save-PSResource
o-l-a-v Jul 14, 2024
90ccdc9
Looks better this way IMO
o-l-a-v Jul 14, 2024
16339a1
Comment update
o-l-a-v Jul 14, 2024
398b2a6
Don't think ExpandEnvironmentVariables throws if some %VAR% does not …
o-l-a-v Jul 14, 2024
ec79901
Added -WhatIf support
o-l-a-v Jul 14, 2024
c80a398
Remove unneccessary "using"
o-l-a-v Jul 14, 2024
ba7b5f4
Fixed adding scripts dir to PATH
o-l-a-v Jul 14, 2024
ce7d0f7
PATH is named Path
o-l-a-v Jul 14, 2024
69c88ad
Big oops, set Path not PSModulePath for scripts dir
o-l-a-v Jul 14, 2024
8026053
Fixed verbose message
o-l-a-v Jul 14, 2024
853fb56
Simplify some more
o-l-a-v Jul 14, 2024
e5d145d
Return path override if present
o-l-a-v Jul 13, 2024
fb1d306
Make more robust
o-l-a-v Jul 13, 2024
7289dee
Get empty/non-existent env variable is not expected to throw
o-l-a-v Jul 13, 2024
76d75e2
More robust
o-l-a-v Jul 13, 2024
288edec
Expand, not resolve
o-l-a-v Jul 13, 2024
89f3dfc
Add verbose message
o-l-a-v Jul 13, 2024
9b4b60f
Don't get paths twice
o-l-a-v Jul 13, 2024
9e8617c
Add newline before comments
o-l-a-v Jul 13, 2024
02b79b7
Remove empty newline
o-l-a-v Jul 14, 2024
82e501c
Added new cmdlet
o-l-a-v Jul 14, 2024
4b02c09
"using System;" was written twice
o-l-a-v Jul 14, 2024
a86eae5
Only for Windows because env variables is very different on Unix + Ma…
o-l-a-v Jul 14, 2024
abb819e
Sort using alphabetically
o-l-a-v Jul 14, 2024
8ad86c5
Made code easier to navigate in VSCode
o-l-a-v Jul 14, 2024
92a72cf
Added ability to set the override env variable
o-l-a-v Jul 14, 2024
d067987
Finished barebone functionality
o-l-a-v Jul 14, 2024
8f2cc35
Simplified expression, thanks C# extension
o-l-a-v Jul 14, 2024
f278bb8
Treat -Path more like in Save-PSResource
o-l-a-v Jul 14, 2024
eabf85d
Looks better this way IMO
o-l-a-v Jul 14, 2024
8b1bdce
Comment update
o-l-a-v Jul 14, 2024
fc20175
Don't think ExpandEnvironmentVariables throws if some %VAR% does not …
o-l-a-v Jul 14, 2024
854e599
Added -WhatIf support
o-l-a-v Jul 14, 2024
2682b98
Remove unneccessary "using"
o-l-a-v Jul 14, 2024
e2e8d0b
Fixed adding scripts dir to PATH
o-l-a-v Jul 14, 2024
d51bdef
PATH is named Path
o-l-a-v Jul 14, 2024
3e243a4
Big oops, set Path not PSModulePath for scripts dir
o-l-a-v Jul 14, 2024
1b2bf32
Fixed verbose message
o-l-a-v Jul 14, 2024
be6d635
Simplify some more
o-l-a-v Jul 14, 2024
b0d7261
Merge branch 'install-path-override' of https://github.com/o-l-a-v/Po…
o-l-a-v Aug 21, 2024
c0f6dba
Merge branch 'master' into install-path-override
o-l-a-v Aug 21, 2024
9b84e89
Remove unneccessary using
o-l-a-v Aug 21, 2024
2f371a2
Merge branch 'master' into install-path-override
o-l-a-v Oct 23, 2024
71db23c
Format on save
o-l-a-v Oct 23, 2024
7cd3a1b
Removed unused "using"
o-l-a-v Oct 23, 2024
3b9ed21
Remove unused using and run formatting
o-l-a-v Oct 23, 2024
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
Prev Previous commit
Next Next commit
Only for Windows because env variables is very different on Unix + Ma…
…ke path to returning standard paths shorter
  • Loading branch information
o-l-a-v committed Jul 14, 2024
commit 386bcf3b25786a8a2ef241e85d0640b200fcc4dd
17 changes: 15 additions & 2 deletions src/code/SetPSResourceGetInstallPathOverride.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using Microsoft.PowerShell.PSResourceGet.UtilClasses;
using System;
using System.Management.Automation;
using System.Runtime.InteropServices;

namespace Microsoft.PowerShell.PSResourceGet.Cmdlets
{
Expand Down Expand Up @@ -36,13 +37,25 @@ public sealed class SetPSResourceGetInstallPathOverride : PSCmdlet

protected override void BeginProcessing()
{
// Only run on Windows for now, due to env variables on Unix being very different
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) {
ThrowTerminatingError(
new ErrorRecord(
new PSInvalidOperationException($"Error this only works on Windows for now'"),
"PathMissingExpectedSubdirectories",
ErrorCategory.InvalidOperation,
this
)
);
}

// Validate path is not null or empty
if (string.IsNullOrEmpty(Path)) {
ThrowTerminatingError(
new ErrorRecord(
new PSInvalidOperationException($"Error input path is null or empty: '{Path}'"),
"PathMissingExpectedSubdirectories",
ErrorCategory.InvalidOperation,
ErrorCategory.InvalidArgument,
this
)
);
Expand All @@ -54,7 +67,7 @@ protected override void BeginProcessing()
new ErrorRecord(
new PSInvalidOperationException($"Error input path is not rooted / absolute: '{Path}'"),
"PathMissingExpectedSubdirectories",
ErrorCategory.InvalidOperation,
ErrorCategory.InvalidArgument,
this
)
);
Expand Down
78 changes: 51 additions & 27 deletions src/code/Utils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -990,60 +990,84 @@ public static string GetInstalledPackageName(string pkgPath)
return new DirectoryInfo(pkgPath).Parent.Name;
}

// Find all potential resource paths
public static List<string> GetPathsFromEnvVarAndScope(
// Get standard install paths given scope
public static List<string> GetStandardPathsForScope(
PSCmdlet psCmdlet,
ScopeType? scope)
{
// Assets
List<string> resourcePaths = new();

// Get standard paths
GetStandardPlatformPaths(
psCmdlet,
out string myDocumentsPath,
out string programFilesPath
);

// Add paths to output
if (scope.Value is ScopeType.AllUsers)
{
resourcePaths.Add(Path.Combine(programFilesPath, "Modules"));
resourcePaths.Add(Path.Combine(programFilesPath, "Scripts"));
}
else
{
resourcePaths.Add(Path.Combine(myDocumentsPath, "Modules"));
resourcePaths.Add(Path.Combine(myDocumentsPath, "Scripts"));
}

// Return results
return resourcePaths;
}

// Find all potential resource paths
public static List<string> GetPathsFromEnvVarAndScope(
PSCmdlet psCmdlet,
ScopeType? scope)
{
// Path override is only implemented for Windows so far
if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) {
return GetStandardPathsForScope(psCmdlet,scope);
}

// Get value of path override environment variable
string pathOverride = Environment.GetEnvironmentVariable(
"PSResourceGetInstallPathOverride",
(scope.Value is ScopeType.AllUsers) ? EnvironmentVariableTarget.Machine : EnvironmentVariableTarget.User
);

// Try to expand path override
if (!string.IsNullOrEmpty(pathOverride)) {
// Return standard paths if no override was found, else try to expand paths found in override
if (string.IsNullOrEmpty(pathOverride)) {
return GetStandardPathsForScope(psCmdlet,scope);
}
else {
try
{
pathOverride = Environment.ExpandEnvironmentVariables(pathOverride);
}
catch (ArgumentException)
{
pathOverride = string.Empty;
psCmdlet.WriteVerbose("Path override was found, but could not expand environment variable(s).");
psCmdlet.WriteWarning("Path override was found, but could not expand environment variable(s). Falling back to standard paths.");
return GetStandardPathsForScope(psCmdlet,scope);
}
}

// Return override if present, else use default paths
if (!string.IsNullOrEmpty(pathOverride) && Path.IsPathRooted(pathOverride) && Directory.Exists(pathOverride))
{
psCmdlet.WriteVerbose(string.Format("Path override was found, using '{0}' as base.",pathOverride));
resourcePaths.Add(Path.Combine(pathOverride, "Modules"));
resourcePaths.Add(Path.Combine(pathOverride, "Scripts"));
psCmdlet.WriteVerbose(string.Format("Path override was found, using '{0}' as base.", pathOverride));
// Assets
List<string> resourcePaths = new()
{
Path.Combine(pathOverride, "Modules"),
Path.Combine(pathOverride, "Scripts")
};
return resourcePaths;
}
else {
GetStandardPlatformPaths(
psCmdlet,
out string myDocumentsPath,
out string programFilesPath
);
if (scope.Value is ScopeType.AllUsers)
{
resourcePaths.Add(Path.Combine(programFilesPath, "Modules"));
resourcePaths.Add(Path.Combine(programFilesPath, "Scripts"));
}
else
{
resourcePaths.Add(Path.Combine(myDocumentsPath, "Modules"));
resourcePaths.Add(Path.Combine(myDocumentsPath, "Scripts"));
}
return GetStandardPathsForScope(psCmdlet,scope);
}

// Return results
return resourcePaths;
}

public static List<string> GetAllResourcePaths(
Expand Down