Skip to content

Commit

Permalink
Use local cache to retreive package information (#156)
Browse files Browse the repository at this point in the history
  • Loading branch information
sensslen authored Nov 2, 2022
1 parent c5f89ff commit c624fb6
Show file tree
Hide file tree
Showing 48 changed files with 607 additions and 698 deletions.
5 changes: 3 additions & 2 deletions src/NuGetUtility/LicenseValidator/LicenseDownloadException.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using NuGet.Packaging.Core;
using NuGetUtility.Wrapper.NuGetWrapper.Packaging.Core;

namespace NuGetUtility.LicenseValidator
{
Expand All @@ -8,6 +8,7 @@ public LicenseDownloadException(Exception inner, string context, PackageIdentity
:
base(
$"Failed to download license for package {packageInfo.Id} ({packageInfo.Version}).\nContext: {context}",
inner) { }
inner)
{ }
}
}
4 changes: 2 additions & 2 deletions src/NuGetUtility/LicenseValidator/LicenseValidationResult.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
using NuGet.Versioning;
using NuGetUtility.Wrapper.NuGetWrapper.Versioning;

namespace NuGetUtility.LicenseValidator
{
public record LicenseValidationResult(string PackageId,
NuGetVersion PackageVersion,
INuGetVersion PackageVersion,
string? PackageProjectUrl,
string? License,
LicenseInformationOrigin LicenseInformationOrigin,
Expand Down
17 changes: 8 additions & 9 deletions src/NuGetUtility/LicenseValidator/LicenseValidator.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
using NuGet.Packaging;
using NuGet.Protocol.Core.Types;
using NuGet.Versioning;
using NuGetUtility.PackageInformationReader;
using NuGetUtility.Wrapper.HttpClientWrapper;
using NuGetUtility.Wrapper.NuGetWrapper.Packaging;
using NuGetUtility.Wrapper.NuGetWrapper.Versioning;
using System.Collections.Concurrent;

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

private void AddOrUpdateLicense(
ConcurrentDictionary<LicenseNameAndVersion, LicenseValidationResult> result,
IPackageSearchMetadata info,
IPackageMetadata info,
LicenseInformationOrigin origin,
ValidationError error,
string? license = null)
Expand All @@ -68,7 +67,7 @@ private void AddOrUpdateLicense(

private void AddOrUpdateLicense(
ConcurrentDictionary<LicenseNameAndVersion, LicenseValidationResult> result,
IPackageSearchMetadata info,
IPackageMetadata info,
LicenseInformationOrigin origin,
string? license = null)
{
Expand Down Expand Up @@ -101,7 +100,7 @@ private LicenseValidationResult CreateResult(LicenseNameAndVersion _, LicenseVal
return newValue;
}

private void ValidateLicenseByMetadata(IPackageSearchMetadata info,
private void ValidateLicenseByMetadata(IPackageMetadata info,
string context,
ConcurrentDictionary<LicenseNameAndVersion, LicenseValidationResult> result)
{
Expand Down Expand Up @@ -137,11 +136,11 @@ private void ValidateLicenseByMetadata(IPackageSearchMetadata info,
}
}

private async Task ValidateLicenseByUrl(IPackageSearchMetadata info,
private async Task ValidateLicenseByUrl(IPackageMetadata info,
string context,
ConcurrentDictionary<LicenseNameAndVersion, LicenseValidationResult> result)
{
if (info.LicenseUrl.IsAbsoluteUri)
if (info.LicenseUrl!.IsAbsoluteUri)
{
try
{
Expand Down Expand Up @@ -212,6 +211,6 @@ private string GetLicenseNotAllowedMessage(string license)
return $"License {license} not found in list of supported licenses";
}

private record LicenseNameAndVersion(string LicenseName, NuGetVersion Version);
private record LicenseNameAndVersion(string LicenseName, INuGetVersion Version);
}
}
10 changes: 5 additions & 5 deletions src/NuGetUtility/NuGetUtility.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="McMaster.Extensions.CommandLineUtils" Version="4.0.1"/>
<PackageReference Include="Microsoft.Build" ExcludeAssets="runtime" Version="17.3.1"/>
<PackageReference Include="Microsoft.Build.Locator" Version="1.4.1"/>
<PackageReference Include="NuGet.Commands" Version="6.3.0"/>
<PackageReference Include="NuGet.Packaging" Version="6.3.0"/>
<PackageReference Include="McMaster.Extensions.CommandLineUtils" Version="4.0.1" />
<PackageReference Include="Microsoft.Build" ExcludeAssets="runtime" Version="17.3.2" />
<PackageReference Include="Microsoft.Build.Locator" Version="1.4.1" />
<PackageReference Include="NuGet.Commands" Version="6.3.1" />
<PackageReference Include="NuGet.Packaging" Version="6.3.1" />
</ItemGroup>

</Project>
6 changes: 3 additions & 3 deletions src/NuGetUtility/Output/Json/JsonOutputFormatter.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using NuGetUtility.LicenseValidator;
using NuGetUtility.LicenseValidator;
using NuGetUtility.Serialization;
using System.Text.Json;

namespace NuGetUtility.Output.Json
Expand All @@ -12,8 +13,7 @@ public JsonOutputFormatter(bool prettyPrint = false, bool printErrorsOnly = fals
_printErrorsOnly = printErrorsOnly;
_options = new JsonSerializerOptions
{
Converters =
{ new NuGetVersionJsonConverter(), new ValidatedLicenseJsonConverterWithOmittingEmptyErrorList() },
Converters = { new NuGetVersionJsonConverter(), new ValidatedLicenseJsonConverterWithOmittingEmptyErrorList() },
WriteIndented = prettyPrint
};
}
Expand Down
20 changes: 0 additions & 20 deletions src/NuGetUtility/Output/Json/NuGetVersionJsonConverter.cs

This file was deleted.

23 changes: 11 additions & 12 deletions src/NuGetUtility/Output/Table/TableOutputFormatter.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using NuGetUtility.LicenseValidator;
using NuGetUtility.LicenseValidator;

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

public async Task Write(Stream stream, IList<LicenseValidationResult> results)
{
var errorColumnDefinition = new ColumnDefinition("Error",
license => string.Join(Environment.NewLine, license.ValidationErrors.Select(e => e.Error)));
var errorColumnDefinition = new ColumnDefinition("Error", license => license.ValidationErrors.Select(e => e.Error), license => license.ValidationErrors.Any());
var columnDefinitions = new[]
{
new ColumnDefinition("Package", license => license.PackageId, true),
new ColumnDefinition("Version", license => license.PackageVersion.ToString() ,true),
new ColumnDefinition("License Information Origin", license => license.LicenseInformationOrigin.ToString(), true),
new ColumnDefinition("License Expression", license => license.License ?? string.Empty),
new ColumnDefinition("Package Project Url",license => license.PackageProjectUrl??string.Empty),
new ColumnDefinition("Package", license => license.PackageId, license => true, true),
new ColumnDefinition("Version", license => license.PackageVersion, license => true, true),
new ColumnDefinition("License Information Origin", license => license.LicenseInformationOrigin, license => true, true),
new ColumnDefinition("License Expression", license => license.License, license => license.License != null),
new ColumnDefinition("Package Project Url",license => license.PackageProjectUrl, license => license.PackageProjectUrl != null),
errorColumnDefinition,
new ColumnDefinition("Error Context", license => string.Join(Environment.NewLine, license.ValidationErrors.Select(e => e.Context))),
new ColumnDefinition("Error Context", license => license.ValidationErrors.Select(e => e.Context), license => license.ValidationErrors.Any()),
};

foreach (var license in results)
{
foreach (var definition in columnDefinitions)
{
definition.Enabled |= !string.IsNullOrWhiteSpace(definition.StringAccessor(license));
definition.Enabled |= definition.IsRelevant(license);
}
}

Expand All @@ -43,11 +42,11 @@ await TablePrinterExtensions
.Create(stream, relevantColumns.Select(d => d.Title))
.FromValues(
results,
license => relevantColumns.Select(d => d.StringAccessor(license)))
license => relevantColumns.Select(d => d.PropertyAccessor(license)))
.Print();
}

private record ColumnDefinition(string Title, Func<LicenseValidationResult, string> StringAccessor, bool Enabled = false)
private record ColumnDefinition(string Title, Func<LicenseValidationResult, object?> PropertyAccessor, Func<LicenseValidationResult, bool> IsRelevant, bool Enabled = false)
{
public bool Enabled { get; set; } = Enabled;
}
Expand Down
31 changes: 12 additions & 19 deletions src/NuGetUtility/Output/Table/TablePrinter.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
// ReSharper disable once CheckNamespace

namespace Utilities
namespace NuGetUtility.Output.Table
{
/// <summary>
/// Credits: https://stackoverflow.com/a/54943087/1199089
Expand All @@ -19,15 +17,15 @@ public TablePrinter(Stream stream, IEnumerable<string> titles)
_lengths = _titles.Select(t => t.Length).ToArray();
}

public void AddRow(string?[] row)
public void AddRow(object?[] row)
{
if (row.Length != _titles.Length)
{
throw new Exception(
$"Added row length [{row.Length}] is not equal to title row length [{_titles.Length}]");
}

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

private string[] GetLines(object? lines)
{
if (lines is IEnumerable<object> enumerable)
{
return enumerable.Select(o => o.ToString() ?? string.Empty).ToArray();
}
return new[] { lines?.ToString() ?? string.Empty };
}

public async Task Print()
{
await using var writer = new StreamWriter(_stream, leaveOpen: true);
Expand Down Expand Up @@ -83,19 +90,5 @@ private async Task WriteSeparator(TextWriter writer)
}
await writer.WriteLineAsync("+");
}

/// <summary>
/// Credit: https://stackoverflow.com/a/23408020/1199089
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
private static IEnumerable<string> SplitToLines(string input)
{
using var reader = new StringReader(input);
while (reader.ReadLine() is { } line)
{
yield return line;
}
}
}
}
4 changes: 1 addition & 3 deletions src/NuGetUtility/Output/Table/TablePrinterExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
using Utilities;

namespace NuGetUtility.Output.Table
{
internal static class TablePrinterExtensions
Expand All @@ -15,7 +13,7 @@ public static TablePrinter Create(Stream stream, IEnumerable<string> headings)

public static TablePrinter FromValues<T>(this TablePrinter printer,
IEnumerable<T> values,
Func<T, IEnumerable<string?>> formatter)
Func<T, IEnumerable<object?>> formatter)
{
foreach (var value in values)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using NuGet.Versioning;
using NuGetUtility.Wrapper.NuGetWrapper.Versioning;

namespace NuGetUtility.PackageInformationReader
{
public record struct CustomPackageInformation(string Id, NuGetVersion Version, string License);
public record struct CustomPackageInformation(string Id, INuGetVersion Version, string License);
}
Loading

0 comments on commit c624fb6

Please sign in to comment.