Skip to content

Commit 4ba0e57

Browse files
committed
(#3675) Migrate environment variable references to StringResources
Update EnvironmentVariables class to reference constants from chocolatey.StringResources instead of hardcoded strings. Mark the old EnvironmentVariables class as obsolete to enforce usage of the new centralized resource definitions. Refactor Helpers/Paths.cs and Helpers/EnvironmentHelper.cs to use EnvironmentVariables.System.Path for PATH variable access and modification, ensuring consistency with the updated resource usage. These changes improve maintainability by consolidating environment variable names in a single source and prevent duplication errors.
1 parent e715134 commit 4ba0e57

File tree

4 files changed

+35
-28
lines changed

4 files changed

+35
-28
lines changed

src/Chocolatey.PowerShell/Chocolatey.PowerShell.csproj

Lines changed: 3 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" />

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 != EnvironmentVariables.System.SystemName)
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)

src/Chocolatey.PowerShell/Shared/EnvironmentVariables.cs

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

17+
using chocolatey;
18+
using System;
19+
1720
namespace Chocolatey.PowerShell.Shared
1821
{
22+
[Obsolete("This class has been deprecated, make use of chocolatey.StringResourrces.EnvironmentVariables instead.", error: false)]
1923
public static class EnvironmentVariables
2024
{
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";
25+
public const string ChocolateyLastPathUpdate = StringResources.EnvironmentVariables.Package.ChocolateyLastPathUpdate;
26+
public const string ComputerName = StringResources.EnvironmentVariables.System.ComputerName;
27+
public const string Path = StringResources.EnvironmentVariables.System.Path;
28+
public const string ProcessorArchitecture = StringResources.EnvironmentVariables.System.ProcessorArchitecture;
29+
public const string PSModulePath = StringResources.EnvironmentVariables.System.PSModulePath;
30+
public const string System = StringResources.EnvironmentVariables.System.SystemName;
31+
public const string SystemRoot = StringResources.EnvironmentVariables.System.SystemRoot;
32+
public const string Username = StringResources.EnvironmentVariables.System.Username;
2933
}
3034
}

0 commit comments

Comments
 (0)