From 1106365a149a2a890e1864793b81549ff0df06e3 Mon Sep 17 00:00:00 2001 From: Benjamin Cartier Date: Tue, 17 Sep 2019 16:53:44 -0400 Subject: [PATCH] Improved package retrieval --- .../Extensions/PackageSourceExtensions.cs | 19 ++++++++++++------- .../Extensions/UpdaterParametersExtension.cs | 9 --------- src/NuGet.Updater/NuGetUpdater.cs | 14 ++++++++++---- 3 files changed, 22 insertions(+), 20 deletions(-) diff --git a/src/NuGet.Updater/Extensions/PackageSourceExtensions.cs b/src/NuGet.Updater/Extensions/PackageSourceExtensions.cs index 30cd488..0f5587a 100644 --- a/src/NuGet.Updater/Extensions/PackageSourceExtensions.cs +++ b/src/NuGet.Updater/Extensions/PackageSourceExtensions.cs @@ -1,4 +1,5 @@ using System.Linq; +using System.Text; using System.Threading; using System.Threading.Tasks; using NuGet.Common; @@ -21,6 +22,8 @@ public static async Task GetPackage( Logger log = null ) { + var logMessage = new StringBuilder(); + var repositoryProvider = new SourceRepositoryProvider( Settings.LoadDefaultSettings(null), Repository.Provider.GetCoreV3() @@ -30,27 +33,29 @@ public static async Task GetPackage( var packageId = reference.Id; - log?.Write($"Retrieving package {packageId} from {source.SourceUri}"); + logMessage.AppendLine($"Retrieving package {packageId} from {source.SourceUri}"); - var metadata = (await repository + var packageMetadata = (await repository .GetResource() .GetMetadataAsync(packageId, true, false, new SourceCacheContext { NoCache = true }, new NullLogger(), ct)) .ToArray(); - log?.Write(metadata.Length > 0 ? $"Found {metadata.Length} versions" : "No versions found"); + logMessage.AppendLine(packageMetadata.Length > 0 ? $"Found {packageMetadata.Length} versions" : "No versions found"); - if(author.HasValue() && metadata.Any()) + if(author.HasValue() && packageMetadata.Any()) { - metadata = metadata.Where(m => m.HasAuthor(author)).ToArray(); + packageMetadata = packageMetadata.Where(m => m.HasAuthor(author)).ToArray(); - log?.Write(metadata.Length > 0 ? $"Found {metadata.Length} version from {author}" : $"No versions from {author} found"); + logMessage.AppendLine(packageMetadata.Length > 0 ? $"Found {packageMetadata.Length} versions from {author}" : $"No versions from {author} found"); } - var versions = metadata + var versions = packageMetadata .Cast() .Select(m => new UpdaterVersion(m.Version, source.SourceUri)) .ToArray(); + log?.Write(logMessage.ToString()); + return new UpdaterPackage(reference, versions); } } diff --git a/src/NuGet.Updater/Extensions/UpdaterParametersExtension.cs b/src/NuGet.Updater/Extensions/UpdaterParametersExtension.cs index 12507d5..32ac0f4 100644 --- a/src/NuGet.Updater/Extensions/UpdaterParametersExtension.cs +++ b/src/NuGet.Updater/Extensions/UpdaterParametersExtension.cs @@ -1,7 +1,6 @@ using System; using System.Collections.Generic; using System.Linq; -using NuGet.Configuration; using NuGet.Updater.Entities; using Uno.Extensions; @@ -9,14 +8,6 @@ namespace NuGet.Updater.Extensions { internal static class UpdaterParametersExtension { - internal static bool ShouldUpdatePackage(this UpdaterParameters parameters, UpdaterPackage package) - { - var isPackageToIgnore = parameters.PackagesToIgnore?.Contains(package.PackageId, StringComparer.OrdinalIgnoreCase) ?? false; - var isPackageToUpdate = parameters.PackagesToUpdate?.Contains(package.PackageId, StringComparer.OrdinalIgnoreCase) ?? true; - - return isPackageToUpdate && !isPackageToIgnore; - } - internal static IEnumerable GetSummary(this UpdaterParameters parameters) { yield return $"## Configuration"; diff --git a/src/NuGet.Updater/NuGetUpdater.cs b/src/NuGet.Updater/NuGetUpdater.cs index 8d8356b..0c37cdc 100644 --- a/src/NuGet.Updater/NuGetUpdater.cs +++ b/src/NuGet.Updater/NuGetUpdater.cs @@ -54,7 +54,7 @@ public async Task UpdatePackages(CancellationToken ct) //Open all the files at once so we don't have to do it all the time var documents = await OpenFiles(ct, packages); - foreach(var package in packages.Where(p => _parameters.ShouldUpdatePackage(p))) + foreach(var package in packages) { var latestVersion = package.LatestVersion; @@ -93,12 +93,18 @@ internal async Task GetPackages(CancellationToken ct) foreach(var reference in references.OrderBy(r => r.Id)) { - var matchingPackages = new List(); - foreach(var source in _parameters.Sources) + if(_parameters.PackagesToIgnore.Contains(reference.Id) || + (_parameters.PackagesToUpdate.Any() && !_parameters.PackagesToUpdate.Contains(reference.Id)) + ) { - matchingPackages.Add(await source.GetPackage(ct, reference, _parameters.PackageAuthor, _log)); + continue; } + var matchingPackages = await Task.WhenAll(_parameters + .Sources + .Select(source => source.GetPackage(ct, reference, _parameters.PackageAuthor, _log)) + ); + _log.Write(""); //If the reference has been found on multiple sources, we merge the packages found together