Add option to search for unlisted packages (#636)

This commit is contained in:
Taylor Southwick 2021-06-16 13:34:46 -07:00 коммит произвёл GitHub
Родитель c234927fac
Коммит ef31c22633
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
10 изменённых файлов: 59 добавлений и 26 удалений

Просмотреть файл

@ -11,9 +11,9 @@ namespace Microsoft.DotNet.UpgradeAssistant
{
Task<bool> DoesPackageSupportTargetFrameworksAsync(NuGetReference packageReference, IEnumerable<TargetFrameworkMoniker> targetFrameworks, CancellationToken token);
Task<IEnumerable<NuGetReference>> GetNewerVersionsAsync(NuGetReference reference, IEnumerable<TargetFrameworkMoniker> tfms, bool latestMinorAndBuildOnly, CancellationToken token);
Task<IEnumerable<NuGetReference>> GetNewerVersionsAsync(NuGetReference reference, IEnumerable<TargetFrameworkMoniker> tfms, PackageSearchOptions options, CancellationToken token);
Task<NuGetReference?> GetLatestVersionAsync(string packageName, IEnumerable<TargetFrameworkMoniker> tfms, bool includePreRelease, CancellationToken token);
Task<NuGetReference?> GetLatestVersionAsync(string packageName, IEnumerable<TargetFrameworkMoniker> tfms, PackageSearchOptions options, CancellationToken token);
Task<NuGetPackageMetadata?> GetPackageMetadata(NuGetReference reference, CancellationToken token);
}

Просмотреть файл

@ -0,0 +1,23 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
namespace Microsoft.DotNet.UpgradeAssistant
{
public record PackageSearchOptions
{
/// <summary>
/// Gets a value indicating whether to search for prerelease versions.
/// </summary>
public bool Prerelease { get; init; }
/// <summary>
/// Gets a value indicating whether to search for unlisted packages.
/// </summary>
public bool Unlisted { get; init; }
/// <summary>
/// Gets a value indicating whether to search for latest changes within the same major version when supplied.
/// </summary>
public bool LatestMinorAndBuildOnly { get; init; }
}
}

Просмотреть файл

@ -79,24 +79,34 @@ namespace Microsoft.DotNet.UpgradeAssistant.MSBuild
return targetFrameworks.All(tfm => packageFrameworks.Any(f => DefaultCompatibilityProvider.Instance.IsCompatible(NuGetFramework.Parse(tfm.Name), f)));
}
public Task<IEnumerable<NuGetReference>> GetNewerVersionsAsync(NuGetReference reference, IEnumerable<TargetFrameworkMoniker> tfms, bool latestMinorAndBuildOnly, CancellationToken token)
public Task<IEnumerable<NuGetReference>> GetNewerVersionsAsync(NuGetReference reference, IEnumerable<TargetFrameworkMoniker> tfms, PackageSearchOptions options, CancellationToken token)
{
if (reference is null)
{
throw new ArgumentNullException(nameof(reference));
}
return SearchByNameAsync(reference.Name, tfms, currentVersion: reference.GetNuGetVersion(), latestMinorAndBuildOnly: latestMinorAndBuildOnly, token: token);
if (options is null)
{
throw new ArgumentNullException(nameof(options));
}
return SearchByNameAsync(reference.Name, tfms, options, currentVersion: reference.GetNuGetVersion(), token: token);
}
public async Task<NuGetReference?> GetLatestVersionAsync(string packageName, IEnumerable<TargetFrameworkMoniker> tfms, bool includePreRelease, CancellationToken token)
public async Task<NuGetReference?> GetLatestVersionAsync(string packageName, IEnumerable<TargetFrameworkMoniker> tfms, PackageSearchOptions options, CancellationToken token)
{
var result = await SearchByNameAsync(packageName, tfms, includePreRelease, token: token).ConfigureAwait(false);
if (options is null)
{
throw new ArgumentNullException(nameof(options));
}
var result = await SearchByNameAsync(packageName, tfms, options, null, token: token).ConfigureAwait(false);
return result.LastOrDefault();
}
private async Task<IEnumerable<NuGetReference>> SearchByNameAsync(string name, IEnumerable<TargetFrameworkMoniker> tfms, bool includePrerelease = false, NuGetVersion? currentVersion = null, bool latestMinorAndBuildOnly = false, CancellationToken token = default)
private async Task<IEnumerable<NuGetReference>> SearchByNameAsync(string name, IEnumerable<TargetFrameworkMoniker> tfms, PackageSearchOptions options, NuGetVersion? currentVersion = null, CancellationToken token = default)
{
var results = new List<IPackageSearchMetadata>();
@ -105,7 +115,7 @@ namespace Microsoft.DotNet.UpgradeAssistant.MSBuild
try
{
var metadata = await GetSourceRepository(source).GetResourceAsync<PackageMetadataResource>(token).ConfigureAwait(false);
var searchResults = await CallWithRetryAsync(() => metadata.GetMetadataAsync(name, includePrerelease: includePrerelease, includeUnlisted: false, _cache, _nugetLogger, token)).ConfigureAwait(false);
var searchResults = await CallWithRetryAsync(() => metadata.GetMetadataAsync(name, includePrerelease: options.Prerelease, includeUnlisted: options.Unlisted, _cache, _nugetLogger, token)).ConfigureAwait(false);
results.AddRange(searchResults);
}
@ -120,7 +130,7 @@ namespace Microsoft.DotNet.UpgradeAssistant.MSBuild
}
}
return FilterSearchResults(name, results, tfms, currentVersion, latestMinorAndBuildOnly);
return FilterSearchResults(name, results, tfms, currentVersion, options.LatestMinorAndBuildOnly);
}
public static IEnumerable<NuGetReference> FilterSearchResults(

Просмотреть файл

@ -60,7 +60,7 @@ namespace Microsoft.DotNet.UpgradeAssistant.Extensions.Web
if (!state.Packages.Any(r => NewtonsoftPackageName.Equals(r.Name, StringComparison.OrdinalIgnoreCase)))
{
var newtonsoftPackage = await _packageLoader.GetLatestVersionAsync(NewtonsoftPackageName, state.TargetFrameworks, false, token).ConfigureAwait(false);
var newtonsoftPackage = await _packageLoader.GetLatestVersionAsync(NewtonsoftPackageName, state.TargetFrameworks, new(), token).ConfigureAwait(false);
if (newtonsoftPackage is not null)
{

Просмотреть файл

@ -59,7 +59,7 @@ namespace Microsoft.DotNet.UpgradeAssistant.Steps.Packages.Analyzers
{
_logger.LogInformation("Marking package {PackageName} for removal based on package mapping configuration {PackageMapSet}", packageReference.Name, map.PackageSetName);
state.Packages.Remove(packageReference, BuildBreakRisk.Medium);
await AddNetCoreReferences(map, state, project, token).ConfigureAwait(false);
await AddNetCoreReferences(map, state, token).ConfigureAwait(false);
}
}
@ -69,7 +69,7 @@ namespace Microsoft.DotNet.UpgradeAssistant.Steps.Packages.Analyzers
{
_logger.LogInformation("Marking assembly reference {ReferenceName} for removal based on package mapping configuration {PackageMapSet}", reference.Name, map.PackageSetName);
state.References.Remove(reference, BuildBreakRisk.Medium);
await AddNetCoreReferences(map, state, project, token).ConfigureAwait(false);
await AddNetCoreReferences(map, state, token).ConfigureAwait(false);
}
}
}
@ -99,14 +99,14 @@ namespace Microsoft.DotNet.UpgradeAssistant.Steps.Packages.Analyzers
return _comparer.Compare(version, reference.Version) <= 0;
}
private async Task AddNetCoreReferences(NuGetPackageMap packageMap, IDependencyAnalysisState state, IProject project, CancellationToken token)
private async Task AddNetCoreReferences(NuGetPackageMap packageMap, IDependencyAnalysisState state, CancellationToken token)
{
foreach (var newPackage in packageMap.NetCorePackages)
{
var packageToAdd = newPackage;
if (packageToAdd.HasWildcardVersion)
{
var reference = await _packageLoader.GetLatestVersionAsync(packageToAdd.Name, state.TargetFrameworks, false, token).ConfigureAwait(false);
var reference = await _packageLoader.GetLatestVersionAsync(packageToAdd.Name, state.TargetFrameworks, new(), token).ConfigureAwait(false);
if (reference is not null)
{

Просмотреть файл

@ -51,7 +51,7 @@ namespace Microsoft.DotNet.UpgradeAssistant.Steps.Packages.Analyzers
else
{
// If the package won't work on the target Framework, check newer versions of the package
var newerVersions = await _packageLoader.GetNewerVersionsAsync(packageReference, state.TargetFrameworks, true, token).ConfigureAwait(false);
var newerVersions = await _packageLoader.GetNewerVersionsAsync(packageReference, state.TargetFrameworks, new() { LatestMinorAndBuildOnly = true }, token).ConfigureAwait(false);
var updatedReference = newerVersions.FirstOrDefault();
if (updatedReference == null)

Просмотреть файл

@ -46,7 +46,7 @@ namespace Microsoft.DotNet.UpgradeAssistant.Steps.Packages.Analyzers
// If the project doesn't include a reference to the analyzer package, mark it for addition
if (!state.Packages.Any(r => AnalyzerPackageName.Equals(r.Name, StringComparison.OrdinalIgnoreCase)))
{
var analyzerPackage = await _packageLoader.GetLatestVersionAsync(AnalyzerPackageName, state.TargetFrameworks, true, token).ConfigureAwait(false);
var analyzerPackage = await _packageLoader.GetLatestVersionAsync(AnalyzerPackageName, state.TargetFrameworks, new() { Prerelease = true }, token).ConfigureAwait(false);
if (analyzerPackage is not null)
{

Просмотреть файл

@ -53,7 +53,7 @@ namespace Microsoft.DotNet.UpgradeAssistant.Steps.Packages.Analyzers
return;
}
var latestVersion = await _loader.GetLatestVersionAsync(PackageName, state.TargetFrameworks, false, token).ConfigureAwait(false);
var latestVersion = await _loader.GetLatestVersionAsync(PackageName, state.TargetFrameworks, new(), token).ConfigureAwait(false);
if (latestVersion is null)
{

Просмотреть файл

@ -46,7 +46,7 @@ namespace Microsoft.DotNet.UpgradeAssistant.Extensions.Web.Tests
// Assert
packages.Verify(p => p.Add(new NuGetReference(NewtonsoftPackageName, NewtonsoftPackageNameVersion), BuildBreakRisk.None));
packageLoader.Verify(pl => pl.GetLatestVersionAsync(It.IsAny<string>(), It.IsAny<IEnumerable<TargetFrameworkMoniker>>(), It.IsAny<bool>(), It.IsAny<CancellationToken>()),
packageLoader.Verify(pl => pl.GetLatestVersionAsync(It.IsAny<string>(), It.IsAny<IEnumerable<TargetFrameworkMoniker>>(), It.IsAny<PackageSearchOptions>(), It.IsAny<CancellationToken>()),
Times.Once());
}
@ -78,7 +78,7 @@ namespace Microsoft.DotNet.UpgradeAssistant.Extensions.Web.Tests
// Assert
packageState.Verify(p => p.Packages.Add(new NuGetReference(NewtonsoftPackageName, NewtonsoftPackageNameVersion), BuildBreakRisk.None), Times.Never);
packageLoader.Verify(pl => pl.GetLatestVersionAsync(It.IsAny<string>(), It.IsAny<IEnumerable<TargetFrameworkMoniker>>(), It.IsAny<bool>(), It.IsAny<CancellationToken>()),
packageLoader.Verify(pl => pl.GetLatestVersionAsync(It.IsAny<string>(), It.IsAny<IEnumerable<TargetFrameworkMoniker>>(), It.IsAny<PackageSearchOptions>(), It.IsAny<CancellationToken>()),
Times.Never());
}
@ -105,7 +105,7 @@ namespace Microsoft.DotNet.UpgradeAssistant.Extensions.Web.Tests
// Assert
packageState.Verify(p => p.Packages.Add(new NuGetReference(NewtonsoftPackageName, NewtonsoftPackageNameVersion), BuildBreakRisk.None), Times.Never);
packageLoader.Verify(pl => pl.GetLatestVersionAsync(It.IsAny<string>(), It.IsAny<IEnumerable<TargetFrameworkMoniker>>(), It.IsAny<bool>(), It.IsAny<CancellationToken>()),
packageLoader.Verify(pl => pl.GetLatestVersionAsync(It.IsAny<string>(), It.IsAny<IEnumerable<TargetFrameworkMoniker>>(), It.IsAny<PackageSearchOptions>(), It.IsAny<CancellationToken>()),
Times.Never());
}
@ -132,7 +132,7 @@ namespace Microsoft.DotNet.UpgradeAssistant.Extensions.Web.Tests
// Assert
packageState.Verify(p => p.Packages.Add(new NuGetReference(NewtonsoftPackageName, NewtonsoftPackageNameVersion), BuildBreakRisk.None), Times.Never);
packageLoader.Verify(pl => pl.GetLatestVersionAsync(It.IsAny<string>(), It.IsAny<IEnumerable<TargetFrameworkMoniker>>(), It.IsAny<bool>(), It.IsAny<CancellationToken>()),
packageLoader.Verify(pl => pl.GetLatestVersionAsync(It.IsAny<string>(), It.IsAny<IEnumerable<TargetFrameworkMoniker>>(), It.IsAny<PackageSearchOptions>(), It.IsAny<CancellationToken>()),
Times.Never());
}
@ -154,7 +154,7 @@ namespace Microsoft.DotNet.UpgradeAssistant.Extensions.Web.Tests
private static Mock<IPackageLoader> CreatePackageLoader(AutoMock mock)
{
var packageLoader = mock.Mock<IPackageLoader>();
packageLoader.Setup(pl => pl.GetLatestVersionAsync(It.IsAny<string>(), It.IsAny<IEnumerable<TargetFrameworkMoniker>>(), It.IsAny<bool>(), It.IsAny<CancellationToken>()))
packageLoader.Setup(pl => pl.GetLatestVersionAsync(It.IsAny<string>(), It.IsAny<IEnumerable<TargetFrameworkMoniker>>(), It.IsAny<PackageSearchOptions>(), It.IsAny<CancellationToken>()))
.Returns(Task.FromResult((NuGetReference?)new NuGetReference(NewtonsoftPackageName, NewtonsoftPackageNameVersion)));
return packageLoader;
}

Просмотреть файл

@ -31,14 +31,14 @@ namespace Integration.Tests
return _other.DoesPackageSupportTargetFrameworksAsync(packageReference, targetFrameworks, token);
}
public async Task<NuGetReference?> GetLatestVersionAsync(string packageName, IEnumerable<TargetFrameworkMoniker> tfms, bool includePreRelease, CancellationToken token)
public async Task<NuGetReference?> GetLatestVersionAsync(string packageName, IEnumerable<TargetFrameworkMoniker> tfms, PackageSearchOptions options, CancellationToken token)
{
if (_packages.TryGetValue(packageName, out var known))
{
return known;
}
var latest = await _other.GetLatestVersionAsync(packageName, tfms, includePreRelease, token).ConfigureAwait(false);
var latest = await _other.GetLatestVersionAsync(packageName, tfms, options, token).ConfigureAwait(false);
if (latest is not null)
{
@ -49,14 +49,14 @@ namespace Integration.Tests
return latest;
}
public async Task<IEnumerable<NuGetReference>> GetNewerVersionsAsync(NuGetReference reference, IEnumerable<TargetFrameworkMoniker> tfms, bool latestMinorAndBuildOnly, CancellationToken token)
public async Task<IEnumerable<NuGetReference>> GetNewerVersionsAsync(NuGetReference reference, IEnumerable<TargetFrameworkMoniker> tfms, PackageSearchOptions options, CancellationToken token)
{
if (_packages.TryGetValue(reference.Name, out var known))
{
return new NuGetReference[] { known };
}
var latest = await _other.GetNewerVersionsAsync(reference, tfms, latestMinorAndBuildOnly, token).ConfigureAwait(false);
var latest = await _other.GetNewerVersionsAsync(reference, tfms, options, token).ConfigureAwait(false);
if (latest is not null)
{