This repository has been archived by the owner on Jul 12, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 123
/
Copy pathBulkPackageLookup.cs
85 lines (74 loc) · 2.93 KB
/
BulkPackageLookup.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using NuGet.Packaging.Core;
using NuKeeper.Abstractions.Configuration;
using NuKeeper.Abstractions.NuGet;
using NuKeeper.Abstractions.NuGetApi;
namespace NuKeeper.Inspection.NuGetApi
{
public class BulkPackageLookup : IBulkPackageLookup
{
private readonly IApiPackageLookup _packageLookup;
private readonly IPackageLookupResultReporter _lookupReporter;
public BulkPackageLookup(
IApiPackageLookup packageLookup,
IPackageLookupResultReporter lookupReporter)
{
_packageLookup = packageLookup;
_lookupReporter = lookupReporter;
}
public async Task<IDictionary<PackageIdentity, PackageLookupResult>> FindVersionUpdates(
IEnumerable<PackageIdentity> packages,
NuGetSources sources,
VersionChange allowedChange,
UsePrerelease usePrerelease
)
{
var lookupTasks = packages
.Distinct()
.GroupBy(pi => (pi.Id, MaxVersion: GetMaxVersion(pi, allowedChange)))
.Select(HighestVersion)
.Select(id => new { Package = id, Update = _packageLookup.FindVersionUpdate(id, sources, allowedChange, usePrerelease) })
.ToList();
await Task.WhenAll(lookupTasks.Select(l => l.Update));
var result = new Dictionary<PackageIdentity, PackageLookupResult>();
foreach (var lookupTask in lookupTasks)
{
ProcessLookupResult(lookupTask.Package, lookupTask.Update.Result, result);
}
return result;
}
private static string GetMaxVersion(PackageIdentity pi, VersionChange allowedChange)
{
return allowedChange switch
{
VersionChange.Major => "X.X.X",
VersionChange.Minor => $"{pi.Version.Major}.X.X",
VersionChange.Patch => $"{pi.Version.Major}.{pi.Version.Minor}.X",
VersionChange.None => $"{pi.Version.Major}.{pi.Version.Minor}.{pi.Version.Patch}",
_ => throw new ArgumentOutOfRangeException(nameof(allowedChange)),
};
}
private void ProcessLookupResult(
PackageIdentity package,
PackageLookupResult packageLookup,
IDictionary<PackageIdentity, PackageLookupResult> result
)
{
var selectedVersion = packageLookup.Selected();
if (selectedVersion?.Identity?.Version != null)
{
_lookupReporter.Report(packageLookup);
result.Add(package, packageLookup);
}
}
private PackageIdentity HighestVersion(IEnumerable<PackageIdentity> packages)
{
return packages
.OrderByDescending(p => p.Version)
.FirstOrDefault();
}
}
}