-
Notifications
You must be signed in to change notification settings - Fork 108
Expand file tree
/
Copy pathSetPSResourceGetInstallPathOverride.cs
More file actions
239 lines (222 loc) · 9.5 KB
/
SetPSResourceGetInstallPathOverride.cs
File metadata and controls
239 lines (222 loc) · 9.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
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
}
}