Skip to content

Commit

Permalink
Merge pull request #21 from nventive/dev/beca/package
Browse files Browse the repository at this point in the history
Improved package retrieval
  • Loading branch information
Benjamin Cartier authored Sep 17, 2019
2 parents 5362121 + 1106365 commit 1ce164c
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 20 deletions.
19 changes: 12 additions & 7 deletions src/NuGet.Updater/Extensions/PackageSourceExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using NuGet.Common;
Expand All @@ -21,6 +22,8 @@ public static async Task<UpdaterPackage> GetPackage(
Logger log = null
)
{
var logMessage = new StringBuilder();

var repositoryProvider = new SourceRepositoryProvider(
Settings.LoadDefaultSettings(null),
Repository.Provider.GetCoreV3()
Expand All @@ -30,27 +33,29 @@ public static async Task<UpdaterPackage> 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<PackageMetadataResource>()
.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<PackageSearchMetadataRegistration>()
.Select(m => new UpdaterVersion(m.Version, source.SourceUri))
.ToArray();

log?.Write(logMessage.ToString());

return new UpdaterPackage(reference, versions);
}
}
Expand Down
9 changes: 0 additions & 9 deletions src/NuGet.Updater/Extensions/UpdaterParametersExtension.cs
Original file line number Diff line number Diff line change
@@ -1,22 +1,13 @@
using System;
using System.Collections.Generic;
using System.Linq;
using NuGet.Configuration;
using NuGet.Updater.Entities;
using Uno.Extensions;

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<string> GetSummary(this UpdaterParameters parameters)
{
yield return $"## Configuration";
Expand Down
14 changes: 10 additions & 4 deletions src/NuGet.Updater/NuGetUpdater.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public async Task<bool> 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;

Expand Down Expand Up @@ -93,12 +93,18 @@ internal async Task<UpdaterPackage[]> GetPackages(CancellationToken ct)

foreach(var reference in references.OrderBy(r => r.Id))
{
var matchingPackages = new List<UpdaterPackage>();
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
Expand Down

0 comments on commit 1ce164c

Please sign in to comment.