/
InstallCommand.cs
446 lines (369 loc) · 18.6 KB
/
InstallCommand.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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
// 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.
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using NuGet.Commands;
using NuGet.Common;
using NuGet.Configuration;
using NuGet.Frameworks;
using NuGet.PackageManagement;
using NuGet.Packaging;
using NuGet.Packaging.Core;
using NuGet.Packaging.PackageExtraction;
using NuGet.Packaging.Signing;
using NuGet.ProjectManagement;
using NuGet.Protocol;
using NuGet.Protocol.Core.Types;
using NuGet.Resolver;
using NuGet.Versioning;
namespace NuGet.CommandLine
{
[Command(typeof(NuGetCommand), "install", "InstallCommandDescription",
MinArgs = 0, MaxArgs = 1, UsageSummaryResourceName = "InstallCommandUsageSummary",
UsageDescriptionResourceName = "InstallCommandUsageDescription",
UsageExampleResourceName = "InstallCommandUsageExamples")]
public class InstallCommand : DownloadCommandBase
{
[Option(typeof(NuGetCommand), "InstallCommandOutputDirDescription")]
public string OutputDirectory { get; set; }
[Option(typeof(NuGetCommand), "InstallCommandVersionDescription")]
public string Version { get; set; }
[Option(typeof(NuGetCommand), "InstallCommandDependencyVersion")]
public string DependencyVersion { get; set; }
[Option(typeof(NuGetCommand), "InstallCommandFrameworkDescription")]
public string Framework { get; set; }
[Option(typeof(NuGetCommand), "InstallCommandExcludeVersionDescription", AltName = "x")]
public bool ExcludeVersion { get; set; }
[Option(typeof(NuGetCommand), "InstallCommandPrerelease")]
public bool Prerelease { get; set; }
[Option(typeof(NuGetCommand), "InstallCommandRequireConsent")]
public bool RequireConsent { get; set; }
[Option(typeof(NuGetCommand), "InstallCommandSolutionDirectory")]
public string SolutionDirectory { get; set; }
[ImportingConstructor]
protected internal InstallCommand()
{
}
public override Task ExecuteCommandAsync()
{
// On mono, parallel builds are broken for some reason. See https://gist.github.com/4201936 for the errors
// That are thrown.
DisableParallelProcessing |= RuntimeEnvironmentHelper.IsMono;
if (DisableParallelProcessing)
{
HttpSourceResourceProvider.Throttle = SemaphoreSlimThrottle.CreateBinarySemaphore();
}
CalculateEffectivePackageSaveMode();
CalculateEffectiveSettings();
var installPath = Path.GetFullPath(ResolveInstallPath());
var configFilePath = Path.GetFullPath(Arguments.Count == 0 ? Constants.PackageReferenceFile : Arguments[0]);
var configFileName = Path.GetFileName(configFilePath);
// If the first argument is a packages.xxx.config file, install everything it lists
// Otherwise, treat the first argument as a package Id
if (CommandLineUtility.IsValidConfigFileName(configFileName))
{
Prerelease = true;
// display opt-out message if needed
if (Console != null && RequireConsent &&
new PackageRestoreConsent(Settings).IsGranted)
{
var message = string.Format(
CultureInfo.CurrentCulture,
LocalizedResourceManager.GetString("RestoreCommandPackageRestoreOptOutMessage"),
NuGetResources.PackageRestoreConsentCheckBoxText.Replace("&", ""));
Console.WriteLine(message);
}
return PerformV2RestoreAsync(configFilePath, installPath);
}
else
{
var packageId = Arguments[0];
var version = Version != null ? new NuGetVersion(Version) : null;
return InstallPackageAsync(packageId, version, installPath);
}
}
private void CalculateEffectiveSettings()
{
// If the SolutionDir is specified, use the .nuget directory under it to determine the solution-level settings
if (!string.IsNullOrEmpty(SolutionDirectory))
{
string path;
try
{
path = Path.Combine(SolutionDirectory, NuGetConstants.NuGetSolutionSettingsFolder);
}
catch (ArgumentException e)
{
throw new ArgumentException(string.Format(CultureInfo.CurrentCulture,
LocalizedResourceManager.GetString("Error_InvalidSolutionDirectory"),
SolutionDirectory),
e);
}
var solutionSettingsFile = Path.GetFullPath(path);
Settings = Configuration.Settings.LoadDefaultSettings(
solutionSettingsFile,
configFileName: null,
machineWideSettings: MachineWideSettings);
// Recreate the source provider and credential provider
SourceProvider = PackageSourceBuilder.CreateSourceProvider(Settings);
SetDefaultCredentialProvider();
}
}
internal string ResolveInstallPath()
{
if (!string.IsNullOrEmpty(OutputDirectory))
{
// Use the OutputDirectory if specified.
return OutputDirectory;
}
var installPath = SettingsUtility.GetRepositoryPath(Settings);
if (!string.IsNullOrEmpty(installPath))
{
// If a value is specified in config, use that.
return installPath;
}
if (!string.IsNullOrEmpty(SolutionDirectory))
{
// For package restore scenarios, deduce the path of the packages directory from the solution directory.
return Path.Combine(SolutionDirectory, CommandLineConstants.PackagesDirectoryName);
}
// Use the current directory as output.
return CurrentDirectory;
}
private async Task PerformV2RestoreAsync(string packagesConfigFilePath, string installPath)
{
var sourceRepositoryProvider = GetSourceRepositoryProvider();
var nuGetPackageManager = new NuGetPackageManager(sourceRepositoryProvider, Settings, installPath, ExcludeVersion);
var installedPackageReferences = GetInstalledPackageReferences(packagesConfigFilePath);
var packageRestoreData = installedPackageReferences.Select(reference =>
new PackageRestoreData(
reference,
new[] { packagesConfigFilePath },
isMissing: true));
var packageSources = GetPackageSources(Settings);
Console.PrintPackageSources(packageSources);
var failedEvents = new ConcurrentQueue<PackageRestoreFailedEventArgs>();
var packageRestoreContext = new PackageRestoreContext(
nuGetPackageManager,
packageRestoreData,
CancellationToken.None,
packageRestoredEvent: null,
packageRestoreFailedEvent: (sender, args) => { failedEvents.Enqueue(args); },
sourceRepositories: packageSources.Select(sourceRepositoryProvider.CreateRepository),
maxNumberOfParallelTasks: DisableParallelProcessing ? 1 : PackageManagementConstants.DefaultMaxDegreeOfParallelism,
enableNuGetAudit: true,
restoreAuditProperties: new(),
logger: Console);
var packageSaveMode = Packaging.PackageSaveMode.Defaultv2;
if (EffectivePackageSaveMode != Packaging.PackageSaveMode.None)
{
packageSaveMode = EffectivePackageSaveMode;
}
var missingPackageReferences = installedPackageReferences.Any(reference => !nuGetPackageManager.PackageExistsInPackagesFolder(reference.PackageIdentity, packageSaveMode));
if (!missingPackageReferences)
{
var message = string.Format(
CultureInfo.CurrentCulture,
LocalizedResourceManager.GetString("InstallCommandNothingToInstall"),
packagesConfigFilePath);
Console.LogMinimal(message);
}
using (var cacheContext = new SourceCacheContext())
{
cacheContext.NoCache = NoCache || NoHttpCache;
cacheContext.DirectDownload = DirectDownload;
var clientPolicyContext = ClientPolicyContext.GetClientPolicy(Settings, Console);
var projectContext = new ConsoleProjectContext(Console)
{
PackageExtractionContext = new PackageExtractionContext(
packageSaveMode,
PackageExtractionBehavior.XmlDocFileSaveMode,
clientPolicyContext,
Console)
};
var packageSourceMapping = PackageSourceMapping.GetPackageSourceMapping(Settings);
var downloadContext = new PackageDownloadContext(cacheContext, installPath, DirectDownload, packageSourceMapping)
{
ClientPolicyContext = clientPolicyContext
};
var result = await PackageRestoreManager.RestoreMissingPackagesAsync(
packageRestoreContext,
projectContext,
downloadContext);
if (downloadContext.DirectDownload)
{
GetDownloadResultUtility.CleanUpDirectDownloads(downloadContext);
}
// Use failure count to determine errors. result.Restored will be false for noop restores.
if (failedEvents.Count > 0)
{
// Log errors if they exist
foreach (var message in failedEvents.Select(e => new RestoreLogMessage(LogLevel.Error, NuGetLogCode.Undefined, e.Exception.Message)))
{
await Console.LogAsync(message);
}
throw new ExitCodeException(1);
}
}
}
private CommandLineSourceRepositoryProvider GetSourceRepositoryProvider()
{
return new CommandLineSourceRepositoryProvider(SourceProvider);
}
private async Task InstallPackageAsync(
string packageId,
NuGetVersion version,
string installPath)
{
if (version == null)
{
// Avoid searching for the highest version in the global packages folder,
// it needs to come from the feeds instead. Once found it may come from
// the global packages folder unless NoCache is true.
ExcludeCacheAsSource = true;
}
var framework = GetTargetFramework();
// Create the project and set the framework if available.
var project = new InstallCommandProject(
root: installPath,
packagePathResolver: new PackagePathResolver(installPath, !ExcludeVersion),
targetFramework: framework);
var sourceRepositoryProvider = GetSourceRepositoryProvider();
var packageManager = new NuGetPackageManager(sourceRepositoryProvider, Settings, installPath);
var packageSources = GetPackageSources(Settings);
var primaryRepositories = packageSources.Select(sourceRepositoryProvider.CreateRepository);
Console.PrintPackageSources(packageSources);
var allowPrerelease = Prerelease || (version != null && version.IsPrerelease);
var dependencyBehavior = DependencyBehaviorHelper.GetDependencyBehavior(DependencyBehavior.Lowest, DependencyVersion, Settings);
using (var sourceCacheContext = new SourceCacheContext())
{
var resolutionContext = new ResolutionContext(
dependencyBehavior,
includePrelease: allowPrerelease,
includeUnlisted: false,
versionConstraints: VersionConstraints.None,
gatherCache: new GatherCache(),
sourceCacheContext: sourceCacheContext);
if (version == null)
{
// Write out a helpful message before the http messages are shown
Console.Log(LogLevel.Minimal, string.Format(
CultureInfo.CurrentCulture,
LocalizedResourceManager.GetString("InstallPackageMessage"), packageId, installPath));
// Find the latest version using NuGetPackageManager
var resolvePackage = await NuGetPackageManager.GetLatestVersionAsync(
packageId,
project,
resolutionContext,
primaryRepositories,
Console,
CancellationToken.None);
if (resolvePackage == null || resolvePackage.LatestVersion == null)
{
var message = string.Format(
CultureInfo.CurrentCulture,
LocalizedResourceManager.GetString("InstallCommandUnableToFindPackage"),
packageId);
throw new CommandException(message);
}
version = resolvePackage.LatestVersion;
}
// Get a list of packages already in the folder.
var installedPackages = await project.GetFolderPackagesAsync(CancellationToken.None);
// Find existing versions of the package
var alreadyInstalledVersions = new HashSet<NuGetVersion>(installedPackages
.Where(e => StringComparer.OrdinalIgnoreCase.Equals(packageId, e.PackageIdentity.Id))
.Select(e => e.PackageIdentity.Version));
var packageIdentity = new PackageIdentity(packageId, version);
var PackageSaveMode = Packaging.PackageSaveMode.Defaultv2;
if (EffectivePackageSaveMode != Packaging.PackageSaveMode.None)
{
PackageSaveMode = EffectivePackageSaveMode;
}
var exactVersionExists = project.PackageExists(packageIdentity, PackageSaveMode);
var higherVersionExists = alreadyInstalledVersions.Any(e => e >= version);
// For SxS skip if exact version exists. For non-SxS skip if a higher version exists.
var skipInstall = exactVersionExists || (ExcludeVersion && higherVersionExists);
if (skipInstall)
{
string message;
if (exactVersionExists)
{
message = string.Format(
CultureInfo.CurrentCulture,
LocalizedResourceManager.GetString("InstallCommandPackageAlreadyExists"),
packageIdentity);
}
else
{
var higherPackageIdentity = new PackageIdentity(packageId, alreadyInstalledVersions.FirstOrDefault(e => e >= version));
message = string.Format(
CultureInfo.CurrentCulture,
LocalizedResourceManager.GetString("InstallCommandHigherVersionAlreadyExists"),
packageIdentity, higherPackageIdentity);
}
Console.LogMinimal(message);
}
else
{
var clientPolicyContext = ClientPolicyContext.GetClientPolicy(Settings, Console);
var projectContext = new ConsoleProjectContext(Console)
{
PackageExtractionContext = new PackageExtractionContext(
PackageSaveMode,
PackageExtractionBehavior.XmlDocFileSaveMode,
clientPolicyContext,
Console)
};
resolutionContext.SourceCacheContext.NoCache = NoCache || NoHttpCache;
resolutionContext.SourceCacheContext.DirectDownload = DirectDownload;
var packageSourceMapping = PackageSourceMapping.GetPackageSourceMapping(Settings);
var downloadContext = new PackageDownloadContext(resolutionContext.SourceCacheContext, installPath, DirectDownload, packageSourceMapping)
{
ClientPolicyContext = clientPolicyContext
};
await packageManager.InstallPackageAsync(
project,
packageIdentity,
resolutionContext,
projectContext,
downloadContext,
primaryRepositories,
Enumerable.Empty<SourceRepository>(),
CancellationToken.None);
if (downloadContext.DirectDownload)
{
GetDownloadResultUtility.CleanUpDirectDownloads(downloadContext);
}
}
}
}
/// <summary>
/// Parse the Framework parameter or use Any as the default framework.
/// </summary>
private NuGetFramework GetTargetFramework()
{
var targetFramework = NuGetFramework.AnyFramework;
if (!string.IsNullOrEmpty(Framework))
{
targetFramework = NuGetFramework.Parse(Framework);
}
if (targetFramework.IsUnsupported)
{
// Fail with a helpful message if the user provided an invalid framework.
var message = string.Format(CultureInfo.CurrentCulture,
LocalizedResourceManager.GetString("UnsupportedFramework"),
Framework);
throw new ArgumentException(message);
}
return targetFramework;
}
}
}