Skip to content

Commit a4ee7d1

Browse files
committed
Add method to NuGetPackageDownloader to get source with best package version
1 parent 6f3e9a8 commit a4ee7d1

File tree

4 files changed

+31
-4
lines changed

4 files changed

+31
-4
lines changed

src/Cli/dotnet/NugetPackageDownloader/INuGetPackageDownloader.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,8 @@ Task<IEnumerable<NuGetVersion>> GetLatestPackageVersions(PackageId packageId,
3939
Task<NuGetVersion> GetBestPackageVersionAsync(PackageId packageId,
4040
VersionRange versionRange,
4141
PackageSourceLocation packageSourceLocation = null);
42+
43+
Task<(NuGetVersion version, PackageSource source)> GetBestPackageVersionAndSourceAsync(PackageId packageId,
44+
VersionRange versionRange,
45+
PackageSourceLocation packageSourceLocation = null);
4246
}

src/Cli/dotnet/NugetPackageDownloader/NuGetPackageDownloader.cs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -614,14 +614,23 @@ public async Task<NuGetVersion> GetBestPackageVersionAsync(PackageId packageId,
614614
return versionRange.MinVersion;
615615
}
616616

617+
return (await GetBestPackageVersionAndSourceAsync(packageId, versionRange, packageSourceLocation)
618+
.ConfigureAwait(false))
619+
.version;
620+
}
621+
622+
public async Task<(NuGetVersion version, PackageSource source)> GetBestPackageVersionAndSourceAsync(PackageId packageId,
623+
VersionRange versionRange,
624+
PackageSourceLocation packageSourceLocation = null)
625+
{
617626
CancellationToken cancellationToken = CancellationToken.None;
618627
IPackageSearchMetadata packageMetadata;
619628

620629
IEnumerable<PackageSource> packagesSources = LoadNuGetSources(packageId, packageSourceLocation);
621-
(_, packageMetadata) = await GetMatchingVersionInternalAsync(packageId.ToString(), packagesSources,
630+
(var source, packageMetadata) = await GetMatchingVersionInternalAsync(packageId.ToString(), packagesSources,
622631
versionRange, cancellationToken).ConfigureAwait(false);
623632

624-
return packageMetadata.Identity.Version;
633+
return (packageMetadata.Identity.Version, source);
625634
}
626635

627636
private async Task<(PackageSource, IPackageSearchMetadata)> GetPackageMetadataAsync(string packageIdentifier,

test/Microsoft.DotNet.Tools.Tests.ComponentMocks/MockNuGetPackageDownloader.cs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -130,18 +130,28 @@ public Task<NuGetVersion> GetLatestPackageVersion(PackageId packageId, PackageSo
130130
}
131131

132132
public Task<NuGetVersion> GetBestPackageVersionAsync(PackageId packageId, VersionRange versionRange, PackageSourceLocation packageSourceLocation = null)
133+
{
134+
return GetBestPackageVersionAndSourceAsync(packageId, versionRange, packageSourceLocation)
135+
.ContinueWith(t => t.Result.version, TaskContinuationOptions.OnlyOnRanToCompletion);
136+
}
137+
138+
public Task<(NuGetVersion version, PackageSource source)> GetBestPackageVersionAndSourceAsync(PackageId packageId,
139+
VersionRange versionRange,PackageSourceLocation packageSourceLocation = null)
133140
{
134141
if (!ShouldFindPackage(packageId, packageSourceLocation))
135142
{
136-
return Task.FromException<NuGetVersion>(new NuGetPackageNotFoundException(string.Format(CliStrings.IsNotFoundInNuGetFeeds, packageId, MOCK_FEEDS_TEXT)));
143+
return Task.FromException<(NuGetVersion version, PackageSource source)>(new NuGetPackageNotFoundException(string.Format(CliStrings.IsNotFoundInNuGetFeeds, packageId, MOCK_FEEDS_TEXT)));
137144
}
138145

139146
var bestVersion = versionRange.FindBestMatch(_packageVersions);
140147
if (bestVersion == null)
141148
{
142149
bestVersion = versionRange.MinVersion;
143150
}
144-
return Task.FromResult(bestVersion);
151+
152+
var source = new PackageSource("http://mock-url", "MockSource");
153+
154+
return Task.FromResult((bestVersion, source));
145155
}
146156

147157

test/dotnet.Tests/CommandTests/Workload/Install/FailingNuGetPackageInstaller.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,10 @@ public Task<IEnumerable<string>> ExtractPackageAsync(string packagePath, Directo
4242
public Task<NuGetVersion> GetLatestPackageVersion(PackageId packageId, PackageSourceLocation packageSourceLocation = null, bool includePreview = false) => throw new NotImplementedException();
4343
public Task<IEnumerable<NuGetVersion>> GetLatestPackageVersions(PackageId packageId, int numberOfResults, PackageSourceLocation packageSourceLocation = null, bool includePreview = false) => throw new NotImplementedException();
4444
public Task<NuGetVersion> GetBestPackageVersionAsync(PackageId packageId, VersionRange versionRange, PackageSourceLocation packageSourceLocation = null) => throw new NotImplementedException();
45+
46+
public Task<(NuGetVersion version, PackageSource source)> GetBestPackageVersionAndSourceAsync(PackageId packageId,
47+
VersionRange versionRange, PackageSourceLocation packageSourceLocation = null) => throw new NotImplementedException();
48+
4549
public Task<string> GetPackageUrl(PackageId packageId,
4650
NuGetVersion packageVersion,
4751
PackageSourceLocation packageSourceLocation = null,

0 commit comments

Comments
 (0)