-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Dotnetup: Add logic to configure PATH and other default install settings when installing SDK #52453
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 5 commits
6158d69
c7dec0a
07f6f38
f7db809
9a331ed
dba18fc
ea5d2bb
ef7728b
a1389bb
bac3323
941fe48
eaab195
9b2f667
2d4b3ac
bb58608
6ba65d1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -24,6 +24,8 @@ internal class SdkInstallCommand(ParseResult result) : CommandBase(result) | |
|
|
||
| private readonly IDotnetInstallManager _dotnetInstaller = new DotnetInstallManager(); | ||
| private readonly ChannelVersionResolver _channelVersionResolver = new ChannelVersionResolver(); | ||
| private InstallRootManager? _installRootManager; | ||
| private InstallRootManager InstallRootManager => _installRootManager ??= new InstallRootManager(_dotnetInstaller); | ||
|
|
||
| public override int Execute() | ||
| { | ||
|
|
@@ -126,6 +128,7 @@ public override int Execute() | |
| } | ||
|
|
||
| bool? resolvedSetDefaultInstall = _setDefaultInstall; | ||
| UserInstallRootChanges? userInstallRootChanges = null; | ||
|
|
||
| if (resolvedSetDefaultInstall == null) | ||
| { | ||
|
|
@@ -142,7 +145,15 @@ public override int Execute() | |
| { | ||
| if (DotnetupUtilities.PathsEqual(resolvedInstallPath, currentDotnetInstallRoot.Path)) | ||
| { | ||
| // No need to prompt here, the default install is already set up. | ||
| // If the current install is fully configured and matches the resolved path, skip the prompt | ||
| if (currentDotnetInstallRoot.IsFullyConfigured) | ||
baronfel marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| { | ||
| // Default install is already set up correctly, no need to prompt | ||
| resolvedSetDefaultInstall = false; | ||
| } | ||
| // If not fully configured, we leave resolvedSetDefaultInstall as null (no prompt) | ||
| // since the install path is already on PATH. The user can run "dotnetup defaultinstall" | ||
| // if they want to fully configure it. | ||
|
||
| } | ||
| else | ||
| { | ||
|
|
@@ -249,7 +260,29 @@ public override int Execute() | |
|
|
||
| if (resolvedSetDefaultInstall == true) | ||
| { | ||
| _dotnetInstaller.ConfigureInstallType(InstallType.User, resolvedInstallPath); | ||
| if (OperatingSystem.IsWindows()) | ||
|
||
| { | ||
| // Use InstallRootManager to apply the user install root configuration | ||
| if (userInstallRootChanges == null) | ||
| { | ||
| userInstallRootChanges = InstallRootManager.GetUserInstallRootChanges(); | ||
| } | ||
|
|
||
| bool succeeded = InstallRootManager.ApplyUserInstallRoot( | ||
| userInstallRootChanges, | ||
| msg => SpectreAnsiConsole.WriteLine(msg), | ||
| msg => SpectreAnsiConsole.MarkupLine($"[red]{msg}[/]")); | ||
|
|
||
| if (!succeeded) | ||
| { | ||
| SpectreAnsiConsole.MarkupLine("[yellow]Warning: Failed to configure default install root. You can configure it later with the \"dotnetup defaultinstall\" command.[/]"); | ||
| } | ||
| } | ||
| else | ||
| { | ||
| // For non-Windows platforms, use the existing ConfigureInstallType method | ||
| _dotnetInstaller.ConfigureInstallType(InstallType.User, resolvedInstallPath); | ||
| } | ||
| } | ||
|
|
||
| if (resolvedUpdateGlobalJson == true) | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -22,27 +22,62 @@ public DotnetInstallManager(IEnvironmentProvider? environmentProvider = null) | |
|
|
||
| public DotnetInstallRootConfiguration? GetConfiguredInstallType() | ||
| { | ||
|
|
||
| string? foundDotnet = _environmentProvider.GetCommandPath("dotnet"); | ||
| if (string.IsNullOrEmpty(foundDotnet)) | ||
| { | ||
| return null; | ||
| } | ||
|
|
||
| string installDir = Path.GetDirectoryName(foundDotnet)!; | ||
|
|
||
|
|
||
| string? dotnetRoot = Environment.GetEnvironmentVariable("DOTNET_ROOT"); | ||
| string programFiles = Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles); | ||
| string programFilesX86 = Environment.GetFolderPath(Environment.SpecialFolder.ProgramFilesX86); | ||
| bool isAdminInstall = installDir.StartsWith(Path.Combine(programFiles, "dotnet"), StringComparison.OrdinalIgnoreCase) || | ||
| installDir.StartsWith(Path.Combine(programFilesX86, "dotnet"), StringComparison.OrdinalIgnoreCase); // TODO: This should be improved to not be windows-specific https://github.com/dotnet/sdk/issues/51601 | ||
|
|
||
| var installRoot = new DotnetInstallRoot(installDir, InstallerUtilities.GetDefaultInstallArchitecture()); | ||
|
|
||
| bool isSetAsDotnetRoot = DotnetupUtilities.PathsEqual(dotnetRoot, installDir); | ||
| // Use InstallRootManager to determine if the install is fully configured | ||
| if (OperatingSystem.IsWindows()) | ||
| { | ||
| var installRootManager = new InstallRootManager(this); | ||
baronfel marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| // Check if user install root is fully configured | ||
| var userChanges = installRootManager.GetUserInstallRootChanges(); | ||
| if (!userChanges.NeedsChange() && DotnetupUtilities.PathsEqual(installDir, userChanges.UserDotnetPath)) | ||
| { | ||
| return new(installRoot, InstallType.User, IsFullyConfigured: true); | ||
| } | ||
|
|
||
| return new(installRoot, isAdminInstall ? InstallType.Admin : InstallType.User, IsOnPath: true, isSetAsDotnetRoot); | ||
| // Check if admin install root is fully configured | ||
| var adminChanges = installRootManager.GetAdminInstallRootChanges(); | ||
| if (!adminChanges.NeedsChange()) | ||
| { | ||
| string programFiles = Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles); | ||
| string programFilesX86 = Environment.GetFolderPath(Environment.SpecialFolder.ProgramFilesX86); | ||
| bool isAdminInstall = installDir.StartsWith(Path.Combine(programFiles, "dotnet"), StringComparison.OrdinalIgnoreCase) || | ||
| installDir.StartsWith(Path.Combine(programFilesX86, "dotnet"), StringComparison.OrdinalIgnoreCase); | ||
|
||
| if (isAdminInstall) | ||
| { | ||
| return new(installRoot, InstallType.Admin, IsFullyConfigured: true); | ||
| } | ||
| } | ||
|
|
||
| // Not fully configured, but PATH resolves to dotnet | ||
| // Determine type based on location | ||
| string programFilesPath = Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles); | ||
| string programFilesX86Path = Environment.GetFolderPath(Environment.SpecialFolder.ProgramFilesX86); | ||
| bool isAdminPath = installDir.StartsWith(Path.Combine(programFilesPath, "dotnet"), StringComparison.OrdinalIgnoreCase) || | ||
| installDir.StartsWith(Path.Combine(programFilesX86Path, "dotnet"), StringComparison.OrdinalIgnoreCase); | ||
|
|
||
| return new(installRoot, isAdminPath ? InstallType.Admin : InstallType.User, IsFullyConfigured: false); | ||
| } | ||
| else | ||
| { | ||
| // For non-Windows platforms, determine based on path location | ||
| // TODO: This should be improved to not be windows-specific https://github.com/dotnet/sdk/issues/51601 | ||
| string programFiles = Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles); | ||
| string programFilesX86 = Environment.GetFolderPath(Environment.SpecialFolder.ProgramFilesX86); | ||
| bool isAdminInstall = installDir.StartsWith(Path.Combine(programFiles, "dotnet"), StringComparison.OrdinalIgnoreCase) || | ||
| installDir.StartsWith(Path.Combine(programFilesX86, "dotnet"), StringComparison.OrdinalIgnoreCase); | ||
|
|
||
| // For now, we consider it fully configured if it's on PATH | ||
| return new(installRoot, isAdminInstall ? InstallType.Admin : InstallType.User, IsFullyConfigured: true); | ||
| } | ||
| } | ||
|
|
||
| public string GetDefaultDotnetInstallPath() | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't declare this here. Only declare it later when it is first used.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done in commit ef7728b. Moved the declaration to line 158 where it's first used.