-
Notifications
You must be signed in to change notification settings - Fork 753
Expand file tree
/
Copy pathPackageDownloadRunner.cs
More file actions
383 lines (332 loc) · 14.8 KB
/
PackageDownloadRunner.cs
File metadata and controls
383 lines (332 loc) · 14.8 KB
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
// 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.
#nullable enable
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using NuGet.CommandLine.XPlat.Utility;
using NuGet.Commands;
using NuGet.Configuration;
using NuGet.Credentials;
using NuGet.Packaging;
using NuGet.Packaging.Core;
using NuGet.Packaging.PackageExtraction;
using NuGet.Packaging.Signing;
using NuGet.Protocol;
using NuGet.Protocol.Core.Types;
using NuGet.Repositories;
using NuGet.Versioning;
namespace NuGet.CommandLine.XPlat.Commands.Package.PackageDownload
{
internal static class PackageDownloadRunner
{
internal const int ExitCodeError = 1;
internal const int ExitCodeSuccess = 0;
public static async Task<int> RunAsync(PackageDownloadArgs args, CancellationToken token)
{
ILoggerWithColor logger = new CommandOutputLogger(args.LogLevel)
{
HidePrefixForInfoAndMinimal = true
};
XPlatUtility.ConfigureProtocol();
DefaultCredentialServiceUtility.SetupDefaultCredentialService(logger, !args.Interactive);
ISettings settings = Settings.LoadDefaultSettings(
Directory.GetCurrentDirectory(),
args.ConfigFile,
new XPlatMachineWideSetting());
IReadOnlyList<PackageSource> packageSources = GetPackageSources(args.Sources, new PackageSourceProvider(settings));
return await RunAsync(args, logger, packageSources, settings, token);
}
public static async Task<int> RunAsync(PackageDownloadArgs args, ILoggerWithColor logger, IReadOnlyList<PackageSource> packageSources, ISettings settings, CancellationToken token)
{
bool hasSourcesArg = args.Sources?.Count > 0;
PackageSourceMapping? packageSourceMapping = null;
if (!hasSourcesArg)
{
packageSourceMapping = PackageSourceMapping.GetPackageSourceMapping(settings);
}
bool ignorePackageSourceMapping =
hasSourcesArg
|| packageSourceMapping is null
|| !packageSourceMapping.IsEnabled;
// When package source mapping is disabled, validate all configured sources upfront.
// When mapping is enabled, source validation is deferred to the per-package resolution step,
// since each package may map to a different subset of sources.
if (ignorePackageSourceMapping && DetectAndReportInsecureSources(args.AllowInsecureConnections, packageSources, logger))
{
return ExitCodeError;
}
string outputDirectory = args.OutputDirectory ?? Directory.GetCurrentDirectory();
var cache = new SourceCacheContext();
IReadOnlyList<SourceRepository> allRepositories = GetSourceRepositories(packageSources);
bool downloadedAllSuccessfully = true;
foreach (var package in args.Packages ?? [])
{
logger.LogMinimal(string.Format(
CultureInfo.CurrentCulture,
Strings.PackageDownloadCommand_Starting,
package.Id,
string.IsNullOrEmpty(package.NuGetVersion?.ToNormalizedString()) ? Strings.PackageDownloadCommand_LatestVersion : package.NuGetVersion.ToNormalizedString()));
// Resolve which repositories to use for this package
IReadOnlyList<SourceRepository> sourceRepositories;
if (ignorePackageSourceMapping)
{
sourceRepositories = allRepositories;
}
else
{
var mappedNames = packageSourceMapping!.GetConfiguredPackageSources(package.Id);
if (mappedNames.Count == 0)
{
// fail, no sources mapped for this package
var notConsideredSources = string.Join(
", ",
allRepositories.Select(repository => repository.PackageSource));
logger.LogError(string.Format(
CultureInfo.CurrentCulture,
Strings.PackageDownloadCommand_PackageSourceMapping_NoSourcesMapped,
package.Id,
notConsideredSources));
downloadedAllSuccessfully &= false;
continue;
}
sourceRepositories = GetMappedRepositories(mappedNames, allRepositories, package.Id, logger);
if (DetectAndReportInsecureSources(args.AllowInsecureConnections, sourceRepositories.Select(r => r.PackageSource), logger))
{
downloadedAllSuccessfully &= false;
continue;
}
}
try
{
(NuGetVersion? version, SourceRepository? downloadRepository) =
await ResolvePackageDownloadVersion(
package,
sourceRepositories,
cache,
logger,
args.IncludePrerelease,
token);
if (version == null)
{
// Unable to find a valid version
downloadedAllSuccessfully &= false;
continue;
}
bool success = await DownloadPackageAsync(
package.Id,
version,
downloadRepository!,
cache,
settings,
outputDirectory,
logger,
token);
if (success)
{
logger.LogMinimal(string.Format(
CultureInfo.CurrentCulture,
Strings.PackageDownloadCommand_Succeeded,
package.Id,
version,
outputDirectory));
}
else
{
logger.LogError(string.Format(
CultureInfo.CurrentCulture,
Strings.PackageDownloadCommand_Failed,
package.Id,
version));
downloadedAllSuccessfully &= false;
}
}
#pragma warning disable CA1031 // Do not catch general exception types
catch (Exception ex)
{
logger.LogError(ex.ToString());
downloadedAllSuccessfully &= false;
}
#pragma warning restore CA1031 // Do not catch general exception types
}
return downloadedAllSuccessfully ? ExitCodeSuccess : ExitCodeError;
}
internal static async Task<(NuGetVersion?, SourceRepository?)> ResolvePackageDownloadVersion(
PackageWithNuGetVersion packageWithNuGetVersion,
IReadOnlyList<SourceRepository> sourceRepositories,
SourceCacheContext cache,
ILoggerWithColor logger,
bool includePrerelease,
CancellationToken token)
{
NuGetVersion? versionToDownload = null;
SourceRepository? downloadSourceRepository = null;
bool versionSpecified = packageWithNuGetVersion.NuGetVersion != null;
foreach (var repo in sourceRepositories)
{
var finder = await repo.GetResourceAsync<PackageMetadataResource>(token);
var packages = await finder.GetMetadataAsync(
packageWithNuGetVersion.Id,
includePrerelease,
includeUnlisted: versionSpecified, // only load unlisted if an exact version is specified
sourceCacheContext: cache,
logger,
token);
if (packages == null)
{
continue;
}
if (versionSpecified)
{
// If an exact version is specified, check if it exists at this source
foreach (var package in packages)
{
if (package?.Identity?.Version == packageWithNuGetVersion.NuGetVersion)
{
return (packageWithNuGetVersion.NuGetVersion, repo);
}
}
continue;
}
foreach (var package in packages)
{
var version = package.Identity.Version;
if (versionToDownload == null || version > versionToDownload)
{
versionToDownload = version;
downloadSourceRepository = repo;
}
}
}
if (versionToDownload == null)
{
logger.LogError(Strings.Error_PackageDownload_VersionNotFound);
}
return (versionToDownload, downloadSourceRepository);
}
internal static IReadOnlyList<SourceRepository> GetMappedRepositories(
IReadOnlyList<string> mappedNames,
IReadOnlyList<SourceRepository> allRepos,
string packageId,
ILoggerWithColor logger)
{
var mappedRepos = new List<SourceRepository>(mappedNames.Count);
foreach (var mappedName in mappedNames)
{
SourceRepository? repo = FindRepositoryByName(mappedName, allRepos);
if (repo != null)
{
mappedRepos.Add(repo);
}
else
{
logger.LogVerbose(
string.Format(
CultureInfo.CurrentCulture,
Strings.PackageDownloadCommand_PackageSourceMapping_NoSuchSource,
mappedName,
packageId));
}
}
return mappedRepos;
}
private static SourceRepository? FindRepositoryByName(
string mappedName,
IReadOnlyList<SourceRepository> allRepos)
{
for (int i = 0; i < allRepos.Count; i++)
{
if (string.Equals(allRepos[i].PackageSource.Name, mappedName, StringComparison.OrdinalIgnoreCase))
{
return allRepos[i];
}
}
return null;
}
private static async Task<bool> DownloadPackageAsync(
string id,
NuGetVersion version,
SourceRepository repo,
SourceCacheContext cache,
ISettings settings,
string outputDirectory,
Common.ILogger logger,
CancellationToken token)
{
var extractionContext = new PackageExtractionContext(
PackageSaveMode.Defaultv3,
PackageExtractionBehavior.XmlDocFileSaveMode,
ClientPolicyContext.GetClientPolicy(settings, logger),
logger);
var resolver = new VersionFolderPathResolver(outputDirectory);
var userPackageFolder = new NuGetv3LocalRepository(outputDirectory);
// no-op if already installed
if (userPackageFolder.Exists(id, version))
{
logger.LogMinimal(string.Format(
CultureInfo.CurrentCulture,
Strings.PackageDownloadCommand_AlreadyInstalled,
id,
version.ToNormalizedString(),
outputDirectory));
return true;
}
var packageIdentity = new PackageIdentity(id, version);
var provider = new SourceRepositoryDependencyProvider(sourceRepository: repo, logger: logger, cacheContext: cache, ignoreFailedSources: false, ignoreWarning: false);
using var downloader = await provider.GetPackageDownloaderAsync(packageIdentity, cache, logger, token);
bool success = await PackageExtractor.InstallFromSourceAsync(packageIdentity, downloader, resolver, extractionContext, token);
if (!success)
{
logger.LogError(string.Format(
CultureInfo.CurrentCulture,
Strings.PackageDownloadCommand_UnableToDownload,
id,
version.ToNormalizedString(),
repo.PackageSource.Source));
return false;
}
return success;
}
private static IReadOnlyList<PackageSource> GetPackageSources(IList<string>? sources, IPackageSourceProvider sourceProvider)
{
IEnumerable<PackageSource> configuredSources = sourceProvider.LoadPackageSources()
.Where(s => s.IsEnabled);
if (sources != null && sources.Count > 0)
{
// Use sources specified on command line
return [.. sources.Select(s => PackageSourceProviderExtensions.ResolveSource(configuredSources, s))];
}
return [.. configuredSources];
}
private static bool DetectAndReportInsecureSources(
bool allowInsecureConnections,
IEnumerable<PackageSource> packageSources,
ILoggerWithColor logger)
{
if (!allowInsecureConnections)
{
var insecureSources = HttpSourcesUtility.GetDisallowedInsecureHttpSources([.. packageSources]);
if (insecureSources.Any())
{
logger.LogError(HttpSourcesUtility.BuildHttpSourceErrorMessage(insecureSources, "package download"));
return true;
}
}
return false;
}
private static IReadOnlyList<SourceRepository> GetSourceRepositories(IReadOnlyList<PackageSource> packageSources)
{
IEnumerable<Lazy<INuGetResourceProvider>> providers = Repository.Provider.GetCoreV3();
List<SourceRepository> sourceRepositories = [];
foreach (var source in packageSources)
{
sourceRepositories.Add(Repository.CreateSource(providers, source, FeedType.Undefined));
}
return sourceRepositories;
}
}
}