Skip to content

Commit c624fb6

Browse files
authored
Use local cache to retreive package information (#156)
1 parent c5f89ff commit c624fb6

File tree

48 files changed

+607
-698
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+607
-698
lines changed
Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using NuGet.Packaging.Core;
1+
using NuGetUtility.Wrapper.NuGetWrapper.Packaging.Core;
22

33
namespace NuGetUtility.LicenseValidator
44
{
@@ -8,6 +8,7 @@ public LicenseDownloadException(Exception inner, string context, PackageIdentity
88
:
99
base(
1010
$"Failed to download license for package {packageInfo.Id} ({packageInfo.Version}).\nContext: {context}",
11-
inner) { }
11+
inner)
12+
{ }
1213
}
1314
}

src/NuGetUtility/LicenseValidator/LicenseValidationResult.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
using NuGet.Versioning;
1+
using NuGetUtility.Wrapper.NuGetWrapper.Versioning;
22

33
namespace NuGetUtility.LicenseValidator
44
{
55
public record LicenseValidationResult(string PackageId,
6-
NuGetVersion PackageVersion,
6+
INuGetVersion PackageVersion,
77
string? PackageProjectUrl,
88
string? License,
99
LicenseInformationOrigin LicenseInformationOrigin,

src/NuGetUtility/LicenseValidator/LicenseValidator.cs

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
using NuGet.Packaging;
2-
using NuGet.Protocol.Core.Types;
3-
using NuGet.Versioning;
41
using NuGetUtility.PackageInformationReader;
52
using NuGetUtility.Wrapper.HttpClientWrapper;
3+
using NuGetUtility.Wrapper.NuGetWrapper.Packaging;
4+
using NuGetUtility.Wrapper.NuGetWrapper.Versioning;
65
using System.Collections.Concurrent;
76

87
namespace NuGetUtility.LicenseValidator
@@ -49,7 +48,7 @@ public async Task<IEnumerable<LicenseValidationResult>> Validate(
4948

5049
private void AddOrUpdateLicense(
5150
ConcurrentDictionary<LicenseNameAndVersion, LicenseValidationResult> result,
52-
IPackageSearchMetadata info,
51+
IPackageMetadata info,
5352
LicenseInformationOrigin origin,
5453
ValidationError error,
5554
string? license = null)
@@ -68,7 +67,7 @@ private void AddOrUpdateLicense(
6867

6968
private void AddOrUpdateLicense(
7069
ConcurrentDictionary<LicenseNameAndVersion, LicenseValidationResult> result,
71-
IPackageSearchMetadata info,
70+
IPackageMetadata info,
7271
LicenseInformationOrigin origin,
7372
string? license = null)
7473
{
@@ -101,7 +100,7 @@ private LicenseValidationResult CreateResult(LicenseNameAndVersion _, LicenseVal
101100
return newValue;
102101
}
103102

104-
private void ValidateLicenseByMetadata(IPackageSearchMetadata info,
103+
private void ValidateLicenseByMetadata(IPackageMetadata info,
105104
string context,
106105
ConcurrentDictionary<LicenseNameAndVersion, LicenseValidationResult> result)
107106
{
@@ -137,11 +136,11 @@ private void ValidateLicenseByMetadata(IPackageSearchMetadata info,
137136
}
138137
}
139138

140-
private async Task ValidateLicenseByUrl(IPackageSearchMetadata info,
139+
private async Task ValidateLicenseByUrl(IPackageMetadata info,
141140
string context,
142141
ConcurrentDictionary<LicenseNameAndVersion, LicenseValidationResult> result)
143142
{
144-
if (info.LicenseUrl.IsAbsoluteUri)
143+
if (info.LicenseUrl!.IsAbsoluteUri)
145144
{
146145
try
147146
{
@@ -212,6 +211,6 @@ private string GetLicenseNotAllowedMessage(string license)
212211
return $"License {license} not found in list of supported licenses";
213212
}
214213

215-
private record LicenseNameAndVersion(string LicenseName, NuGetVersion Version);
214+
private record LicenseNameAndVersion(string LicenseName, INuGetVersion Version);
216215
}
217216
}

src/NuGetUtility/NuGetUtility.csproj

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,11 @@
2424
</PropertyGroup>
2525

2626
<ItemGroup>
27-
<PackageReference Include="McMaster.Extensions.CommandLineUtils" Version="4.0.1"/>
28-
<PackageReference Include="Microsoft.Build" ExcludeAssets="runtime" Version="17.3.1"/>
29-
<PackageReference Include="Microsoft.Build.Locator" Version="1.4.1"/>
30-
<PackageReference Include="NuGet.Commands" Version="6.3.0"/>
31-
<PackageReference Include="NuGet.Packaging" Version="6.3.0"/>
27+
<PackageReference Include="McMaster.Extensions.CommandLineUtils" Version="4.0.1" />
28+
<PackageReference Include="Microsoft.Build" ExcludeAssets="runtime" Version="17.3.2" />
29+
<PackageReference Include="Microsoft.Build.Locator" Version="1.4.1" />
30+
<PackageReference Include="NuGet.Commands" Version="6.3.1" />
31+
<PackageReference Include="NuGet.Packaging" Version="6.3.1" />
3232
</ItemGroup>
3333

3434
</Project>

src/NuGetUtility/Output/Json/JsonOutputFormatter.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using NuGetUtility.LicenseValidator;
1+
using NuGetUtility.LicenseValidator;
2+
using NuGetUtility.Serialization;
23
using System.Text.Json;
34

45
namespace NuGetUtility.Output.Json
@@ -12,8 +13,7 @@ public JsonOutputFormatter(bool prettyPrint = false, bool printErrorsOnly = fals
1213
_printErrorsOnly = printErrorsOnly;
1314
_options = new JsonSerializerOptions
1415
{
15-
Converters =
16-
{ new NuGetVersionJsonConverter(), new ValidatedLicenseJsonConverterWithOmittingEmptyErrorList() },
16+
Converters = { new NuGetVersionJsonConverter(), new ValidatedLicenseJsonConverterWithOmittingEmptyErrorList() },
1717
WriteIndented = prettyPrint
1818
};
1919
}

src/NuGetUtility/Output/Json/NuGetVersionJsonConverter.cs

Lines changed: 0 additions & 20 deletions
This file was deleted.

src/NuGetUtility/Output/Table/TableOutputFormatter.cs

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using NuGetUtility.LicenseValidator;
1+
using NuGetUtility.LicenseValidator;
22

33
namespace NuGetUtility.Output.Table
44
{
@@ -12,24 +12,23 @@ public TableOutputFormatter(bool printErrorsOnly = false)
1212

1313
public async Task Write(Stream stream, IList<LicenseValidationResult> results)
1414
{
15-
var errorColumnDefinition = new ColumnDefinition("Error",
16-
license => string.Join(Environment.NewLine, license.ValidationErrors.Select(e => e.Error)));
15+
var errorColumnDefinition = new ColumnDefinition("Error", license => license.ValidationErrors.Select(e => e.Error), license => license.ValidationErrors.Any());
1716
var columnDefinitions = new[]
1817
{
19-
new ColumnDefinition("Package", license => license.PackageId, true),
20-
new ColumnDefinition("Version", license => license.PackageVersion.ToString() ,true),
21-
new ColumnDefinition("License Information Origin", license => license.LicenseInformationOrigin.ToString(), true),
22-
new ColumnDefinition("License Expression", license => license.License ?? string.Empty),
23-
new ColumnDefinition("Package Project Url",license => license.PackageProjectUrl??string.Empty),
18+
new ColumnDefinition("Package", license => license.PackageId, license => true, true),
19+
new ColumnDefinition("Version", license => license.PackageVersion, license => true, true),
20+
new ColumnDefinition("License Information Origin", license => license.LicenseInformationOrigin, license => true, true),
21+
new ColumnDefinition("License Expression", license => license.License, license => license.License != null),
22+
new ColumnDefinition("Package Project Url",license => license.PackageProjectUrl, license => license.PackageProjectUrl != null),
2423
errorColumnDefinition,
25-
new ColumnDefinition("Error Context", license => string.Join(Environment.NewLine, license.ValidationErrors.Select(e => e.Context))),
24+
new ColumnDefinition("Error Context", license => license.ValidationErrors.Select(e => e.Context), license => license.ValidationErrors.Any()),
2625
};
2726

2827
foreach (var license in results)
2928
{
3029
foreach (var definition in columnDefinitions)
3130
{
32-
definition.Enabled |= !string.IsNullOrWhiteSpace(definition.StringAccessor(license));
31+
definition.Enabled |= definition.IsRelevant(license);
3332
}
3433
}
3534

@@ -43,11 +42,11 @@ await TablePrinterExtensions
4342
.Create(stream, relevantColumns.Select(d => d.Title))
4443
.FromValues(
4544
results,
46-
license => relevantColumns.Select(d => d.StringAccessor(license)))
45+
license => relevantColumns.Select(d => d.PropertyAccessor(license)))
4746
.Print();
4847
}
4948

50-
private record ColumnDefinition(string Title, Func<LicenseValidationResult, string> StringAccessor, bool Enabled = false)
49+
private record ColumnDefinition(string Title, Func<LicenseValidationResult, object?> PropertyAccessor, Func<LicenseValidationResult, bool> IsRelevant, bool Enabled = false)
5150
{
5251
public bool Enabled { get; set; } = Enabled;
5352
}

src/NuGetUtility/Output/Table/TablePrinter.cs

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
// ReSharper disable once CheckNamespace
2-
3-
namespace Utilities
1+
namespace NuGetUtility.Output.Table
42
{
53
/// <summary>
64
/// Credits: https://stackoverflow.com/a/54943087/1199089
@@ -19,15 +17,15 @@ public TablePrinter(Stream stream, IEnumerable<string> titles)
1917
_lengths = _titles.Select(t => t.Length).ToArray();
2018
}
2119

22-
public void AddRow(string?[] row)
20+
public void AddRow(object?[] row)
2321
{
2422
if (row.Length != _titles.Length)
2523
{
2624
throw new Exception(
2725
$"Added row length [{row.Length}] is not equal to title row length [{_titles.Length}]");
2826
}
2927

30-
var rowElements = row.Select(item => SplitToLines(item?.ToString() ?? string.Empty).ToArray()).ToArray();
28+
var rowElements = row.Select(GetLines).ToArray();
3129
for (var i = 0; i < _titles.Length; i++)
3230
{
3331
var maxLineLength = rowElements[i].Any() ? rowElements[i].Max(line => line.Length) : 0;
@@ -39,6 +37,15 @@ public void AddRow(string?[] row)
3937
_rows.Add(rowElements);
4038
}
4139

40+
private string[] GetLines(object? lines)
41+
{
42+
if (lines is IEnumerable<object> enumerable)
43+
{
44+
return enumerable.Select(o => o.ToString() ?? string.Empty).ToArray();
45+
}
46+
return new[] { lines?.ToString() ?? string.Empty };
47+
}
48+
4249
public async Task Print()
4350
{
4451
await using var writer = new StreamWriter(_stream, leaveOpen: true);
@@ -83,19 +90,5 @@ private async Task WriteSeparator(TextWriter writer)
8390
}
8491
await writer.WriteLineAsync("+");
8592
}
86-
87-
/// <summary>
88-
/// Credit: https://stackoverflow.com/a/23408020/1199089
89-
/// </summary>
90-
/// <param name="input"></param>
91-
/// <returns></returns>
92-
private static IEnumerable<string> SplitToLines(string input)
93-
{
94-
using var reader = new StringReader(input);
95-
while (reader.ReadLine() is { } line)
96-
{
97-
yield return line;
98-
}
99-
}
10093
}
10194
}

src/NuGetUtility/Output/Table/TablePrinterExtensions.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
using Utilities;
2-
31
namespace NuGetUtility.Output.Table
42
{
53
internal static class TablePrinterExtensions
@@ -15,7 +13,7 @@ public static TablePrinter Create(Stream stream, IEnumerable<string> headings)
1513

1614
public static TablePrinter FromValues<T>(this TablePrinter printer,
1715
IEnumerable<T> values,
18-
Func<T, IEnumerable<string?>> formatter)
16+
Func<T, IEnumerable<object?>> formatter)
1917
{
2018
foreach (var value in values)
2119
{
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
using NuGet.Versioning;
1+
using NuGetUtility.Wrapper.NuGetWrapper.Versioning;
22

33
namespace NuGetUtility.PackageInformationReader
44
{
5-
public record struct CustomPackageInformation(string Id, NuGetVersion Version, string License);
5+
public record struct CustomPackageInformation(string Id, INuGetVersion Version, string License);
66
}

0 commit comments

Comments
 (0)