Skip to content

Commit 8472caf

Browse files
committed
Simplify
1 parent ca34b33 commit 8472caf

4 files changed

Lines changed: 215 additions & 348 deletions

File tree

src/NuGet.Core/NuGet.CommandLine.XPlat/Commands/Package/Download/PackageDownloadRunner.cs

Lines changed: 30 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,12 @@ public static async Task<int> RunAsync(PackageDownloadArgs args, CancellationTok
4141
Directory.GetCurrentDirectory(),
4242
args.ConfigFile,
4343
new XPlatMachineWideSetting());
44-
IEnumerable<PackageSource> packageSources = GetPackageSources(args.Sources, new PackageSourceProvider(settings));
44+
IReadOnlyList<PackageSource> packageSources = GetPackageSources(args.Sources, new PackageSourceProvider(settings));
4545

4646
return await RunAsync(args, logger, packageSources, settings, token);
4747
}
4848

49-
public static async Task<int> RunAsync(PackageDownloadArgs args, ILoggerWithColor logger, IEnumerable<PackageSource> packageSources, ISettings settings, CancellationToken token)
49+
public static async Task<int> RunAsync(PackageDownloadArgs args, ILoggerWithColor logger, IReadOnlyList<PackageSource> packageSources, ISettings settings, CancellationToken token)
5050
{
5151
// Check for insecure sources
5252
if (DetectAndReportInsecureSources(args.AllowInsecureConnections, packageSources, logger))
@@ -56,7 +56,7 @@ public static async Task<int> RunAsync(PackageDownloadArgs args, ILoggerWithColo
5656

5757
string outputDirectory = args.OutputDirectory ?? Directory.GetCurrentDirectory();
5858
var cache = new SourceCacheContext();
59-
IEnumerable<SourceRepository> sourceRepositories = GetSourceRepositories(packageSources);
59+
IReadOnlyList<SourceRepository> sourceRepositories = GetSourceRepositories(packageSources);
6060
bool downloadedAllSuccessfully = true;
6161

6262
foreach (var package in args.Packages)
@@ -85,7 +85,7 @@ await ResolvePackageDownloadVersion(
8585
continue;
8686
}
8787

88-
bool download = await DownloadPackageAsync(
88+
bool success = await DownloadPackageAsync(
8989
package.Id,
9090
version,
9191
downloadRepository,
@@ -95,7 +95,7 @@ await ResolvePackageDownloadVersion(
9595
logger,
9696
token);
9797

98-
if (download)
98+
if (success)
9999
{
100100
logger.LogMinimal(string.Format(
101101
CultureInfo.CurrentCulture,
@@ -128,7 +128,7 @@ await ResolvePackageDownloadVersion(
128128
}
129129

130130
internal static async Task<(NuGetVersion, SourceRepository)> ResolvePackageDownloadVersion(
131-
PackageWithNuGetVersion package,
131+
PackageWithNuGetVersion packageWithNuGetVersion,
132132
IEnumerable<SourceRepository> sourceRepositories,
133133
SourceCacheContext cache,
134134
ILoggerWithColor logger,
@@ -137,13 +137,13 @@ await ResolvePackageDownloadVersion(
137137
{
138138
NuGetVersion versionToDownload = null;
139139
SourceRepository downloadSourceRepository = null;
140-
bool versionSpecified = package.NuGetVersion != null;
140+
bool versionSpecified = packageWithNuGetVersion.NuGetVersion != null;
141141

142142
foreach (var repo in sourceRepositories)
143143
{
144144
var finder = await repo.GetResourceAsync<PackageMetadataResource>(token);
145145
var packages = await finder.GetMetadataAsync(
146-
package.Id,
146+
packageWithNuGetVersion.Id,
147147
includePrerelease,
148148
includeUnlisted: false,
149149
sourceCacheContext: cache,
@@ -158,23 +158,23 @@ await ResolvePackageDownloadVersion(
158158
if (versionSpecified)
159159
{
160160
// If an exact version is specified, check if it exists at this source
161-
foreach (var p in packages)
161+
foreach (var package in packages)
162162
{
163-
if (p?.Identity?.Version == package.NuGetVersion)
163+
if (package?.Identity?.Version == packageWithNuGetVersion.NuGetVersion)
164164
{
165-
return (package.NuGetVersion, repo);
165+
return (packageWithNuGetVersion.NuGetVersion, repo);
166166
}
167167
}
168168

169169
continue;
170170
}
171171

172-
foreach (var p in packages)
172+
foreach (var package in packages)
173173
{
174-
var v = p.Identity.Version;
175-
if (versionToDownload == null || v > versionToDownload)
174+
var version = package.Identity.Version;
175+
if (versionToDownload == null || version > versionToDownload)
176176
{
177-
versionToDownload = v;
177+
versionToDownload = version;
178178
downloadSourceRepository = repo;
179179
}
180180
}
@@ -220,67 +220,36 @@ private static async Task<bool> DownloadPackageAsync(
220220
return true;
221221
}
222222

223-
try
223+
var packageIdentity = new PackageIdentity(id, version);
224+
var provider = new SourceRepositoryDependencyProvider(sourceRepository: repo, logger: logger, cacheContext: cache, ignoreFailedSources: false, ignoreWarning: false);
225+
using var downloader = await provider.GetPackageDownloaderAsync(packageIdentity, cache, logger, token);
226+
bool success = await PackageExtractor.InstallFromSourceAsync(packageIdentity, downloader, resolver, extractionContext, token);
227+
if (!success)
224228
{
225-
var finder = await repo.GetResourceAsync<FindPackageByIdResource>(token);
226-
bool installed = await PackageExtractor.InstallFromSourceAsync(
227-
repo.PackageSource.Source,
228-
new PackageIdentity(id, version),
229-
async (destination) =>
230-
{
231-
using var nupkg = new MemoryStream();
232-
bool ok = await finder.CopyNupkgToStreamAsync(
233-
id, version, nupkg, cache, logger, token);
234-
235-
if (!ok) throw new InvalidOperationException("Package not found.");
236-
237-
nupkg.Position = 0;
238-
await nupkg.CopyToAsync(destination, 81920);
239-
},
240-
resolver,
241-
extractionContext,
242-
token);
243-
244-
if (installed)
245-
{
246-
return true;
247-
}
248-
}
249-
catch (InvalidOperationException)
250-
{
251-
// Unable to download the package
252229
logger.LogError(string.Format(
253-
CultureInfo.CurrentCulture,
254-
Strings.PackageDownloadCommand_UnableToDownload,
255-
id,
256-
version.ToNormalizedString(),
257-
repo.PackageSource.Source));
230+
CultureInfo.CurrentCulture,
231+
Strings.PackageDownloadCommand_UnableToDownload,
232+
id,
233+
version.ToNormalizedString(),
234+
repo.PackageSource.Source));
258235
return false;
259236
}
260237

261-
return false;
238+
return success;
262239
}
263240

264-
private static IEnumerable<PackageSource> GetPackageSources(IList<string> sources, IPackageSourceProvider sourceProvider)
241+
private static IReadOnlyList<PackageSource> GetPackageSources(IList<string> sources, IPackageSourceProvider sourceProvider)
265242
{
266243
IEnumerable<PackageSource> configuredSources = sourceProvider.LoadPackageSources()
267244
.Where(s => s.IsEnabled);
268245

269-
IEnumerable<PackageSource> packageSources;
270-
271246
if (sources != null && sources.Count > 0)
272247
{
273248
// Use sources specified on command line
274-
packageSources = sources
275-
.Select(s => PackageSourceProviderExtensions.ResolveSource(configuredSources, s));
276-
}
277-
else
278-
{
279-
// Use all configured sources
280-
packageSources = configuredSources;
249+
return [.. sources.Select(s => PackageSourceProviderExtensions.ResolveSource(configuredSources, s))];
281250
}
282251

283-
return packageSources;
252+
return [.. configuredSources];
284253
}
285254

286255
private static bool DetectAndReportInsecureSources(
@@ -301,7 +270,7 @@ private static bool DetectAndReportInsecureSources(
301270
return false;
302271
}
303272

304-
private static IEnumerable<SourceRepository> GetSourceRepositories(IEnumerable<PackageSource> packageSources)
273+
private static IReadOnlyList<SourceRepository> GetSourceRepositories(IReadOnlyList<PackageSource> packageSources)
305274
{
306275
IEnumerable<Lazy<INuGetResourceProvider>> providers = Repository.Provider.GetCoreV3();
307276
List<SourceRepository> sourceRepositories = [];

0 commit comments

Comments
 (0)