-
Notifications
You must be signed in to change notification settings - Fork 697
/
InstallCommand.cs
309 lines (254 loc) · 12.1 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
// 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.ComponentModel.Composition;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using NuGet.Common;
using NuGet.Configuration;
using NuGet.PackageManagement;
using NuGet.Packaging;
using NuGet.Packaging.Core;
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), "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()
{
// On mono, parallel builds are broken for some reason. See https://gist.github.com/4201936 for the errors
// That are thrown.
DisableParallelProcessing = RuntimeEnvironmentHelper.IsMono;
}
public override Task ExecuteCommandAsync()
{
if (DisableParallelProcessing)
{
HttpSourceResourceProvider.Throttle = SemaphoreSlimThrottle.CreateBinarySemaphore();
}
CalculateEffectivePackageSaveMode();
CalculateEffectiveSettings();
string installPath = ResolveInstallPath();
string configFilePath = Path.GetFullPath(Arguments.Count == 0 ? Constants.PackageReferenceFile : Arguments[0]);
string 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)
{
string message = String.Format(
CultureInfo.CurrentCulture,
LocalizedResourceManager.GetString("RestoreCommandPackageRestoreOptOutMessage"),
NuGetResources.PackageRestoreConsentCheckBoxText.Replace("&", ""));
Console.WriteLine(message);
}
return PerformV2Restore(configFilePath, installPath);
}
else
{
var packageId = Arguments[0];
var version = Version != null ? new NuGetVersion(Version) : null;
return InstallPackage(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))
{
var path = Path.Combine(SolutionDirectory.TrimEnd(Path.DirectorySeparatorChar), NuGetConstants.NuGetSolutionSettingsFolder);
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;
}
string 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 PerformV2Restore(string packagesConfigFilePath, string installPath)
{
var sourceRepositoryProvider = GetSourceRepositoryProvider();
var nuGetPackageManager = new NuGetPackageManager(sourceRepositoryProvider, Settings, installPath, ExcludeVersion);
var installedPackageReferences = GetInstalledPackageReferences(
packagesConfigFilePath,
allowDuplicatePackageIds: true);
var packageRestoreData = installedPackageReferences.Select(reference =>
new PackageRestoreData(
reference,
new[] { packagesConfigFilePath },
isMissing: true));
var packageSources = GetPackageSources(Settings);
Console.PrintPackageSources(packageSources);
var packageRestoreContext = new PackageRestoreContext(
nuGetPackageManager,
packageRestoreData,
CancellationToken.None,
packageRestoredEvent: null,
packageRestoreFailedEvent: null,
sourceRepositories: packageSources.Select(sourceRepositoryProvider.CreateRepository),
maxNumberOfParallelTasks: DisableParallelProcessing ? 1 : PackageManagementConstants.DefaultMaxDegreeOfParallelism);
var missingPackageReferences = installedPackageReferences.Where(reference =>
!nuGetPackageManager.PackageExistsInPackagesFolder(reference.PackageIdentity)).Any();
if (!missingPackageReferences)
{
var message = string.Format(
CultureInfo.CurrentCulture,
LocalizedResourceManager.GetString("InstallCommandNothingToInstall"),
packagesConfigFilePath);
Console.LogMinimal(message);
}
using (var cacheContext = new SourceCacheContext())
{
cacheContext.NoCache = NoCache;
cacheContext.DirectDownload = DirectDownload;
var downloadContext = new PackageDownloadContext(cacheContext, installPath, DirectDownload);
await PackageRestoreManager.RestoreMissingPackagesAsync(
packageRestoreContext,
new ConsoleProjectContext(Console),
downloadContext);
if (downloadContext.DirectDownload)
{
GetDownloadResultUtility.CleanUpDirectDownloads(downloadContext);
}
}
}
private CommandLineSourceRepositoryProvider GetSourceRepositoryProvider()
{
return new CommandLineSourceRepositoryProvider(SourceProvider);
}
private async Task InstallPackage(
string packageId,
NuGetVersion version,
string installPath)
{
if (version == null)
{
NoCache = true;
}
var folderProject = new FolderNuGetProject(
installPath,
new PackagePathResolver(installPath, !ExcludeVersion));
var sourceRepositoryProvider = GetSourceRepositoryProvider();
var packageManager = new NuGetPackageManager(sourceRepositoryProvider, Settings, installPath);
var packageSources = GetPackageSources(Settings);
Console.PrintPackageSources(packageSources);
var primaryRepositories = packageSources.Select(sourceRepositoryProvider.CreateRepository);
var allowPrerelease = Prerelease || (version != null && version.IsPrerelease);
var resolutionContext = new ResolutionContext(
DependencyBehavior.Lowest,
includePrelease: allowPrerelease,
includeUnlisted: true,
versionConstraints: VersionConstraints.None);
if (version == null)
{
// Find the latest version using NuGetPackageManager
var resolvePackage = await NuGetPackageManager.GetLatestVersionAsync(
packageId,
folderProject,
resolutionContext,
primaryRepositories,
Console,
CancellationToken.None);
if (resolvePackage == null || resolvePackage.LatestVersion == null)
{
var message = string.Format(
CultureInfo.CurrentCulture,
LocalizedResourceManager.GetString("InstallCommandUnableToFindPackage"),
packageId);
throw new CommandLineException(message);
}
version = resolvePackage.LatestVersion;
}
var packageIdentity = new PackageIdentity(packageId, version);
if (folderProject.PackageExists(packageIdentity))
{
var message = string.Format(
CultureInfo.CurrentCulture,
LocalizedResourceManager.GetString("InstallCommandPackageAlreadyExists"),
packageIdentity);
Console.LogMinimal(message);
}
else
{
var projectContext = new ConsoleProjectContext(Console)
{
PackageExtractionContext = new PackageExtractionContext(Console)
};
if (EffectivePackageSaveMode != Packaging.PackageSaveMode.None)
{
projectContext.PackageExtractionContext.PackageSaveMode = EffectivePackageSaveMode;
}
using(var cacheContext = new SourceCacheContext())
{
cacheContext.NoCache = NoCache;
cacheContext.DirectDownload = DirectDownload;
var downloadContext = new PackageDownloadContext(cacheContext, installPath, DirectDownload);
await packageManager.InstallPackageAsync(
folderProject,
packageIdentity,
resolutionContext,
projectContext,
downloadContext,
primaryRepositories,
Enumerable.Empty<SourceRepository>(),
CancellationToken.None);
if (downloadContext.DirectDownload)
{
GetDownloadResultUtility.CleanUpDirectDownloads(downloadContext);
}
}
}
}
}
}