|
| 1 | +// Copyright (c) .NET Foundation. All rights reserved. |
| 2 | +// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. |
| 3 | +#nullable disable |
| 4 | + |
| 5 | +using System; |
| 6 | +using System.Collections.Generic; |
| 7 | +using System.ComponentModel.Composition; |
| 8 | +using System.Diagnostics.CodeAnalysis; |
| 9 | +using System.Globalization; |
| 10 | +using System.IO; |
| 11 | +using System.Linq; |
| 12 | +using System.Threading.Tasks; |
| 13 | +using NuGet.Common; |
| 14 | +using NuGet.Credentials; |
| 15 | +using NuGet.Protocol; |
| 16 | +using NuGet.Protocol.Core.Types; |
| 17 | +using NuGet.Protocol.Plugins; |
| 18 | + |
| 19 | +namespace NuGet.CommandLine |
| 20 | +{ |
| 21 | + public abstract class Command : ICommand |
| 22 | + { |
| 23 | + private const string CommandSuffix = "Command"; |
| 24 | + private CommandAttribute _commandAttribute; |
| 25 | + private string _currentDirectory; |
| 26 | + |
| 27 | + protected Command() |
| 28 | + { |
| 29 | + Arguments = new List<string>(); |
| 30 | + } |
| 31 | + |
| 32 | + public IList<string> Arguments { get; private set; } |
| 33 | + |
| 34 | + [Import] |
| 35 | + public IConsole Console { get; set; } |
| 36 | + |
| 37 | + [Import] |
| 38 | + public HelpCommand HelpCommand { get; set; } |
| 39 | + |
| 40 | + [Import] |
| 41 | + public ICommandManager Manager { get; set; } |
| 42 | + |
| 43 | + [Import] |
| 44 | + public Configuration.IMachineWideSettings MachineWideSettings { get; set; } |
| 45 | + |
| 46 | + [Option(typeof(NuGetCommand), "Option_Help", AltName = "?")] |
| 47 | + public bool Help { get; set; } |
| 48 | + |
| 49 | + [Option(typeof(NuGetCommand), "Option_Verbosity")] |
| 50 | + public Verbosity Verbosity { get; set; } |
| 51 | + |
| 52 | + [Option(typeof(NuGetCommand), "Option_NonInteractive")] |
| 53 | + public bool NonInteractive { get; set; } |
| 54 | + |
| 55 | + [Option(typeof(NuGetCommand), "Option_ConfigFile")] |
| 56 | + public string ConfigFile { get; set; } |
| 57 | + |
| 58 | + [Option(typeof(NuGetCommand), "Option_ForceEnglishOutput")] |
| 59 | + public bool ForceEnglishOutput { get; set; } |
| 60 | + |
| 61 | + protected Configuration.ICredentialService CredentialService { get; private set; } |
| 62 | + |
| 63 | + public DeprecatedCommandAttribute DeprecatedCommandAttribute |
| 64 | + { |
| 65 | + get |
| 66 | + { |
| 67 | + var deprecatedAttrs = GetType().GetCustomAttributes(typeof(DeprecatedCommandAttribute), false); |
| 68 | + |
| 69 | + if (deprecatedAttrs.Length > 0) |
| 70 | + { |
| 71 | + return deprecatedAttrs[0] as DeprecatedCommandAttribute; |
| 72 | + } |
| 73 | + |
| 74 | + return null; |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + public string CurrentDirectory |
| 79 | + { |
| 80 | + get |
| 81 | + { |
| 82 | + return _currentDirectory ?? Directory.GetCurrentDirectory(); |
| 83 | + } |
| 84 | + set |
| 85 | + { |
| 86 | + _currentDirectory = value; |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + protected internal Configuration.ISettings Settings { get; set; } |
| 91 | + |
| 92 | + protected internal Configuration.IPackageSourceProvider SourceProvider { get; set; } |
| 93 | + |
| 94 | + private Lazy<MsBuildToolset> MsBuildToolset |
| 95 | + { |
| 96 | + get |
| 97 | + { |
| 98 | + if (_defaultMsBuildToolset == null) |
| 99 | + { |
| 100 | + _defaultMsBuildToolset = MsBuildUtility.GetMsBuildDirectoryFromMsBuildPath(null, null, Console); |
| 101 | + |
| 102 | + } |
| 103 | + return _defaultMsBuildToolset; |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + private Lazy<MsBuildToolset> _defaultMsBuildToolset; |
| 108 | + |
| 109 | + public CommandAttribute CommandAttribute |
| 110 | + { |
| 111 | + get |
| 112 | + { |
| 113 | + if (_commandAttribute == null) |
| 114 | + { |
| 115 | + _commandAttribute = GetCommandAttribute(); |
| 116 | + } |
| 117 | + return _commandAttribute; |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + public virtual bool IncludedInHelp(string optionName) |
| 122 | + { |
| 123 | + return true; |
| 124 | + } |
| 125 | + |
| 126 | + public void Execute() |
| 127 | + { |
| 128 | + if (Help) |
| 129 | + { |
| 130 | + if (DeprecatedCommandAttribute != null) |
| 131 | + { |
| 132 | + var deprecationMessage = DeprecatedCommandAttribute.GetDeprecationMessage(CommandAttribute.CommandName); |
| 133 | + Console.WriteWarning(deprecationMessage); |
| 134 | + } |
| 135 | + |
| 136 | + HelpCommand.ViewHelpForCommand(CommandAttribute.CommandName); |
| 137 | + } |
| 138 | + else |
| 139 | + { |
| 140 | + if (string.IsNullOrEmpty(ConfigFile)) |
| 141 | + { |
| 142 | + string configFileName = null; |
| 143 | + |
| 144 | + var packCommand = this as PackCommand; |
| 145 | + if (packCommand != null && !string.IsNullOrEmpty(packCommand.ConfigFile)) |
| 146 | + { |
| 147 | + configFileName = packCommand.ConfigFile; |
| 148 | + } |
| 149 | + |
| 150 | + Settings = Configuration.Settings.LoadDefaultSettings( |
| 151 | + CurrentDirectory, |
| 152 | + configFileName: configFileName, |
| 153 | + machineWideSettings: MachineWideSettings); |
| 154 | + } |
| 155 | + else |
| 156 | + { |
| 157 | + var configFileFullPath = Path.GetFullPath(ConfigFile); |
| 158 | + var directory = Path.GetDirectoryName(configFileFullPath); |
| 159 | + var configFileName = Path.GetFileName(configFileFullPath); |
| 160 | + Settings = Configuration.Settings.LoadDefaultSettings( |
| 161 | + directory, |
| 162 | + configFileName, |
| 163 | + MachineWideSettings); |
| 164 | + } |
| 165 | + |
| 166 | + SourceProvider = PackageSourceBuilder.CreateSourceProvider(Settings); |
| 167 | + |
| 168 | + SetDefaultCredentialProvider(); |
| 169 | + |
| 170 | + UserAgent.SetUserAgentString(new UserAgentStringBuilder(CommandLineConstants.UserAgent)); |
| 171 | + |
| 172 | + if (DeprecatedCommandAttribute != null) |
| 173 | + { |
| 174 | + var deprecationMessage = DeprecatedCommandAttribute.GetDeprecationMessage(CommandAttribute.CommandName); |
| 175 | + Console.WriteWarning(deprecationMessage); |
| 176 | + } |
| 177 | + |
| 178 | + OutputNuGetVersion(); |
| 179 | + ExecuteCommandAsync().GetAwaiter().GetResult(); |
| 180 | + } |
| 181 | + } |
| 182 | + |
| 183 | + /// <summary> |
| 184 | + /// Outputs the current NuGet version (by default, only when vebosity is detailed). |
| 185 | + /// </summary> |
| 186 | + private void OutputNuGetVersion() |
| 187 | + { |
| 188 | + if (ShouldOutputNuGetVersion) |
| 189 | + { |
| 190 | + var assemblyName = typeof(Command).Assembly.GetName(); |
| 191 | + var assemblyLocation = typeof(Command).Assembly.Location; |
| 192 | + var version = System.Diagnostics.FileVersionInfo.GetVersionInfo(assemblyLocation).FileVersion; |
| 193 | + var message = string.Format( |
| 194 | + CultureInfo.CurrentCulture, |
| 195 | + LocalizedResourceManager.GetString("OutputNuGetVersion"), |
| 196 | + assemblyName.Name, |
| 197 | + version); |
| 198 | + Console.WriteLine(message); |
| 199 | + } |
| 200 | + } |
| 201 | + |
| 202 | + protected virtual bool ShouldOutputNuGetVersion |
| 203 | + { |
| 204 | + get { return Console.Verbosity == Verbosity.Detailed; } |
| 205 | + } |
| 206 | + |
| 207 | + protected virtual void SetDefaultCredentialProvider() |
| 208 | + { |
| 209 | + SetDefaultCredentialProvider(MsBuildToolset); |
| 210 | + } |
| 211 | + |
| 212 | + /// <summary> |
| 213 | + /// Set default credential provider for the HttpClient, which is used by V2 sources. |
| 214 | + /// Also set up authenticated proxy handling for V3 sources. |
| 215 | + /// </summary> |
| 216 | + protected void SetDefaultCredentialProvider(Lazy<MsBuildToolset> msbuildDirectory) |
| 217 | + { |
| 218 | + PluginDiscoveryUtility.InternalPluginDiscoveryRoot = new Lazy<string>(() => PluginDiscoveryUtility.GetInternalPluginRelativeToMSBuildDirectory(msbuildDirectory.Value.Path)); |
| 219 | + CredentialService = new CredentialService(new AsyncLazy<IEnumerable<ICredentialProvider>>(() => GetCredentialProvidersAsync()), NonInteractive, handlesDefaultCredentials: PreviewFeatureSettings.DefaultCredentialsAfterCredentialProviders); |
| 220 | + |
| 221 | + HttpHandlerResourceV3.CredentialService = new Lazy<Configuration.ICredentialService>(() => CredentialService); |
| 222 | + |
| 223 | + HttpHandlerResourceV3.CredentialsSuccessfullyUsed = (uri, credentials) => |
| 224 | + { |
| 225 | + }; |
| 226 | + } |
| 227 | + |
| 228 | + private async Task<IEnumerable<ICredentialProvider>> GetCredentialProvidersAsync() |
| 229 | + { |
| 230 | + var extensionLocator = new ExtensionLocator(); |
| 231 | + var providers = new List<ICredentialProvider>(); |
| 232 | + var pluginProviders = new PluginCredentialProviderBuilder(extensionLocator, Settings, Console) |
| 233 | + .BuildAll(Verbosity.ToString()) |
| 234 | + .ToList(); |
| 235 | + var securePluginProviders = await (new SecurePluginCredentialProviderBuilder(PluginManager.Instance, canShowDialog: true, logger: Console)).BuildAllAsync(); |
| 236 | + |
| 237 | + providers.Add(new SettingsCredentialProvider(SourceProvider, Console)); |
| 238 | + providers.AddRange(securePluginProviders); |
| 239 | + providers.AddRange(pluginProviders); |
| 240 | + |
| 241 | + if (pluginProviders.Any() || securePluginProviders.Any()) |
| 242 | + { |
| 243 | + if (PreviewFeatureSettings.DefaultCredentialsAfterCredentialProviders) |
| 244 | + { |
| 245 | + providers.Add(new DefaultNetworkCredentialsCredentialProvider()); |
| 246 | + } |
| 247 | + } |
| 248 | + providers.Add(new ConsoleCredentialProvider(Console)); |
| 249 | + |
| 250 | + return providers; |
| 251 | + } |
| 252 | + |
| 253 | + public virtual Task ExecuteCommandAsync() |
| 254 | + { |
| 255 | + ExecuteCommand(); |
| 256 | + return Task.CompletedTask; |
| 257 | + } |
| 258 | + |
| 259 | + public virtual void ExecuteCommand() |
| 260 | + { |
| 261 | + } |
| 262 | + |
| 263 | + [SuppressMessage("Microsoft.Design", "CA1024:UsePropertiesWhereAppropriate", Justification = "This method does quite a bit of processing.")] |
| 264 | + public virtual CommandAttribute GetCommandAttribute() |
| 265 | + { |
| 266 | + var type = GetType(); |
| 267 | + var attributes = type.GetCustomAttributes(typeof(CommandAttribute), true); |
| 268 | + var attribute = attributes.FirstOrDefault(); |
| 269 | + if (attribute != null) |
| 270 | + { |
| 271 | + return (CommandAttribute)attribute; |
| 272 | + } |
| 273 | + |
| 274 | + // Use the command name minus the suffix if present and default description |
| 275 | + var name = type.Name; |
| 276 | + var idx = name.LastIndexOf(CommandSuffix, StringComparison.OrdinalIgnoreCase); |
| 277 | + if (idx >= 0) |
| 278 | + { |
| 279 | + name = name.Substring(0, idx); |
| 280 | + } |
| 281 | + if (!string.IsNullOrEmpty(name)) |
| 282 | + { |
| 283 | + return new CommandAttribute(name, LocalizedResourceManager.GetString("DefaultCommandDescription")); |
| 284 | + } |
| 285 | + return null; |
| 286 | + } |
| 287 | + } |
| 288 | +} |
0 commit comments