|
| 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.Collections.Generic; |
| 6 | +using System.Text; |
| 7 | +using Microsoft.DotNet.Cli.NuGetPackageDownloader; |
| 8 | +using Microsoft.DotNet.Cli.ToolManifest; |
| 9 | +using Microsoft.DotNet.Cli.ToolPackage; |
| 10 | +using Microsoft.DotNet.Cli.Utils; |
| 11 | +using Microsoft.Extensions.EnvironmentAbstractions; |
| 12 | +using NuGet.Commands; |
| 13 | +using NuGet.Frameworks; |
| 14 | +using NuGet.Versioning; |
| 15 | +using static Microsoft.DotNet.Cli.Commands.Tool.Restore.ToolRestoreCommand; |
| 16 | + |
| 17 | +namespace Microsoft.DotNet.Cli.Commands.Tool.Restore; |
| 18 | + |
| 19 | +internal class ToolPackageRestorer |
| 20 | +{ |
| 21 | + private readonly IToolPackageDownloader _toolPackageDownloader; |
| 22 | + private readonly string[] _additionalSources; |
| 23 | + private readonly string[] _overrideSources; |
| 24 | + private readonly VerbosityOptions _verbosity; |
| 25 | + private readonly RestoreActionConfig _restoreActionConfig; |
| 26 | + |
| 27 | + private readonly ILocalToolsResolverCache _localToolsResolverCache; |
| 28 | + private readonly IFileSystem _fileSystem; |
| 29 | + |
| 30 | + |
| 31 | + |
| 32 | + public ToolPackageRestorer(IToolPackageDownloader toolPackageDownloader, |
| 33 | + string[] additionalSources, |
| 34 | + string[] overrideSources, |
| 35 | + VerbosityOptions verbosity, |
| 36 | + RestoreActionConfig restoreActionConfig, |
| 37 | + ILocalToolsResolverCache? localToolsResolverCache = null, |
| 38 | + IFileSystem? fileSystem = null) |
| 39 | + { |
| 40 | + _toolPackageDownloader = toolPackageDownloader; |
| 41 | + _additionalSources = additionalSources; |
| 42 | + _overrideSources = overrideSources; |
| 43 | + _verbosity = verbosity; |
| 44 | + _restoreActionConfig = restoreActionConfig; |
| 45 | + |
| 46 | + _localToolsResolverCache = localToolsResolverCache ?? new LocalToolsResolverCache(); |
| 47 | + _fileSystem = fileSystem ?? new FileSystemWrapper(); |
| 48 | + } |
| 49 | + |
| 50 | + public ToolRestoreResult InstallPackages( |
| 51 | + ToolManifestPackage package, |
| 52 | + FilePath? configFile) |
| 53 | + { |
| 54 | + string targetFramework = BundledTargetFramework.GetTargetFrameworkMoniker(); |
| 55 | + |
| 56 | + if (PackageHasBeenRestored(package, targetFramework)) |
| 57 | + { |
| 58 | + return ToolRestoreResult.Success( |
| 59 | + saveToCache: [], |
| 60 | + message: string.Format( |
| 61 | + CliCommandStrings.RestoreSuccessful, package.PackageId, |
| 62 | + package.Version.ToNormalizedString(), string.Join(", ", package.CommandNames))); |
| 63 | + } |
| 64 | + |
| 65 | + try |
| 66 | + { |
| 67 | + IToolPackage toolPackage = |
| 68 | + _toolPackageDownloader.InstallPackage( |
| 69 | + new PackageLocation( |
| 70 | + nugetConfig: configFile, |
| 71 | + additionalFeeds: _additionalSources, |
| 72 | + sourceFeedOverrides: _overrideSources, |
| 73 | + rootConfigDirectory: package.FirstEffectDirectory), |
| 74 | + package.PackageId, |
| 75 | + verbosity: _verbosity, |
| 76 | + ToVersionRangeWithOnlyOneVersion(package.Version), |
| 77 | + targetFramework, |
| 78 | + restoreActionConfig: _restoreActionConfig |
| 79 | + ); |
| 80 | + |
| 81 | + if (!ManifestCommandMatchesActualInPackage(package.CommandNames, [toolPackage.Command])) |
| 82 | + { |
| 83 | + return ToolRestoreResult.Failure( |
| 84 | + string.Format(CliCommandStrings.CommandsMismatch, |
| 85 | + JoinBySpaceWithQuote(package.CommandNames.Select(c => c.Value.ToString())), |
| 86 | + package.PackageId, |
| 87 | + toolPackage.Command.Name)); |
| 88 | + } |
| 89 | + |
| 90 | + return ToolRestoreResult.Success( |
| 91 | + saveToCache: |
| 92 | + [(new RestoredCommandIdentifier( |
| 93 | + toolPackage.Id, |
| 94 | + toolPackage.Version, |
| 95 | + NuGetFramework.Parse(targetFramework), |
| 96 | + Constants.AnyRid, |
| 97 | + toolPackage.Command.Name), |
| 98 | + toolPackage.Command)], |
| 99 | + message: string.Format( |
| 100 | + CliCommandStrings.RestoreSuccessful, |
| 101 | + package.PackageId, |
| 102 | + package.Version.ToNormalizedString(), |
| 103 | + string.Join(" ", package.CommandNames))); |
| 104 | + } |
| 105 | + catch (ToolPackageException e) |
| 106 | + { |
| 107 | + return ToolRestoreResult.Failure(package.PackageId, e); |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + private static bool ManifestCommandMatchesActualInPackage( |
| 112 | + ToolCommandName[] commandsFromManifest, |
| 113 | + IReadOnlyList<ToolCommand> toolPackageCommands) |
| 114 | + { |
| 115 | + ToolCommandName[] commandsFromPackage = [.. toolPackageCommands.Select(t => t.Name)]; |
| 116 | + foreach (var command in commandsFromManifest) |
| 117 | + { |
| 118 | + if (!commandsFromPackage.Contains(command)) |
| 119 | + { |
| 120 | + return false; |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + foreach (var command in commandsFromPackage) |
| 125 | + { |
| 126 | + if (!commandsFromManifest.Contains(command)) |
| 127 | + { |
| 128 | + return false; |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + return true; |
| 133 | + } |
| 134 | + |
| 135 | + public bool PackageHasBeenRestored( |
| 136 | + ToolManifestPackage package, |
| 137 | + string targetFramework) |
| 138 | + { |
| 139 | + var sampleRestoredCommandIdentifierOfThePackage = new RestoredCommandIdentifier( |
| 140 | + package.PackageId, |
| 141 | + package.Version, |
| 142 | + NuGetFramework.Parse(targetFramework), |
| 143 | + Constants.AnyRid, |
| 144 | + package.CommandNames.First()); |
| 145 | + |
| 146 | + return _localToolsResolverCache.TryLoad( |
| 147 | + sampleRestoredCommandIdentifierOfThePackage, |
| 148 | + out var toolCommand) |
| 149 | + && _fileSystem.File.Exists(toolCommand.Executable.Value); |
| 150 | + } |
| 151 | + |
| 152 | + private static string JoinBySpaceWithQuote(IEnumerable<object> objects) |
| 153 | + { |
| 154 | + return string.Join(" ", objects.Select(o => $"\"{o.ToString()}\"")); |
| 155 | + } |
| 156 | + |
| 157 | + private static VersionRange ToVersionRangeWithOnlyOneVersion(NuGetVersion version) |
| 158 | + { |
| 159 | + return new VersionRange( |
| 160 | + version, |
| 161 | + includeMinVersion: true, |
| 162 | + maxVersion: version, |
| 163 | + includeMaxVersion: true); |
| 164 | + } |
| 165 | +} |
| 166 | + |
0 commit comments