|
| 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.CommandLine; |
| 5 | +using Microsoft.DotNet.Cli.CommandFactory; |
| 6 | +using Microsoft.DotNet.Cli.CommandFactory.CommandResolution; |
| 7 | +using Microsoft.DotNet.Cli.Commands.Tool.Install; |
| 8 | +using Microsoft.DotNet.Cli.Commands.Tool.Restore; |
| 9 | +using Microsoft.DotNet.Cli.Commands.Tool.Run; |
| 10 | +using Microsoft.DotNet.Cli.Extensions; |
| 11 | +using Microsoft.DotNet.Cli.NuGetPackageDownloader; |
| 12 | +using Microsoft.DotNet.Cli.ToolManifest; |
| 13 | +using Microsoft.DotNet.Cli.ToolPackage; |
| 14 | +using Microsoft.DotNet.Cli.Utils; |
| 15 | +using Microsoft.DotNet.Cli.Utils.Extensions; |
| 16 | + |
| 17 | +using Microsoft.Extensions.EnvironmentAbstractions; |
| 18 | +using NuGet.Common; |
| 19 | +using NuGet.Configuration; |
| 20 | +using NuGet.Packaging.Core; |
| 21 | +using NuGet.Versioning; |
| 22 | + |
| 23 | + |
| 24 | +namespace Microsoft.DotNet.Cli.Commands.Tool.Execute; |
| 25 | + |
| 26 | +internal class ToolExecuteCommand(ParseResult result, ToolManifestFinder? toolManifestFinder = null, string? currentWorkingDirectory = null) : CommandBase(result) |
| 27 | +{ |
| 28 | + const int ERROR_CANCELLED = 1223; // Windows error code for "Operation canceled by user" |
| 29 | + |
| 30 | + private readonly PackageIdentity _packageToolIdentityArgument = result.GetRequiredValue(ToolExecuteCommandParser.PackageIdentityArgument); |
| 31 | + private readonly IEnumerable<string> _forwardArguments = result.GetValue(ToolExecuteCommandParser.CommandArgument) ?? Enumerable.Empty<string>(); |
| 32 | + private readonly bool _allowRollForward = result.GetValue(ToolExecuteCommandParser.RollForwardOption); |
| 33 | + private readonly string? _configFile = result.GetValue(ToolExecuteCommandParser.ConfigOption); |
| 34 | + private readonly string[] _sources = result.GetValue(ToolExecuteCommandParser.SourceOption) ?? []; |
| 35 | + private readonly string[] _addSource = result.GetValue(ToolExecuteCommandParser.AddSourceOption) ?? []; |
| 36 | + private readonly bool _interactive = result.GetValue(ToolExecuteCommandParser.InteractiveOption); |
| 37 | + private readonly VerbosityOptions _verbosity = result.GetValue(ToolExecuteCommandParser.VerbosityOption); |
| 38 | + private readonly bool _yes = result.GetValue(ToolExecuteCommandParser.YesOption); |
| 39 | + private readonly IToolPackageDownloader _toolPackageDownloader = ToolPackageFactory.CreateToolPackageStoresAndDownloader().downloader; |
| 40 | + |
| 41 | + private readonly RestoreActionConfig _restoreActionConfig = new RestoreActionConfig(DisableParallel: result.GetValue(ToolCommandRestorePassThroughOptions.DisableParallelOption), |
| 42 | + NoCache: result.GetValue(ToolCommandRestorePassThroughOptions.NoCacheOption) || result.GetValue(ToolCommandRestorePassThroughOptions.NoHttpCacheOption), |
| 43 | + IgnoreFailedSources: result.GetValue(ToolCommandRestorePassThroughOptions.IgnoreFailedSourcesOption), |
| 44 | + Interactive: result.GetValue(ToolExecuteCommandParser.InteractiveOption)); |
| 45 | + |
| 46 | + private readonly ToolManifestFinder _toolManifestFinder = toolManifestFinder ?? new ToolManifestFinder(new DirectoryPath(currentWorkingDirectory ?? Directory.GetCurrentDirectory())); |
| 47 | + |
| 48 | + public override int Execute() |
| 49 | + { |
| 50 | + VersionRange versionRange = _parseResult.GetVersionRange(); |
| 51 | + PackageId packageId = new PackageId(_packageToolIdentityArgument.Id); |
| 52 | + |
| 53 | + // Look in local tools manifest first, but only if version is not specified |
| 54 | + if (versionRange == null) |
| 55 | + { |
| 56 | + var localToolsResolverCache = new LocalToolsResolverCache(); |
| 57 | + |
| 58 | + if (_toolManifestFinder.TryFindPackageId(packageId, out var toolManifestPackage)) |
| 59 | + { |
| 60 | + var toolPackageRestorer = new ToolPackageRestorer( |
| 61 | + _toolPackageDownloader, |
| 62 | + _sources, |
| 63 | + overrideSources: [], |
| 64 | + _verbosity, |
| 65 | + _restoreActionConfig, |
| 66 | + localToolsResolverCache, |
| 67 | + new FileSystemWrapper()); |
| 68 | + |
| 69 | + var restoreResult = toolPackageRestorer.InstallPackage(toolManifestPackage, _configFile == null ? null : new FilePath(_configFile)); |
| 70 | + |
| 71 | + if (!restoreResult.IsSuccess) |
| 72 | + { |
| 73 | + Reporter.Error.WriteLine(restoreResult.Message.Red()); |
| 74 | + return 1; |
| 75 | + } |
| 76 | + |
| 77 | + var localToolsCommandResolver = new LocalToolsCommandResolver( |
| 78 | + _toolManifestFinder, |
| 79 | + localToolsResolverCache); |
| 80 | + |
| 81 | + return ToolRunCommand.ExecuteCommand(localToolsCommandResolver, toolManifestPackage.CommandNames.Single().Value, _forwardArguments, _allowRollForward); |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + var packageLocation = new PackageLocation( |
| 86 | + nugetConfig: _configFile != null ? new(_configFile) : null, |
| 87 | + sourceFeedOverrides: _sources, |
| 88 | + additionalFeeds: _addSource); |
| 89 | + |
| 90 | + (var bestVersion, var packageSource) = _toolPackageDownloader.GetNuGetVersion(packageLocation, packageId, _verbosity, versionRange, _restoreActionConfig); |
| 91 | + |
| 92 | + IToolPackage toolPackage; |
| 93 | + |
| 94 | + // TargetFramework is null, which means to use the current framework. Global tools can override the target framework to use (or select assets for), |
| 95 | + // but we don't support this for local or one-shot tools. |
| 96 | + if (!_toolPackageDownloader.TryGetDownloadedTool(packageId, bestVersion, targetFramework: null, out toolPackage)) |
| 97 | + { |
| 98 | + if (!UserAgreedToRunFromSource(packageId, bestVersion, packageSource)) |
| 99 | + { |
| 100 | + if (_interactive) |
| 101 | + { |
| 102 | + Reporter.Error.WriteLine(CliCommandStrings.ToolDownloadCanceled.Red().Bold()); |
| 103 | + return ERROR_CANCELLED; |
| 104 | + } |
| 105 | + else |
| 106 | + { |
| 107 | + Reporter.Error.WriteLine(CliCommandStrings.ToolDownloadNeedsConfirmation.Red().Bold()); |
| 108 | + return 1; |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + // We've already determined which source we will use and displayed that in a confirmation message to the user. |
| 113 | + // So set the package location here to override the source feeds to just the source we already resolved to. |
| 114 | + // This does mean that we won't work with feeds that have a primary package but where the RID-specific packages are on |
| 115 | + // other feeds, but this is probably OK. |
| 116 | + var downloadPackageLocation = new PackageLocation( |
| 117 | + nugetConfig: _configFile != null ? new(_configFile) : null, |
| 118 | + sourceFeedOverrides: [packageSource.Source], |
| 119 | + additionalFeeds: _addSource); |
| 120 | + |
| 121 | + toolPackage = _toolPackageDownloader.InstallPackage( |
| 122 | + downloadPackageLocation, |
| 123 | + packageId: packageId, |
| 124 | + verbosity: _verbosity, |
| 125 | + versionRange: new VersionRange(bestVersion, true, bestVersion, true), |
| 126 | + isGlobalToolRollForward: false, |
| 127 | + restoreActionConfig: _restoreActionConfig); |
| 128 | + } |
| 129 | + |
| 130 | + var commandSpec = ToolCommandSpecCreator.CreateToolCommandSpec(toolPackage.Command.Name.Value, toolPackage.Command.Executable.Value, toolPackage.Command.Runner, _allowRollForward, _forwardArguments); |
| 131 | + var command = CommandFactoryUsingResolver.Create(commandSpec); |
| 132 | + var result = command.Execute(); |
| 133 | + return result.ExitCode; |
| 134 | + } |
| 135 | + |
| 136 | + private bool UserAgreedToRunFromSource(PackageId packageId, NuGetVersion version, PackageSource source) |
| 137 | + { |
| 138 | + if (_yes) |
| 139 | + { |
| 140 | + return true; |
| 141 | + } |
| 142 | + |
| 143 | + if (!_interactive) |
| 144 | + { |
| 145 | + return false; |
| 146 | + } |
| 147 | + |
| 148 | + string promptMessage = string.Format(CliCommandStrings.ToolDownloadConfirmationPrompt, packageId, version.ToString(), source.Source); |
| 149 | + |
| 150 | + static string AddPromptOptions(string message) |
| 151 | + { |
| 152 | + return $"{message} [{CliCommandStrings.ConfirmationPromptYesValue}/{CliCommandStrings.ConfirmationPromptNoValue}] ({CliCommandStrings.ConfirmationPromptYesValue}): "; |
| 153 | + } |
| 154 | + |
| 155 | + Console.Write(AddPromptOptions(promptMessage)); |
| 156 | + |
| 157 | + static bool KeyMatches(ConsoleKeyInfo pressedKey, string valueKey) |
| 158 | + { |
| 159 | + // Apparently you can't do invariant case insensitive comparison on a char directly, so we have to convert it to a string. |
| 160 | + // The resource string should be a single character, but we take the first character just to be sure. |
| 161 | + return pressedKey.KeyChar.ToString().ToLowerInvariant().Equals( |
| 162 | + valueKey.ToLowerInvariant().Substring(0, 1)); |
| 163 | + } |
| 164 | + |
| 165 | + while (true) |
| 166 | + { |
| 167 | + var key = Console.ReadKey(); |
| 168 | + Console.WriteLine(); |
| 169 | + if (key.Key == ConsoleKey.Enter || KeyMatches(key, CliCommandStrings.ConfirmationPromptYesValue)) |
| 170 | + { |
| 171 | + return true; |
| 172 | + } |
| 173 | + if (key.Key == ConsoleKey.Escape || KeyMatches(key, CliCommandStrings.ConfirmationPromptNoValue)) |
| 174 | + { |
| 175 | + return false; |
| 176 | + } |
| 177 | + |
| 178 | + Console.Write(AddPromptOptions(string.Format(CliCommandStrings.ConfirmationPromptInvalidChoiceMessage, CliCommandStrings.ConfirmationPromptYesValue, CliCommandStrings.ConfirmationPromptNoValue))); |
| 179 | + } |
| 180 | + } |
| 181 | +} |
0 commit comments