Skip to content

Commit 52f9b05

Browse files
committed
nullable and fix
1 parent 9b76d50 commit 52f9b05

File tree

1 file changed

+49
-29
lines changed

1 file changed

+49
-29
lines changed

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

Lines changed: 49 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
// Copyright (c) .NET Foundation. All rights reserved.
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

4+
#nullable enable
5+
46
using System;
57
using System.Collections.Generic;
68
using System.Globalization;
@@ -48,8 +50,10 @@ public static async Task<int> RunAsync(PackageDownloadArgs args, CancellationTok
4850

4951
public static async Task<int> RunAsync(PackageDownloadArgs args, ILoggerWithColor logger, IReadOnlyList<PackageSource> packageSources, ISettings settings, CancellationToken token)
5052
{
51-
// If --source is explicitly provided, validate those sources up front.
52-
if (args.Sources != null && args.Sources.Count > 0 && DetectAndReportInsecureSources(args.AllowInsecureConnections, packageSources, logger))
53+
var packageSourceMapping = PackageSourceMapping.GetPackageSourceMapping(settings);
54+
var hasSourcesArg = args.Sources != null && args.Sources.Count > 0;
55+
var mappingDisabled = (packageSourceMapping != null && !packageSourceMapping.IsEnabled) || packageSourceMapping == null;
56+
if ((mappingDisabled || hasSourcesArg) && DetectAndReportInsecureSources(args.AllowInsecureConnections, packageSources, logger))
5357
{
5458
return ExitCodeError;
5559
}
@@ -58,11 +62,10 @@ public static async Task<int> RunAsync(PackageDownloadArgs args, ILoggerWithColo
5862
var cache = new SourceCacheContext();
5963

6064
IReadOnlyDictionary<string, SourceRepository> sourceRepositoriesMap = GetSourceRepositories(packageSources);
61-
var packageSourceMapping = PackageSourceMapping.GetPackageSourceMapping(settings);
6265

6366
bool downloadedAllSuccessfully = true;
6467

65-
foreach (var package in args.Packages)
68+
foreach (var package in args.Packages ?? [])
6669
{
6770
logger.LogMinimal(string.Format(
6871
CultureInfo.CurrentCulture,
@@ -85,7 +88,7 @@ public static async Task<int> RunAsync(PackageDownloadArgs args, ILoggerWithColo
8588

8689
try
8790
{
88-
(NuGetVersion version, SourceRepository downloadRepository) =
91+
(NuGetVersion? version, SourceRepository? downloadRepository) =
8992
await ResolvePackageDownloadVersion(
9093
package,
9194
sourceRepositories,
@@ -104,7 +107,7 @@ await ResolvePackageDownloadVersion(
104107
bool success = await DownloadPackageAsync(
105108
package.Id,
106109
version,
107-
downloadRepository,
110+
downloadRepository!,
108111
cache,
109112
settings,
110113
outputDirectory,
@@ -143,16 +146,16 @@ await ResolvePackageDownloadVersion(
143146
return downloadedAllSuccessfully ? ExitCodeSuccess : ExitCodeError;
144147
}
145148

146-
internal static async Task<(NuGetVersion, SourceRepository)> ResolvePackageDownloadVersion(
149+
internal static async Task<(NuGetVersion?, SourceRepository?)> ResolvePackageDownloadVersion(
147150
PackageWithNuGetVersion packageWithNuGetVersion,
148151
IEnumerable<SourceRepository> sourceRepositories,
149152
SourceCacheContext cache,
150153
ILoggerWithColor logger,
151154
bool includePrerelease,
152155
CancellationToken token)
153156
{
154-
NuGetVersion versionToDownload = null;
155-
SourceRepository downloadSourceRepository = null;
157+
NuGetVersion? versionToDownload = null;
158+
SourceRepository? downloadSourceRepository = null;
156159
bool versionSpecified = packageWithNuGetVersion.NuGetVersion != null;
157160

158161
foreach (var repo in sourceRepositories)
@@ -213,41 +216,58 @@ private static bool TryGetRepositoriesForPackage(
213216
string packageId,
214217
PackageDownloadArgs args,
215218
IReadOnlyList<PackageSource> allPackageSources,
216-
PackageSourceMapping packageSourceMapping,
219+
PackageSourceMapping? packageSourceMapping,
217220
IReadOnlyDictionary<string, SourceRepository> sourceRepositoriesMap,
218221
ILoggerWithColor logger,
219222
out List<SourceRepository> repositories)
220223
{
221-
repositories = new List<SourceRepository>();
224+
List<PackageSource> effectiveSources;
222225

223-
if (args.Sources != null && args.Sources.Count > 0)
226+
var sourceExplicitlyProvided = args.Sources?.Count > 0;
227+
if (sourceExplicitlyProvided || (packageSourceMapping != null && !packageSourceMapping.IsEnabled))
224228
{
225-
// --source specified: use the provided sources
226-
foreach (var source in allPackageSources)
227-
{
228-
repositories.Add(sourceRepositoriesMap[source.Name]);
229-
}
230-
231-
return true;
229+
// --source given OR mapping disabled: use all provided sources as-is
230+
effectiveSources = [.. allPackageSources];
232231
}
233-
234-
// No --source: use package source mapping
235-
var mappedSourceNames = packageSourceMapping.GetConfiguredPackageSources(packageId);
236-
var mappedSources = allPackageSources.Where(ps => mappedSourceNames.Contains(ps.Name, StringComparer.OrdinalIgnoreCase)).ToList();
237-
if (DetectAndReportInsecureSources(args.AllowInsecureConnections, mappedSources, logger))
232+
else
238233
{
239-
return false;
234+
// Mapping enabled, no --source: try mapped names first
235+
var mappedNames = packageSourceMapping == null ? [] : packageSourceMapping.GetConfiguredPackageSources(packageId);
236+
237+
// Build effective sources in the same order as mappedNames
238+
var mapped = mappedNames
239+
.Select(n => allPackageSources.FirstOrDefault(ps =>
240+
string.Equals(ps.Name, n, StringComparison.OrdinalIgnoreCase)))
241+
.ToList();
242+
243+
// Only validate insecure sources when mapping produced something
244+
if (mapped.Count > 0)
245+
{
246+
if (DetectAndReportInsecureSources(args.AllowInsecureConnections, mapped!, logger))
247+
{
248+
repositories = [];
249+
return false;
250+
}
251+
252+
effectiveSources = mapped!;
253+
}
254+
else
255+
{
256+
// No mapping for this package: fall back to all sources
257+
effectiveSources = [.. allPackageSources];
258+
}
240259
}
241260

242-
foreach (var name in mappedSourceNames)
261+
// Convert effective sources to repositories
262+
repositories = new List<SourceRepository>(effectiveSources.Count);
263+
foreach (var src in effectiveSources)
243264
{
244-
repositories.Add(sourceRepositoriesMap[name]);
265+
repositories.Add(sourceRepositoriesMap[src.Name]);
245266
}
246267

247268
return true;
248269
}
249270

250-
251271
private static async Task<bool> DownloadPackageAsync(
252272
string id,
253273
NuGetVersion version,
@@ -298,7 +318,7 @@ private static async Task<bool> DownloadPackageAsync(
298318
return success;
299319
}
300320

301-
private static IReadOnlyList<PackageSource> GetPackageSources(IList<string> sources, IPackageSourceProvider sourceProvider)
321+
private static IReadOnlyList<PackageSource> GetPackageSources(IList<string>? sources, IPackageSourceProvider sourceProvider)
302322
{
303323
IEnumerable<PackageSource> configuredSources = sourceProvider.LoadPackageSources()
304324
.Where(s => s.IsEnabled);

0 commit comments

Comments
 (0)