Skip to content

Commit fca4582

Browse files
authored
Merge pull request #3241 from AdmiringWorm/environment-variables
(#3675) Extract all used environment variables
2 parents 099c10b + 1a86273 commit fca4582

File tree

29 files changed

+982
-286
lines changed

29 files changed

+982
-286
lines changed

src/Chocolatey.PowerShell/Chocolatey.PowerShell.csproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,9 @@
5858
</Reference>
5959
</ItemGroup>
6060
<ItemGroup>
61+
<Compile Include="..\chocolatey\StringResources.cs">
62+
<Link>Shared\StringResources.cs</Link>
63+
</Compile>
6164
<Compile Include="Commands\GetEnvironmentVariableCommand.cs" />
6265
<Compile Include="Commands\GetEnvironmentVariableNamesCommand.cs" />
6366
<Compile Include="Commands\InstallChocolateyPathCommand.cs" />
@@ -75,6 +78,7 @@
7578
<Link>Properties\SolutionVersion.cs</Link>
7679
</Compile>
7780
<Compile Include="Shared\ChocolateyCmdlet.cs" />
81+
<Compile Include="Shared\EnvironmentNames.cs" />
7882
<Compile Include="Shared\EnvironmentVariables.cs" />
7983
<Compile Include="Win32\NativeMethods.cs" />
8084
</ItemGroup>

src/Chocolatey.PowerShell/Helpers/EnvironmentHelper.cs

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@
1818
using System.Collections.Generic;
1919
using System.Linq;
2020
using System.Management.Automation;
21-
using Chocolatey.PowerShell.Shared;
2221
using Chocolatey.PowerShell.Win32;
2322
using Microsoft.Win32;
23+
using static chocolatey.StringResources;
2424

2525
namespace Chocolatey.PowerShell.Helpers
2626
{
@@ -163,7 +163,7 @@ public static void SetVariable(PSCmdlet cmdlet, string name, EnvironmentVariable
163163
// The value doesn't exist yet, suppress the error.
164164
}
165165

166-
if (name.ToUpper() == EnvironmentVariables.Path)
166+
if (name.ToUpper() == EnvironmentVariables.System.Path)
167167
{
168168
registryType = RegistryValueKind.ExpandString;
169169
}
@@ -199,8 +199,8 @@ public static void SetVariable(PSCmdlet cmdlet, string name, EnvironmentVariable
199199
out UIntPtr result);
200200

201201
// 2. Set a user environment variable making the system refresh
202-
var setxPath = string.Format(@"{0}\System32\setx.exe", GetVariable(cmdlet, EnvironmentVariables.SystemRoot, EnvironmentVariableTarget.Process));
203-
cmdlet.InvokeCommand.InvokeScript($"& \"{setxPath}\" {EnvironmentVariables.ChocolateyLastPathUpdate} \"{DateTime.Now.ToFileTime()}\"");
202+
var setxPath = string.Format(@"{0}\System32\setx.exe", GetVariable(cmdlet, EnvironmentVariables.System.SystemRoot, EnvironmentVariableTarget.Process));
203+
cmdlet.InvokeCommand.InvokeScript($"& \"{setxPath}\" {EnvironmentVariables.Package.ChocolateyLastPathUpdate} \"{DateTime.Now.ToFileTime()}\"");
204204
}
205205
}
206206
catch (Exception error)
@@ -217,16 +217,16 @@ public static void SetVariable(PSCmdlet cmdlet, string name, EnvironmentVariable
217217
/// <param name="cmdlet">The cmdlet calling the method.</param>
218218
public static void UpdateSession(PSCmdlet cmdlet)
219219
{
220-
var userName = GetVariable(cmdlet, EnvironmentVariables.Username, EnvironmentVariableTarget.Process);
221-
var architecture = GetVariable(cmdlet, EnvironmentVariables.ProcessorArchitecture, EnvironmentVariableTarget.Process);
222-
var psModulePath = GetVariable(cmdlet, EnvironmentVariables.PSModulePath, EnvironmentVariableTarget.Process);
220+
var userName = GetVariable(cmdlet, EnvironmentVariables.System.Username, EnvironmentVariableTarget.Process);
221+
var architecture = GetVariable(cmdlet, EnvironmentVariables.System.ProcessorArchitecture, EnvironmentVariableTarget.Process);
222+
var psModulePath = GetVariable(cmdlet, EnvironmentVariables.System.PSModulePath, EnvironmentVariableTarget.Process);
223223

224224
var scopeList = new List<EnvironmentVariableTarget>() { EnvironmentVariableTarget.Process, EnvironmentVariableTarget.Machine };
225225

226-
var computerName = GetVariable(cmdlet, EnvironmentVariables.ComputerName, EnvironmentVariableTarget.Process);
226+
var computerName = GetVariable(cmdlet, EnvironmentVariables.System.ComputerName, EnvironmentVariableTarget.Process);
227227

228228
// User scope should override (be checked after) machine scope, but only if we're not running as SYSTEM
229-
if (userName != computerName && userName != EnvironmentVariables.System)
229+
if (userName != computerName && userName != Shared.EnvironmentNames.System)
230230
{
231231
scopeList.Add(EnvironmentVariableTarget.User);
232232
}
@@ -247,23 +247,23 @@ public static void UpdateSession(PSCmdlet cmdlet)
247247

248248
// Update PATH, combining both scopes' values.
249249
var paths = new string[2];
250-
paths[0] = GetVariable(cmdlet, EnvironmentVariables.Path, EnvironmentVariableTarget.Machine);
251-
paths[1] = GetVariable(cmdlet, EnvironmentVariables.Path, EnvironmentVariableTarget.User);
250+
paths[0] = GetVariable(cmdlet, EnvironmentVariables.System.Path, EnvironmentVariableTarget.Machine);
251+
paths[1] = GetVariable(cmdlet, EnvironmentVariables.System.Path, EnvironmentVariableTarget.User);
252252

253-
SetVariable(cmdlet, EnvironmentVariables.Path, EnvironmentVariableTarget.Process, string.Join(";", paths));
253+
SetVariable(cmdlet, EnvironmentVariables.System.Path, EnvironmentVariableTarget.Process, string.Join(";", paths));
254254

255255
// Preserve PSModulePath as it's almost always updated by process, preserve it
256-
SetVariable(cmdlet, EnvironmentVariables.PSModulePath, EnvironmentVariableTarget.Process, psModulePath);
256+
SetVariable(cmdlet, EnvironmentVariables.System.PSModulePath, EnvironmentVariableTarget.Process, psModulePath);
257257

258258
// Preserve user and architecture
259259
if (!string.IsNullOrEmpty(userName))
260260
{
261-
SetVariable(cmdlet, EnvironmentVariables.Username, EnvironmentVariableTarget.Process, userName);
261+
SetVariable(cmdlet, EnvironmentVariables.System.Username, EnvironmentVariableTarget.Process, userName);
262262
}
263263

264264
if (!string.IsNullOrEmpty(architecture))
265265
{
266-
SetVariable(cmdlet, EnvironmentVariables.ProcessorArchitecture, EnvironmentVariableTarget.Process, architecture);
266+
SetVariable(cmdlet, EnvironmentVariables.System.ProcessorArchitecture, EnvironmentVariableTarget.Process, architecture);
267267
}
268268
}
269269
}

src/Chocolatey.PowerShell/Helpers/Paths.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
using System.Management.Automation;
2121
using System.Text;
2222
using System.Text.RegularExpressions;
23-
using Chocolatey.PowerShell.Shared;
23+
using static chocolatey.StringResources;
2424

2525
namespace Chocolatey.PowerShell.Helpers
2626
{
@@ -106,7 +106,7 @@ internal static string GetPathString(IList<string> paths)
106106
/// <param name="scope">The target scope of the PATH variable to modify.</param>
107107
public static void InstallPathEntry(PSCmdlet cmdlet, string pathEntry, EnvironmentVariableTarget scope)
108108
{
109-
var pathEntries = new List<string>(ParsePathString(EnvironmentHelper.GetVariable(cmdlet, EnvironmentVariables.Path, scope, preserveVariables: true)));
109+
var pathEntries = new List<string>(ParsePathString(EnvironmentHelper.GetVariable(cmdlet, EnvironmentVariables.System.Path, scope, preserveVariables: true)));
110110
if (FindPathIndex(pathEntries, pathEntry) == -1)
111111
{
112112
PSHelper.WriteHost(cmdlet, $"PATH environment variable does not have {pathEntry} in it. Adding...");
@@ -116,7 +116,7 @@ public static void InstallPathEntry(PSCmdlet cmdlet, string pathEntry, Environme
116116

117117
void updatePath()
118118
{
119-
EnvironmentHelper.SetVariable(cmdlet, EnvironmentVariables.Path, scope, newPath);
119+
EnvironmentHelper.SetVariable(cmdlet, EnvironmentVariables.System.Path, scope, newPath);
120120
}
121121

122122
if (scope == EnvironmentVariableTarget.Machine)
@@ -138,7 +138,7 @@ void updatePath()
138138
/// <param name="scope">The target scope of the PATH variable to modify.</param>
139139
public static void UninstallPathEntry(PSCmdlet cmdlet, string pathEntry, EnvironmentVariableTarget scope)
140140
{
141-
var pathEntries = new List<string>(ParsePathString(EnvironmentHelper.GetVariable(cmdlet, EnvironmentVariables.Path, scope, preserveVariables: true)));
141+
var pathEntries = new List<string>(ParsePathString(EnvironmentHelper.GetVariable(cmdlet, EnvironmentVariables.System.Path, scope, preserveVariables: true)));
142142
var removeIndex = FindPathIndex(pathEntries, pathEntry);
143143
if (removeIndex >= 0)
144144
{
@@ -149,7 +149,7 @@ public static void UninstallPathEntry(PSCmdlet cmdlet, string pathEntry, Environ
149149

150150
void updatePath()
151151
{
152-
EnvironmentHelper.SetVariable(cmdlet, EnvironmentVariables.Path, scope, newPath);
152+
EnvironmentHelper.SetVariable(cmdlet, EnvironmentVariables.System.Path, scope, newPath);
153153
}
154154

155155
if (scope == EnvironmentVariableTarget.Machine)
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// Copyright © 2017 - 2025 Chocolatey Software, Inc
2+
// Copyright © 2011 - 2017 RealDimensions Software, LLC
3+
//
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
//
7+
// You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing, software
12+
// distributed under the License is distributed on an "AS IS" BASIS,
13+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
// See the License for the specific language governing permissions and
15+
// limitations under the License.
16+
17+
namespace Chocolatey.PowerShell.Shared
18+
{
19+
public static class EnvironmentNames
20+
{
21+
public const string System = "SYSTEM";
22+
}
23+
}

src/Chocolatey.PowerShell/Shared/EnvironmentVariables.cs

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,23 @@
1414
// See the License for the specific language governing permissions and
1515
// limitations under the License.
1616

17+
using System;
18+
using chocolatey;
19+
1720
namespace Chocolatey.PowerShell.Shared
1821
{
22+
23+
[Obsolete("This class has been deprecated, make use of chocolatey.StringResources.EnvironmentVariables instead.", error: false)]
1924
public static class EnvironmentVariables
2025
{
21-
public const string ChocolateyLastPathUpdate = "ChocolateyLastPathUpdate";
22-
public const string ComputerName = "COMPUTERNAME";
23-
public const string Path = "PATH";
24-
public const string ProcessorArchitecture = "PROCESSOR_ARCHITECTURE";
25-
public const string PSModulePath = "PSModulePath";
26-
public const string System = "SYSTEM";
27-
public const string SystemRoot = "SystemRoot";
28-
public const string Username = "USERNAME";
26+
public const string ChocolateyLastPathUpdate = StringResources.EnvironmentVariables.Package.ChocolateyLastPathUpdate;
27+
public const string ComputerName = StringResources.EnvironmentVariables.System.ComputerName;
28+
public const string Path = StringResources.EnvironmentVariables.System.Path;
29+
public const string ProcessorArchitecture = StringResources.EnvironmentVariables.System.ProcessorArchitecture;
30+
public const string PSModulePath = StringResources.EnvironmentVariables.System.PSModulePath;
31+
[Obsolete("This constant has been replaced by EnvironmentNames.System.")]
32+
public const string System = EnvironmentNames.System;
33+
public const string SystemRoot = StringResources.EnvironmentVariables.System.SystemRoot;
34+
public const string Username = StringResources.EnvironmentVariables.System.Username;
2935
}
3036
}

src/chocolatey.console/Properties/AssemblyInfo.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,18 @@
1414
// See the License for the specific language governing permissions and
1515
// limitations under the License.
1616

17+
using System.Runtime.CompilerServices;
1718
using System.Runtime.InteropServices;
1819

1920
// The following GUID is for the ID of the typelib if this project is exposed to COM
2021

2122
[assembly: Guid("2f406051-3fe2-45f4-97bf-40968b86c5a9")]
23+
24+
// We allow the officially built chocolatey.extension to always see the internals.
25+
[assembly: InternalsVisibleTo("chocolatey.licensed, PublicKey=002400000480000094000000060200000024000052534131000400000100010001f55d4a9065e32d5e9854e592ffa5f7b3a707f55a17796937faf70f3ade21346dcf735216015d20304acd25d260d01202a390ac648ace0e93f6c4d6ac7cbede5b3e8f66e536d03ffa2d09594ac8de7bd147419c17e0fa1fa112b81b1b65a9e8b0ca148dc3a77e7b2917f448455ce9dbad266351710d097424692be8854704e8")]
26+
[assembly: InternalsVisibleTo("chocolatey.interfaces, PublicKey=002400000480000094000000060200000024000052534131000400000100010001f55d4a9065e32d5e9854e592ffa5f7b3a707f55a17796937faf70f3ade21346dcf735216015d20304acd25d260d01202a390ac648ace0e93f6c4d6ac7cbede5b3e8f66e536d03ffa2d09594ac8de7bd147419c17e0fa1fa112b81b1b65a9e8b0ca148dc3a77e7b2917f448455ce9dbad266351710d097424692be8854704e8")]
27+
28+
#if !FORCE_CHOCOLATEY_OFFICIAL_KEY
29+
[assembly: InternalsVisibleTo("chocolatey.licensed, PublicKey=00240000048000009400000006020000002400005253413100040000010001003f70732af6adf3f525d983852cc7049878c498e4f8a413bd7685c9edc503ed6c6e4087354c7c1797b7c9f6d9bd3c25cdd5f97b0e810b7dd1aaba2e489f60d17d1f03c0f4db27c63146ee64ce797e4c92d591a750d8c342f5b67775710f6f9b3d9d10b4121522779a1ff72776bcce3962ca66f1755919972fb70ffb289bc082b3")]
30+
[assembly: InternalsVisibleTo("chocolatey.interfaces, PublicKey=00240000048000009400000006020000002400005253413100040000010001003f70732af6adf3f525d983852cc7049878c498e4f8a413bd7685c9edc503ed6c6e4087354c7c1797b7c9f6d9bd3c25cdd5f97b0e810b7dd1aaba2e489f60d17d1f03c0f4db27c63146ee64ce797e4c92d591a750d8c342f5b67775710f6f9b3d9d10b4121522779a1ff72776bcce3962ca66f1755919972fb70ffb289bc082b3")]
31+
#endif
596 Bytes
Binary file not shown.

src/chocolatey.tests.integration/chocolatey.tests.integration.csproj

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,12 @@
6161
<CodeAnalysisRuleSet>MinimumRecommendedRules.ruleset</CodeAnalysisRuleSet>
6262
<Prefer32Bit>false</Prefer32Bit>
6363
</PropertyGroup>
64+
<PropertyGroup>
65+
<SignAssembly>true</SignAssembly>
66+
</PropertyGroup>
67+
<PropertyGroup>
68+
<AssemblyOriginatorKeyFile>TestKey.snk</AssemblyOriginatorKeyFile>
69+
</PropertyGroup>
6470
<ItemGroup>
6571
<Reference Include="AnyOf, Version=0.3.0.0, Culture=neutral, PublicKeyToken=b35e6abbb527c6b1, processorArchitecture=MSIL">
6672
<HintPath>..\packages\AnyOf.0.3.0\lib\net45\AnyOf.dll</HintPath>
@@ -493,6 +499,7 @@
493499
<None Include="packages.config">
494500
<SubType>Designer</SubType>
495501
</None>
502+
<None Include="TestKey.snk" />
496503
</ItemGroup>
497504
<ItemGroup>
498505
<ProjectReference Include="..\chocolatey.tests\chocolatey.tests.csproj">

src/chocolatey.tests.integration/infrastructure.app/builders/ConfigurationBuilderSpecs.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
using Microsoft.Win32;
3333
using chocolatey.tests.integration.scenarios;
3434
using FluentAssertions;
35+
using static chocolatey.StringResources;
3536

3637
namespace chocolatey.tests.integration.infrastructure.app.builders
3738
{
@@ -116,11 +117,11 @@ public override void Context()
116117

117118
if (EnvironmentVariableSet)
118119
{
119-
Environment.Setup(e => e.GetEnvironmentVariable(It.IsIn("http_proxy", "https_proxy"))).Returns(EnvironmentVariableProxyValue);
120+
Environment.Setup(e => e.GetEnvironmentVariable(It.IsIn(EnvironmentVariables.System.HttpProxy, EnvironmentVariables.System.HttpsProxy))).Returns(EnvironmentVariableProxyValue);
120121
}
121122
else
122123
{
123-
Environment.Setup(e => e.GetEnvironmentVariable(It.IsIn("http_proxy", "https_proxy"))).Returns(string.Empty);
124+
Environment.Setup(e => e.GetEnvironmentVariable(It.IsIn(EnvironmentVariables.System.HttpProxy, EnvironmentVariables.System.HttpsProxy))).Returns(string.Empty);
124125
}
125126

126127
if (ConfigSet)
@@ -192,11 +193,11 @@ public override void Context()
192193

193194
if (EnvironmentVariableSet)
194195
{
195-
Environment.Setup(e => e.GetEnvironmentVariable("no_proxy")).Returns(EnvironmentVariableProxyValue);
196+
Environment.Setup(e => e.GetEnvironmentVariable(EnvironmentVariables.System.NoProxy)).Returns(EnvironmentVariableProxyValue);
196197
}
197198
else
198199
{
199-
Environment.Setup(e => e.GetEnvironmentVariable("no_proxy")).Returns(string.Empty);
200+
Environment.Setup(e => e.GetEnvironmentVariable(EnvironmentVariables.System.NoProxy)).Returns(string.Empty);
200201
}
201202

202203
if (ArgumentSet)

src/chocolatey.tests/TestKey.snk

596 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)