Skip to content

Commit

Permalink
Add parameter to select the type of output for version 3.0.0 (#142)
Browse files Browse the repository at this point in the history
  • Loading branch information
sensslen authored Oct 3, 2022
1 parent 05cfa53 commit d48224e
Show file tree
Hide file tree
Showing 47 changed files with 522 additions and 93 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ jobs:

steps:
# Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
- uses: actions/checkout@v2
- uses: actions/checkout@v3
- name: Setup dotnet 6.0.x
uses: actions/setup-dotnet@v1
uses: actions/setup-dotnet@v2
with:
dotnet-version: "6.0.x" # SDK Version to use (x uses the latest version).
# dotnet restore
Expand Down
8 changes: 4 additions & 4 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ jobs:
runs-on: ubuntu-latest
steps:
# Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
- uses: actions/checkout@v2
- uses: actions/checkout@v3
- name: Setup dotnet 6.0.x
uses: actions/setup-dotnet@v1
uses: actions/setup-dotnet@v2
with:
dotnet-version: "6.0.x" # SDK Version to use (x uses the latest version).
env:
Expand Down Expand Up @@ -43,12 +43,12 @@ jobs:
run: dotnet pack -c Release -o ./artifacts
# Zip Artifacts
- name: Zip artifacts
uses: tomchavakis/[email protected].0
uses: tomchavakis/[email protected].1
with:
args: zip -qq -r ./release.zip ./artifacts
- name: Create release
id: create_release
uses: actions/create-release@v1.0.0
uses: actions/create-release@v1.1.4
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
Expand Down
14 changes: 14 additions & 0 deletions .whitesource
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"scanSettings": {
"baseBranches": []
},
"checkRunSettings": {
"vulnerableCheckRunConclusionLevel": "failure",
"displayMode": "diff",
"useMendCheckNames": true
},
"issueSettings": {
"minSeverityLevel": "LOW",
"issueType": "DEPENDENCY"
}
}
1 change: 1 addition & 0 deletions NugetUtility.sln
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "SolutionItems", "SolutionItems", "{B72B75A4-ADBC-4F0B-82DA-0C8594437F5D}"
ProjectSection(SolutionItems) = preProject
.editorconfig = .editorconfig
README.md = README.md
EndProjectSection
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NuGetUtility.Test", "tests\NuGetUtility.Test\NuGetUtility.Test.csproj", "{1EBC8E4A-7677-4AF9-9C37-0CF81A9A689E}"
Expand Down
12 changes: 9 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Nuget License Utility [![Build Status](https://travis-ci.com/tomchavakis/nuget-license.svg?branch=develop)](https://travis-ci.com/tomchavakis/nuget-license.svg?branch=develop) [![NuGet](https://img.shields.io/nuget/v/dotnet-project-licenses.svg)](https://www.nuget.org/packages/dotnet-project-licenses)

A .net core tool to print the licenses of a project. This tool supports .NET Core and .NET Standard and .NET Framework Projects.
A .net core tool to print the licenses of a project. This tool supports .NET Core and .NET Standard and .NET Framework
Projects.

## dotnet-project-licenses tool

Expand Down Expand Up @@ -33,16 +34,21 @@ Usage: dotnet-project-licenses [options]
| `-mapping, --licenseurl-to-license-mappings` | When used, this option allows to override the url to license mapping built into the application (see [here](src/NuGetUtility/LicenseValidator/UrlToLicenseMapping.cs)) |
| `-override, --override-package-information` | When used, this option allows to override the package information used for the validation. This makes sure that no attempt is made to get the associated information about the package from the available web resources. This is useful for packages that e.g. provide a license file as part of the nuget package which (at the time of writing) cannot be used for validation and thus requires the package's information to be provided by this option. |
| `-d, --license-information-download-location` | When used, this option downloads the html content of the license URL to the specified folder. This is done for all NuGet packages that specify a license URL instead of providing the license expression. |
| `-o, --output` | This Parameter accepts the value `table` or `json`. It allows to select the type of output that should be given. If omitted, the output is given in tabular form. |

## Example tool commands

### Show help
```ps
dotnet-project-licenses --help
```

### Validate licenses for .csproj file
```ps
dotnet-project-licenses -i project.csproj
```
### Generate machine readable output
```ps
dotnet-project-licenses -i project.csproj -o json
```

## Docker

Expand Down
6 changes: 6 additions & 0 deletions renovate.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"$schema": "https://docs.renovatebot.com/renovate-schema.json",
"extends": [
"config:base"
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace NuGetUtility.LicenseValidator
{
public record struct LicenseValidationError(string Context,
public record LicenseValidationError(string Context,
string PackageId,
NuGetVersion PackageVersion,
string Message);
Expand Down
2 changes: 1 addition & 1 deletion src/NuGetUtility/LicenseValidator/ValidatedLicense.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace NuGetUtility.LicenseValidator
{
public record struct ValidatedLicense(string PackageId,
public record ValidatedLicense(string PackageId,
NuGetVersion PackageVersion,
string License,
LicenseInformationOrigin LicenseInformationOrigin);
Expand Down
10 changes: 10 additions & 0 deletions src/NuGetUtility/Output/IOuputFormatter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using NuGetUtility.LicenseValidator;

namespace NuGetUtility.Output
{
public interface IOutputFormatter
{
Task Write(Stream stream, IEnumerable<LicenseValidationError> errors);
Task Write(Stream stream, IEnumerable<ValidatedLicense> validated);
}
}
26 changes: 26 additions & 0 deletions src/NuGetUtility/Output/Json/JsonOutputFormatter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using NuGetUtility.LicenseValidator;
using System.Text.Json;

namespace NuGetUtility.Output.Json
{
public class JsonOutputFormatter : IOutputFormatter
{
private readonly JsonSerializerOptions _options;
public JsonOutputFormatter()
{
_options = new JsonSerializerOptions
{
Converters = { new NuGetVersionJsonConverter() }
};
}

public async Task Write(Stream stream, IEnumerable<LicenseValidationError> errors)
{
await JsonSerializer.SerializeAsync(stream, errors, _options);
}
public async Task Write(Stream stream, IEnumerable<ValidatedLicense> validated)
{
await JsonSerializer.SerializeAsync(stream, validated, _options);
}
}
}
20 changes: 20 additions & 0 deletions src/NuGetUtility/Output/Json/NuGetVersionJsonConverter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using NuGet.Versioning;
using System.Text.Json;
using System.Text.Json.Serialization;

namespace NuGetUtility.Output.Json
{
public class NuGetVersionJsonConverter : JsonConverter<NuGetVersion>
{
public override NuGetVersion? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
var readStringValue = reader.GetString();
return new NuGetVersion(readStringValue);
}

public override void Write(Utf8JsonWriter writer, NuGetVersion value, JsonSerializerOptions options)
{
writer.WriteStringValue(value.ToString());
}
}
}
34 changes: 34 additions & 0 deletions src/NuGetUtility/Output/Table/TableOutputFormatter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using NuGetUtility.LicenseValidator;

namespace NuGetUtility.Output.Table
{
public class TableOutputFormatter : IOutputFormatter
{
public async Task Write(Stream stream, IEnumerable<LicenseValidationError> errors)
{
await TablePrinterExtensions.Create(stream, "Context", "Package", "Version", "LicenseError")
.FromValues(errors,
error =>
{
return new object[] { error.Context, error.PackageId, error.PackageVersion, error.Message };
})
.Print();
}

public async Task Write(Stream stream, IEnumerable<ValidatedLicense> validated)
{
await TablePrinterExtensions
.Create(stream, "Package", "Version", "License Information Origin", "License Expression")
.FromValues(
validated,
license =>
{
return new object[]
{
license.PackageId, license.PackageVersion, license.LicenseInformationOrigin, license.License
};
})
.Print();
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
// ReSharper disable once CheckNamespace

using System.Text;

namespace Utilities
{
/// <summary>
Expand All @@ -9,10 +11,12 @@ public class TablePrinter
{
private readonly List<int> _lengths;
private readonly List<string[]> _rows = new List<string[]>();
private readonly Stream _stream;
private readonly string[] _titles;

public TablePrinter(params string[] titles)
public TablePrinter(Stream stream, params string[] titles)
{
_stream = stream;
_titles = titles;
_lengths = titles.Select(t => t.Length).ToList();
}
Expand All @@ -35,42 +39,40 @@ public void AddRow(params object?[] row)
}
}

public void Print()
public async Task Print()
{
_lengths.ForEach(l => Console.Write("+-" + new string('-', l) + '-'));
Console.WriteLine("+");
await using var writer = new StreamWriter(_stream, leaveOpen: true);

var line = "";
for (var i = 0; i < _titles.Length; i++)
await WriteSeparator(writer);
await WriteRow(_titles, writer);
await WriteSeparator(writer);

foreach (var row in _rows)
{
line += "| " + _titles[i].PadRight(_lengths[i]) + ' ';
await WriteRow(row, writer);
}

Console.WriteLine(line + "|");
await WriteSeparator(writer);
}
private async Task WriteRow(string[] values, TextWriter writer)
{
for (var i = 0; i < values.Length; i++)
{
await writer.WriteAsync("| ");
await writer.WriteAsync(values[i].PadRight(_lengths[i]));
await writer.WriteAsync(' ');
}

_lengths.ForEach(l => Console.Write("+-" + new string('-', l) + '-'));
Console.WriteLine("+");
await writer.WriteLineAsync("|");
}

foreach (var row in _rows)
private async Task WriteSeparator(TextWriter writer)
{
foreach (var l in _lengths)
{
line = "";
for (var i = 0; i < row.Length; i++)
{
if (int.TryParse(row[i], out var n))
{
line += "| " + row[i].PadLeft(_lengths[i]) + ' ';// numbers are padded to the left
}
else
{
line += "| " + row[i].PadRight(_lengths[i]) + ' ';
}
}

Console.WriteLine(line + "|");
await writer.WriteAsync("+-" + new string('-', l) + '-');
}

_lengths.ForEach(l => Console.Write("+-" + new string('-', l) + '-'));
Console.WriteLine("+");
await writer.WriteLineAsync("+");
}
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
using Utilities;

namespace NuGetUtility.ConsoleUtilities
namespace NuGetUtility.Output.Table
{
internal static class TablePrinterExtensions
{
public static TablePrinter Create(params string[] headings)
public static TablePrinter Create(Stream stream, params string[] headings)
{
return new TablePrinter(headings);
return new TablePrinter(stream, headings);
}

public static TablePrinter FromValues<T>(this TablePrinter printer,
Expand Down
8 changes: 8 additions & 0 deletions src/NuGetUtility/OutputType.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace NuGetUtility
{
public enum OutputType
{
Table,
Json
}
}
Loading

0 comments on commit d48224e

Please sign in to comment.