display highest version package in results

This commit is contained in:
Matt Parker
2025-11-01 20:57:02 +10:00
parent 3afb8b56fa
commit 2933e5dfc2
2 changed files with 18 additions and 14 deletions

View File

@@ -1,12 +1,15 @@
using NuGet.Common; using NuGet.Common;
using NuGet.Configuration; using NuGet.Configuration;
using NuGet.Packaging.Core;
using NuGet.Protocol; using NuGet.Protocol;
using NuGet.Protocol.Core.Types; using NuGet.Protocol.Core.Types;
using SharpIDE.Application.Features.SolutionDiscovery.VsPersistence; using SharpIDE.Application.Features.SolutionDiscovery.VsPersistence;
namespace SharpIDE.Application.Features.Nuget; namespace SharpIDE.Application.Features.Nuget;
public record IdePackageResult(IPackageSearchMetadata PackageSearchMetadata, List<PackageSource> PackageSources); //public record IdePackageResult(IPackageSearchMetadata PackageSearchMetadata, List<PackageSource> PackageSources);
public record IdePackageResult(string PackageId, List<IdePackageFromSourceResult> PackageFromSources);
public record struct IdePackageFromSourceResult(IPackageSearchMetadata PackageSearchMetadata, PackageSource Source);
public class NugetClientService public class NugetClientService
{ {
private readonly bool _includePrerelease = false; private readonly bool _includePrerelease = false;
@@ -38,14 +41,14 @@ public class NugetClientService
log: logger, log: logger,
cancellationToken: cancellationToken).ConfigureAwait(false); cancellationToken: cancellationToken).ConfigureAwait(false);
packagesResult.AddRange(results.Select(s => new IdePackageResult(s, [source]))); packagesResult.AddRange(results.Select(s => new IdePackageResult(s.Identity.Id, [new IdePackageFromSourceResult(s, source)])));
} }
// Combine, group, and order by download count // Combine, group, and order by download count
var topPackages = packagesResult var topPackages = packagesResult
.GroupBy(p => p.PackageSearchMetadata.Identity.Id, StringComparer.OrdinalIgnoreCase) .GroupBy(p => p.PackageFromSources.First().PackageSearchMetadata.Identity.Id, StringComparer.OrdinalIgnoreCase)
.Select(g => g.First()) .Select(g => g.First())
.OrderByDescending(p => p.PackageSearchMetadata.DownloadCount ?? 0) .OrderByDescending(p => p.PackageFromSources.First().PackageSearchMetadata.DownloadCount ?? 0)
.Take(100) .Take(100)
.ToList(); .ToList();
@@ -63,7 +66,7 @@ public class NugetClientService
// we need to find out if other package sources have the package too // we need to find out if other package sources have the package too
foreach (var package in topPackages) foreach (var package in topPackages)
{ {
foreach (var source in packageSources.Except(package.PackageSources).ToList()) foreach (var source in packageSources.Except(package.PackageFromSources.Select(s => s.Source)).ToList())
{ {
var repository = Repository.Factory.GetCoreV3(source.Source); var repository = Repository.Factory.GetCoreV3(source.Source);
var searchResource = await repository.GetResourceAsync<PackageSearchResource>(cancellationToken).ConfigureAwait(false); var searchResource = await repository.GetResourceAsync<PackageSearchResource>(cancellationToken).ConfigureAwait(false);
@@ -71,19 +74,18 @@ public class NugetClientService
if (searchResource == null) if (searchResource == null)
continue; continue;
var packageId = package.PackageSearchMetadata.Identity.Id;
// TODO: Might be faster to use FindPackageByIdResource // TODO: Might be faster to use FindPackageByIdResource
var results = await searchResource.SearchAsync( var results = await searchResource.SearchAsync(
searchTerm: packageId, searchTerm: package.PackageId,
filters: new SearchFilter(includePrerelease: _includePrerelease), filters: new SearchFilter(includePrerelease: _includePrerelease),
skip: 0, skip: 0,
take: 2, take: 2,
log: logger, log: logger,
cancellationToken: cancellationToken).ConfigureAwait(false); cancellationToken: cancellationToken).ConfigureAwait(false);
var foundPackage = results.SingleOrDefault(r => r.Identity.Id.Equals(packageId, StringComparison.OrdinalIgnoreCase)); var foundPackage = results.SingleOrDefault(r => r.Identity.Id.Equals(package.PackageId, StringComparison.OrdinalIgnoreCase));
if (foundPackage != null) if (foundPackage != null)
{ {
package.PackageSources.Add(source); package.PackageFromSources.Add(new IdePackageFromSourceResult(foundPackage, source));
} }
} }
} }

View File

@@ -43,14 +43,16 @@ public partial class PackageEntry : MarginContainer
private void ApplyValues() private void ApplyValues()
{ {
if (PackageResult is null) return; if (PackageResult is null) return;
_packageNameLabel.Text = PackageResult.PackageSearchMetadata.Identity.Id; _packageNameLabel.Text = PackageResult.PackageId;
_currentVersionLabel.Text = string.Empty; _currentVersionLabel.Text = string.Empty;
_latestVersionLabel.Text = PackageResult.PackageSearchMetadata.Identity.Version.ToNormalizedString(); var highestVersionPackageFromSource = PackageResult.PackageFromSources
.MaxBy(p => p.PackageSearchMetadata.Identity.Version);
_latestVersionLabel.Text = highestVersionPackageFromSource.PackageSearchMetadata.Identity.Version.ToNormalizedString();
_sourceNamesContainer.QueueFreeChildren(); _sourceNamesContainer.QueueFreeChildren();
_ = Task.GodotRun(async () => _ = Task.GodotRun(async () =>
{ {
var (iconBytes, iconFormat) = await _nugetPackageIconCacheService.GetNugetPackageIcon(PackageResult.PackageSearchMetadata.Identity.Id, PackageResult.PackageSearchMetadata.IconUrl); var (iconBytes, iconFormat) = await _nugetPackageIconCacheService.GetNugetPackageIcon(PackageResult.PackageId, PackageResult.PackageFromSources.First().PackageSearchMetadata.IconUrl);
var image = new Image(); var image = new Image();
var error = iconFormat switch var error = iconFormat switch
{ {
@@ -66,9 +68,9 @@ public partial class PackageEntry : MarginContainer
} }
}); });
foreach (var (index, source) in PackageResult.PackageSources.Index()) foreach (var (index, packageFromSource) in PackageResult.PackageFromSources.Index())
{ {
var label = new Label { Text = source.Name }; var label = new Label { Text = packageFromSource.Source.Name };
var labelColour = SourceColors[index % SourceColors.Length]; var labelColour = SourceColors[index % SourceColors.Length];
label.AddThemeColorOverride(ThemeStringNames.FontColor, labelColour); label.AddThemeColorOverride(ThemeStringNames.FontColor, labelColour);
_sourceNamesContainer.AddChild(label); _sourceNamesContainer.AddChild(label);