-
Notifications
You must be signed in to change notification settings - Fork 674
/
Command.cs
230 lines (193 loc) · 7.8 KB
/
Command.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
extern alias CoreV2;
using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
using NuGet.Credentials;
using NuGet.Protocol;
using NuGet.Protocol.Core.Types;
namespace NuGet.CommandLine
{
public abstract class Command : ICommand
{
private const string CommandSuffix = "Command";
private CommandAttribute _commandAttribute;
private string _currentDirectory;
protected Command()
{
Arguments = new List<string>();
}
public IList<string> Arguments { get; private set; }
[Import]
public IConsole Console { get; set; }
[Import]
public HelpCommand HelpCommand { get; set; }
[Import]
public ICommandManager Manager { get; set; }
[Import]
public Configuration.IMachineWideSettings MachineWideSettings { get; set; }
[Option("help", AltName = "?")]
public bool Help { get; set; }
[Option(typeof(NuGetCommand), "Option_Verbosity")]
public Verbosity Verbosity { get; set; }
[Option(typeof(NuGetCommand), "Option_NonInteractive")]
public bool NonInteractive { get; set; }
[Option(typeof(NuGetCommand), "Option_ConfigFile")]
public string ConfigFile { get; set; }
[Option(typeof(NuGetCommand), "Option_ForceEnglishOutput")]
public bool ForceEnglishOutput { get; set; }
protected Configuration.ICredentialService CredentialService { get; private set; }
public string CurrentDirectory
{
get
{
return _currentDirectory ?? Directory.GetCurrentDirectory();
}
set
{
_currentDirectory = value;
}
}
protected internal Configuration.ISettings Settings { get; set; }
protected internal Configuration.IPackageSourceProvider SourceProvider { get; set; }
protected internal CoreV2.NuGet.IPackageRepositoryFactory RepositoryFactory { get; set; }
public CommandAttribute CommandAttribute
{
get
{
if (_commandAttribute == null)
{
_commandAttribute = GetCommandAttribute();
}
return _commandAttribute;
}
}
public virtual bool IncludedInHelp(string optionName)
{
return true;
}
public void Execute()
{
if (Help)
{
HelpCommand.ViewHelpForCommand(CommandAttribute.CommandName);
}
else
{
if (String.IsNullOrEmpty(ConfigFile))
{
Settings = Configuration.Settings.LoadDefaultSettings(
CurrentDirectory,
configFileName: null,
machineWideSettings: MachineWideSettings);
}
else
{
var configFileFullPath = Path.GetFullPath(ConfigFile);
var directory = Path.GetDirectoryName(configFileFullPath);
var configFileName = Path.GetFileName(configFileFullPath);
Settings = Configuration.Settings.LoadDefaultSettings(
directory,
configFileName,
MachineWideSettings);
}
SourceProvider = PackageSourceBuilder.CreateSourceProvider(Settings);
SetDefaultCredentialProvider();
RepositoryFactory = new CommandLineRepositoryFactory(Console);
UserAgent.SetUserAgentString(new UserAgentStringBuilder(CommandLineConstants.UserAgent));
OutputNuGetVersion();
ExecuteCommandAsync().Wait();
}
}
/// <summary>
/// Outputs the current NuGet version (by default, only when vebosity is detailed).
/// </summary>
private void OutputNuGetVersion()
{
if (ShouldOutputNuGetVersion)
{
var assemblyName = Assembly.GetExecutingAssembly().GetName();
var message = string.Format(
CultureInfo.CurrentCulture,
LocalizedResourceManager.GetString("OutputNuGetVersion"),
assemblyName.Name,
assemblyName.Version);
Console.WriteLine(message);
}
}
protected virtual bool ShouldOutputNuGetVersion
{
get { return Console.Verbosity == Verbosity.Detailed; }
}
/// <summary>
/// Set default credential provider for the HttpClient, which is used by V2 sources.
/// Also set up authenticated proxy handling for V3 sources.
/// </summary>
protected void SetDefaultCredentialProvider()
{
CredentialService = new CredentialService(GetCredentialProviders(), NonInteractive);
CoreV2.NuGet.HttpClient.DefaultCredentialProvider = new CredentialServiceAdapter(CredentialService);
HttpHandlerResourceV3.CredentialService = CredentialService;
HttpHandlerResourceV3.CredentialsSuccessfullyUsed = (uri, credentials) =>
{
// v2 stack credentials update
CoreV2.NuGet.CredentialStore.Instance.Add(uri, credentials);
};
}
private IEnumerable<NuGet.Credentials.ICredentialProvider> GetCredentialProviders()
{
var extensionLocator = new ExtensionLocator();
var providers = new List<Credentials.ICredentialProvider>();
var pluginProviders = new PluginCredentialProviderBuilder(extensionLocator, Settings, Console)
.BuildAll(Verbosity.ToString())
.ToList();
providers.Add(new CredentialProviderAdapter(new SettingsCredentialProvider(SourceProvider, Console)));
if (pluginProviders.Any())
{
providers.AddRange(pluginProviders);
if (PreviewFeatureSettings.DefaultCredentialsAfterCredentialProviders)
{
providers.Add(new DefaultCredentialsCredentialProvider());
}
}
providers.Add(new ConsoleCredentialProvider(Console));
return providers;
}
public virtual Task ExecuteCommandAsync()
{
ExecuteCommand();
return Task.FromResult(0);
}
public virtual void ExecuteCommand()
{
}
[SuppressMessage("Microsoft.Design", "CA1024:UsePropertiesWhereAppropriate", Justification = "This method does quite a bit of processing.")]
public virtual CommandAttribute GetCommandAttribute()
{
var attributes = GetType().GetCustomAttributes(typeof(CommandAttribute), true);
if (attributes.Any())
{
return (CommandAttribute)attributes.FirstOrDefault();
}
// Use the command name minus the suffix if present and default description
string name = GetType().Name;
int idx = name.LastIndexOf(CommandSuffix, StringComparison.OrdinalIgnoreCase);
if (idx >= 0)
{
name = name.Substring(0, idx);
}
if (!String.IsNullOrEmpty(name))
{
return new CommandAttribute(name, LocalizedResourceManager.GetString("DefaultCommandDescription"));
}
return null;
}
}
}