Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
64 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
05357b1
Merge branch 'master' into install-path-override
o-l-a-v May 16, 2026
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
4 changes: 2 additions & 2 deletions src/Microsoft.PowerShell.PSResourceGet.psd1
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@
FunctionsToExport = @(
'Import-PSGetRepository'
)
VariablesToExport = 'PSGetPath'
AliasesToExport = @(
VariablesToExport = 'PSGetPath'
AliasesToExport = @(
'Get-PSResource',
'fdres',
'gres',
Expand Down
3 changes: 1 addition & 2 deletions src/code/GetInstalledPSResource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,6 @@ protected override void BeginProcessing()
{
ThrowTerminatingError(new ErrorRecord(
new PSArgumentException($"Error: Could not resolve provided Path argument '{Path}' into a single path."),

"ErrorInvalidPathArgument",
ErrorCategory.InvalidArgument,
this));
Expand Down Expand Up @@ -121,7 +120,7 @@ protected override void BeginProcessing()

protected override void ProcessRecord()
{
var namesToSearch = Utils.ProcessNameWildcards(Name, removeWildcardEntries:false, out string[] errorMsgs, out bool _);
var namesToSearch = Utils.ProcessNameWildcards(Name, removeWildcardEntries: false, out string[] errorMsgs, out bool _);
foreach (string error in errorMsgs)
{
WriteError(new ErrorRecord(
Expand Down
9 changes: 6 additions & 3 deletions src/code/InstallPSResource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ private enum ResourceFileType

#endregion

#region Method Overrides
#region Method override - Begin

protected override void BeginProcessing()
{
Expand All @@ -266,7 +266,7 @@ protected override void BeginProcessing()
RepositorySettings.CheckRepositoryStore();

_pathsToInstallPkg = Utils.GetAllInstallationPaths(this, Scope);
List<string> pathsToSearch = Utils.GetAllResourcePaths(this, Scope);
List<string> pathsToSearch = _pathsToInstallPkg;
// Only need to find packages installed if -Reinstall is not passed in
_packagesOnMachine = Reinstall ? new HashSet<string>(StringComparer.CurrentCultureIgnoreCase) : Utils.GetInstalledPackages(pathsToSearch, this);

Expand All @@ -275,6 +275,9 @@ protected override void BeginProcessing()
_installHelper = new InstallHelper(cmdletPassedIn: this, networkCredential: networkCred);
}

#endregion

#region Method Override - Process
protected override void ProcessRecord()
{
switch (ParameterSetName)
Expand Down Expand Up @@ -359,7 +362,7 @@ protected override void ProcessRecord()
catch (Exception)
{
ThrowTerminatingError(new ErrorRecord(
new ArgumentException($"Argument for parameter -RequiredResourceFile is not in proper json or hashtable format. Make sure argument is either a valid .json or .psd1 file."),
new ArgumentException($"Argument for parameter -RequiredResourceFile is not in proper json or hashtable format. Make sure argument is either a valid .json or .psd1 file."),
"RequiredResourceFileNotInProperJsonFormat",
ErrorCategory.InvalidData,
this));
Expand Down
11 changes: 7 additions & 4 deletions src/code/ServerApiCall.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@
// Licensed under the MIT License.

using Microsoft.PowerShell.PSResourceGet.UtilClasses;
using NuGet.Versioning;
using System;
using System.IO;
using System.Net.Http;
using NuGet.Versioning;
using System.Management.Automation;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Runtime.ExceptionServices;
using System.Management.Automation;
Expand All @@ -33,7 +34,7 @@ public ServerApiCall(PSRepositoryInfo repository, NetworkCredential networkCrede
HttpClientHandler handler = new HttpClientHandler();
bool token = false;

if(networkCredential != null)
if (networkCredential != null)
{
token = String.Equals("token", networkCredential.UserName) ? true : false;
};
Expand All @@ -45,7 +46,9 @@ public ServerApiCall(PSRepositoryInfo repository, NetworkCredential networkCrede

_sessionClient = new HttpClient(handler);
_sessionClient.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Basic", Convert.ToBase64String(byteArray));
} else {
}
else
{

handler.Credentials = networkCredential;

Expand Down
239 changes: 239 additions & 0 deletions src/code/SetPSResourceGetInstallPathOverride.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,239 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using Microsoft.PowerShell.PSResourceGet.UtilClasses;
using System;
using System.Linq;
using System.Management.Automation;
using System.Runtime.InteropServices;

namespace Microsoft.PowerShell.PSResourceGet.Cmdlets
{
/// <summary>
/// The Set-PSResourceGetInstallPathOverride cmdlet is used to override install path for PS resources.
/// </summary>
[Cmdlet(VerbsCommon.Set, "PSResourceGetInstallPathOverride", SupportsShouldProcess = true, ConfirmImpact = ConfirmImpact.Medium)]
[Alias("Update-PSResourceGetInstallPathOverride")]
[OutputType(typeof(void))]

public sealed class SetPSResourceGetInstallPathOverride : PSCmdlet
{
#region Parameters

/// <summary>
/// Specifies the desired path for the override.
/// </summary>
[Parameter(Position = 0, ValueFromPipeline = true, Mandatory = true, HelpMessage = "Path for the override.")]
[ValidateNotNullOrEmpty]
public string Path
{
get
{
return _path;
}

set
{
if (WildcardPattern.ContainsWildcardCharacters(value))
{
throw new PSArgumentException("Wildcard characters are not allowed in the path.");
}

// This will throw if path cannot be resolved
_path = GetResolvedProviderPathFromPSPath(
Environment.ExpandEnvironmentVariables(value),
out ProviderInfo provider
).First();
}
}
private string _path;

/// <summary>
/// Specifies the scope of installation.
/// </summary>
[Parameter(Position = 1)]
public ScopeType Scope { get; set; }

#endregion

#region Method override - Begin

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"),
"OsIsNotWindows",
ErrorCategory.InvalidOperation,
this
)
);
}
}

#endregion

#region Method override - Process

protected override void ProcessRecord()
{
// Assets
EnvironmentVariableTarget EnvScope = (Scope is ScopeType.AllUsers) ? EnvironmentVariableTarget.Machine : EnvironmentVariableTarget.User;
string PathForModules = System.IO.Path.Combine(_path, "Modules");
string PathForScripts = System.IO.Path.Combine(_path, "Scripts");

// Set env variable for install path override
string PathOverrideCurrentValue = Environment.GetEnvironmentVariable(
"PSResourceGetInstallPathOverride",
EnvScope
);
if (!String.IsNullOrEmpty(PathOverrideCurrentValue))
{
WriteVerbose(
String.Format(
"Current value of PSResourceGetInstallPathOverride in scope '{0}': '{1}'",
EnvScope.ToString(),
PathOverrideCurrentValue
)
);
}
if (
!String.IsNullOrEmpty(PathOverrideCurrentValue) &&
String.Equals(
Environment.ExpandEnvironmentVariables(PathOverrideCurrentValue),
_path,
StringComparison.Ordinal
)
)
{
WriteVerbose(
String.Format(
"PSResourceGetInstallPathOverride in scope '{0}' is already '{1}', no change needed.",
EnvScope.ToString(),
_path
)
);
}
else
{
if (this.ShouldProcess($"Set environment variable PSResourceGetPathOverride in scope '{EnvScope} to '{_path}"))
{
Environment.SetEnvironmentVariable(
"PSResourceGetInstallPathOverride",
_path,
EnvScope
);
WriteVerbose(
String.Format(
"PSResourceGetInstallPathOverride in scope '{0}' was successfully set to: '{1}'",
EnvScope.ToString(),
_path
)
);
}
}

// Add install path override for modules to PSModulePath
string CurrentPSModulePath = Environment.GetEnvironmentVariable(
"PSModulePath",
EnvScope
);
if (String.IsNullOrEmpty(CurrentPSModulePath))
{
WriteVerbose(String.Format("PSModulePath in scope '{0}' is empty.", EnvScope.ToString()));
if (this.ShouldProcess($"Set environment pariable 'PSModulePath' in scope '{EnvScope} to '{PathForModules}"))
{
Environment.SetEnvironmentVariable(
"PSModulePath",
PathForModules,
EnvScope
);
}
}
WriteVerbose(string.Format("Current value of PSModulePath in {0} context: '{1}'", EnvScope.ToString(), CurrentPSModulePath));
string[] CurrentPSModulePaths = CurrentPSModulePath.Trim(';').Split(';').Select(s => Environment.ExpandEnvironmentVariables(s)).ToArray();
if (CurrentPSModulePaths.Contains(PathForModules))
{
WriteVerbose(String.Format("PSModulePath in scope '{0}' already contains '{1}', no change needed.", EnvScope.ToString(), PathForModules));
}
else
{
WriteVerbose(
String.Format(
"PSModulePath in scope '{0}' does not already contain '{1}'",
EnvScope.ToString(),
PathForModules
)
);
if (this.ShouldProcess($"Add '{PathForModules}' to environment variable 'PSModulePath' in scope '{EnvScope}"))
{
Environment.SetEnvironmentVariable(
"PSModulePath",
String.Format("{0};{1}", PathForModules, CurrentPSModulePath),
EnvScope
);
WriteVerbose(
String.Format(
"Successfully added '{0}' to PSModulePath in scope '{1}'",
PathForModules,
EnvScope.ToString()
)
);
}
}

// Add install path override for scripts to Path
string CurrentPath = Environment.GetEnvironmentVariable(
"Path",
EnvScope
);
if (String.IsNullOrEmpty(CurrentPath))
{
WriteVerbose(String.Format("Path in scope '{0}' is empty.", EnvScope.ToString()));
if (this.ShouldProcess($"Set environment pariable 'Path' in scope '{EnvScope} to '{PathForScripts}"))
{
Environment.SetEnvironmentVariable(
"Path",
PathForScripts,
EnvScope
);
}
}
WriteVerbose(string.Format("Current value of Path in {0} context: '{1}'", EnvScope.ToString(), CurrentPath));
string[] CurrentPaths = CurrentPath.Trim(';').Split(';').Select(s => Environment.ExpandEnvironmentVariables(s)).ToArray();
if (CurrentPaths.Contains(PathForScripts))
{
WriteVerbose(String.Format("Path in scope '{0}' already contains '{1}', no change needed.", EnvScope.ToString(), PathForScripts));
}
else
{
WriteVerbose(
String.Format(
"Override install path is not already in Path for scope '{0}'",
EnvScope.ToString()
)
);
if (this.ShouldProcess($"Add '{PathForScripts}' to environment variable 'Path' in scope '{EnvScope}"))
{
Environment.SetEnvironmentVariable(
"Path",
String.Format("{0};{1}", PathForScripts, CurrentPath),
EnvScope
);
WriteVerbose(
String.Format(
"Successfully added '{0}' to Path in scope '{1}'",
PathForScripts,
EnvScope.ToString()
)
);
}
}
}

#endregion
}
}
Loading