|
| 1 | +// Licensed to the .NET Foundation under one or more agreements. |
| 2 | +// The .NET Foundation licenses this file to you under the MIT license. |
| 3 | + |
| 4 | +using System; |
| 5 | +using System.IO; |
| 6 | +using System.Linq; |
| 7 | +using System.Text.Json; |
| 8 | +using Microsoft.DotNet.Cli.Utils; |
| 9 | + |
| 10 | +namespace Microsoft.DotNet.Tools.Bootstrapper; |
| 11 | + |
| 12 | +public class DotnetInstaller : IDotnetInstaller |
| 13 | +{ |
| 14 | + private readonly IEnvironmentProvider _environmentProvider; |
| 15 | + |
| 16 | + public DotnetInstaller(IEnvironmentProvider? environmentProvider = null) |
| 17 | + { |
| 18 | + _environmentProvider = environmentProvider ?? new EnvironmentProvider(); |
| 19 | + } |
| 20 | + |
| 21 | + public SdkInstallType GetConfiguredInstallType(out string? currentInstallPath) |
| 22 | + { |
| 23 | + currentInstallPath = null; |
| 24 | + string? foundDotnet = _environmentProvider.GetCommandPath("dotnet"); |
| 25 | + if (string.IsNullOrEmpty(foundDotnet)) |
| 26 | + { |
| 27 | + return SdkInstallType.None; |
| 28 | + } |
| 29 | + |
| 30 | + string installDir = Path.GetDirectoryName(foundDotnet)!; |
| 31 | + currentInstallPath = installDir; |
| 32 | + |
| 33 | + string? dotnetRoot = Environment.GetEnvironmentVariable("DOTNET_ROOT"); |
| 34 | + string programFiles = Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles); |
| 35 | + string programFilesX86 = Environment.GetFolderPath(Environment.SpecialFolder.ProgramFilesX86); |
| 36 | + bool isAdminInstall = installDir.StartsWith(Path.Combine(programFiles, "dotnet"), StringComparison.OrdinalIgnoreCase) |
| 37 | + || installDir.StartsWith(Path.Combine(programFilesX86, "dotnet"), StringComparison.OrdinalIgnoreCase); |
| 38 | + |
| 39 | + if (isAdminInstall) |
| 40 | + { |
| 41 | + // Admin install: DOTNET_ROOT should not be set, or if set, should match installDir |
| 42 | + if (!string.IsNullOrEmpty(dotnetRoot) && !PathsEqual(dotnetRoot, installDir) && !dotnetRoot.StartsWith(Path.Combine(programFiles, "dotnet"), StringComparison.OrdinalIgnoreCase) && !dotnetRoot.StartsWith(Path.Combine(programFilesX86, "dotnet"), StringComparison.OrdinalIgnoreCase)) |
| 43 | + { |
| 44 | + return SdkInstallType.Inconsistent; |
| 45 | + } |
| 46 | + return SdkInstallType.Admin; |
| 47 | + } |
| 48 | + else |
| 49 | + { |
| 50 | + // User install: DOTNET_ROOT must be set and match installDir |
| 51 | + if (string.IsNullOrEmpty(dotnetRoot) || !PathsEqual(dotnetRoot, installDir)) |
| 52 | + { |
| 53 | + return SdkInstallType.Inconsistent; |
| 54 | + } |
| 55 | + return SdkInstallType.User; |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + private static bool PathsEqual(string a, string b) |
| 60 | + { |
| 61 | + return string.Equals(Path.GetFullPath(a).TrimEnd(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar), |
| 62 | + Path.GetFullPath(b).TrimEnd(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar), |
| 63 | + StringComparison.OrdinalIgnoreCase); |
| 64 | + } |
| 65 | + |
| 66 | + public string GetDefaultDotnetInstallPath() |
| 67 | + { |
| 68 | + return Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "dotnet"); |
| 69 | + } |
| 70 | + |
| 71 | + public GlobalJsonInfo GetGlobalJsonInfo(string initialDirectory) |
| 72 | + { |
| 73 | + string? directory = initialDirectory; |
| 74 | + while (!string.IsNullOrEmpty(directory)) |
| 75 | + { |
| 76 | + string globalJsonPath = Path.Combine(directory, "global.json"); |
| 77 | + if (File.Exists(globalJsonPath)) |
| 78 | + { |
| 79 | + using var stream = File.OpenRead(globalJsonPath); |
| 80 | + var contents = JsonSerializer.Deserialize( |
| 81 | + stream, |
| 82 | + GlobalJsonContentsJsonContext.Default.GlobalJsonContents); |
| 83 | + return new GlobalJsonInfo |
| 84 | + { |
| 85 | + GlobalJsonPath = globalJsonPath, |
| 86 | + GlobalJsonContents = contents |
| 87 | + }; |
| 88 | + } |
| 89 | + var parent = Directory.GetParent(directory); |
| 90 | + if (parent == null) |
| 91 | + break; |
| 92 | + directory = parent.FullName; |
| 93 | + } |
| 94 | + return new GlobalJsonInfo(); |
| 95 | + } |
| 96 | + |
| 97 | + public string? GetLatestInstalledAdminVersion() |
| 98 | + { |
| 99 | + // TODO: Implement this |
| 100 | + return null; |
| 101 | + } |
| 102 | +} |
0 commit comments