Skip to content

Commit 19f42c8

Browse files
committed
Initial ConfigureInstallType implementation
Copilot prompt: Implement the #method:'Microsoft.DotNet.Tools.Bootstrapper.DotnetInstaller.ConfigureInstallType':4479-4606 method. If the install type is user, remove any other folder with dotnet in it from the PATH and add the dotnetRoot to the PATH. Also set the DOTNET_ROOT environment variable to dotnetRoot. If the install type is Admin, unset DOTNET_ROOT, and add dotnetRoot to the path (removing any other dotnet folder from the path). If the install type is None, unset DOTNET_ROOT and remove any dotnet folder from the path. For any other install type, throw an ArgumentException.
1 parent 8e2bdf9 commit 19f42c8

File tree

1 file changed

+38
-1
lines changed

1 file changed

+38
-1
lines changed

src/Installer/dnup/DotnetInstaller.cs

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,5 +103,42 @@ public GlobalJsonInfo GetGlobalJsonInfo(string initialDirectory)
103103

104104
public void InstallSdks(string dotnetRoot, ProgressContext progressContext, IEnumerable<string> sdkVersions) => throw new NotImplementedException();
105105
public void UpdateGlobalJson(string globalJsonPath, string? sdkVersion = null, bool? allowPrerelease = null, string? rollForward = null) => throw new NotImplementedException();
106-
public void ConfigureInstallType(SdkInstallType installType, string? dotnetRoot = null) => throw new NotImplementedException();
106+
107+
public void ConfigureInstallType(SdkInstallType installType, string? dotnetRoot = null)
108+
{
109+
// Get current PATH
110+
var path = Environment.GetEnvironmentVariable("PATH", EnvironmentVariableTarget.User) ?? string.Empty;
111+
var pathEntries = path.Split(Path.PathSeparator, StringSplitOptions.RemoveEmptyEntries).ToList();
112+
// Remove all entries containing "dotnet" (case-insensitive)
113+
pathEntries = pathEntries.Where(p => !p.Contains("dotnet", StringComparison.OrdinalIgnoreCase)).ToList();
114+
115+
switch (installType)
116+
{
117+
case SdkInstallType.User:
118+
if (string.IsNullOrEmpty(dotnetRoot))
119+
throw new ArgumentNullException(nameof(dotnetRoot));
120+
// Add dotnetRoot to PATH
121+
pathEntries.Insert(0, dotnetRoot);
122+
// Set DOTNET_ROOT
123+
Environment.SetEnvironmentVariable("DOTNET_ROOT", dotnetRoot, EnvironmentVariableTarget.User);
124+
break;
125+
case SdkInstallType.Admin:
126+
if (string.IsNullOrEmpty(dotnetRoot))
127+
throw new ArgumentNullException(nameof(dotnetRoot));
128+
// Add dotnetRoot to PATH
129+
pathEntries.Insert(0, dotnetRoot);
130+
// Unset DOTNET_ROOT
131+
Environment.SetEnvironmentVariable("DOTNET_ROOT", null, EnvironmentVariableTarget.User);
132+
break;
133+
case SdkInstallType.None:
134+
// Unset DOTNET_ROOT
135+
Environment.SetEnvironmentVariable("DOTNET_ROOT", null, EnvironmentVariableTarget.User);
136+
break;
137+
default:
138+
throw new ArgumentException($"Unknown install type: {installType}", nameof(installType));
139+
}
140+
// Update PATH
141+
var newPath = string.Join(Path.PathSeparator, pathEntries);
142+
Environment.SetEnvironmentVariable("PATH", newPath, EnvironmentVariableTarget.User);
143+
}
107144
}

0 commit comments

Comments
 (0)