-
Notifications
You must be signed in to change notification settings - Fork 753
Expand file tree
/
Copy pathPackageDownloadRunner.cs
More file actions
355 lines (313 loc) · 14.1 KB
/
PackageDownloadRunner.cs
File metadata and controls
355 lines (313 loc) · 14.1 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
// 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)
{
var packageSourceMapping = PackageSourceMapping.GetPackageSourceMapping(settings);
var hasSourcesArg = args.Sources != null && args.Sources.Count > 0;
var mappingDisabled = (packageSourceMapping != null && !packageSourceMapping.IsEnabled) || packageSourceMapping == null;
bool ignorePackageSourceMapping = hasSourcesArg || mappingDisabled;
// Validate all configured sources only when mapping is disabled
if (ignorePackageSourceMapping && DetectAndReportInsecureSources(args.AllowInsecureConnections, packageSources, logger))
{
return ExitCodeError;
}
string outputDirectory = args.OutputDirectory ?? Directory.GetCurrentDirectory();
var cache = new SourceCacheContext();
(IReadOnlyDictionary<string, SourceRepository> sourceRepositoriesMap, List<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
List<SourceRepository> sourceRepositories;
if (ignorePackageSourceMapping)
{
sourceRepositories = allRepositories;
}
else
{
if (!TryGetRepositoriesForPackage(
package.Id,
args,
packageSourceMapping!,
sourceRepositoriesMap,
allRepositories,
logger,
out sourceRepositories))
{
return ExitCodeError;
}
}
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,
IEnumerable<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);
}
/// <summary>
/// Builds the set of SourceRepository objects to use for a given package,
/// applying package source mapping
/// validating HTTP usage only on the *effective* sources.
/// </summary>
private static bool TryGetRepositoriesForPackage(
string packageId,
PackageDownloadArgs args,
PackageSourceMapping packageSourceMapping,
IReadOnlyDictionary<string, SourceRepository> sourceRepositoriesMap,
List<SourceRepository> allRepos,
ILoggerWithColor logger,
out List<SourceRepository> repositories)
{
var mappedNames = packageSourceMapping.GetConfiguredPackageSources(packageId);
var mappedRepos = mappedNames
.Select(name => sourceRepositoriesMap[name])
.ToList();
// Only validate insecure sources when mapping produced something
if (mappedRepos.Count > 0)
{
if (DetectAndReportInsecureSources(args.AllowInsecureConnections, mappedRepos.Select(sourceRepo => sourceRepo.PackageSource), logger))
{
repositories = [];
return false;
}
repositories = mappedRepos;
return true;
}
else
{
// No mapping for this package: fall back to all sources
repositories = allRepos;
return true;
}
}
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 (IReadOnlyDictionary<string, SourceRepository>, List<SourceRepository>) GetSourceRepositories(IReadOnlyList<PackageSource> packageSources)
{
IEnumerable<Lazy<INuGetResourceProvider>> providers = Repository.Provider.GetCoreV3();
Dictionary<string, SourceRepository> sourceRepositories = [];
List<SourceRepository> allRepositories = [];
foreach (var source in packageSources)
{
sourceRepositories[source.Name] = Repository.CreateSource(providers, source, FeedType.Undefined);
allRepositories.Add(sourceRepositories[source.Name]);
}
return (sourceRepositories, allRepositories);
}
}
}