Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/NuGetLicense/CommandLineOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ public class CommandLineOptions
public string? InputFile { get; set; }
public string? InputJsonFile { get; set; }
public bool IncludeTransitive { get; set; }
public bool SkipInvalidProjects { get; set; }
public string? AllowedLicenses { get; set; }
public string? IgnoredPackages { get; set; }
public string? LicenseMapping { get; set; }
Expand Down
18 changes: 17 additions & 1 deletion src/NuGetLicense/LicenseValidationOrchestrator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System.Collections.Immutable;
using System.IO.Abstractions;
using Microsoft.Build.Exceptions;
using NuGet.Configuration;
using NuGet.Protocol.Core.Types;
using NuGetLicense.LicenseValidator;
Expand Down Expand Up @@ -73,7 +74,7 @@ public async Task<int> ValidateAsync(CommandLineOptions options, CancellationTok

string[] excludedProjectsArray = _optionsParser.GetExcludedProjects(options.ExcludedProjects);
IEnumerable<string> projects = (await inputFiles.SelectManyAsync(projectCollector.GetProjectsAsync)).Where(p => !Array.Exists(excludedProjectsArray, ignored => p.Like(ignored)));
IEnumerable<ProjectWithReferencedPackages> packagesForProject = GetPackagesPerProject(projects, projectReader, options.IncludeTransitive, options.TargetFramework, options.IncludeSharedProjects, out IReadOnlyCollection<Exception> projectReaderExceptions);
IEnumerable<ProjectWithReferencedPackages> packagesForProject = GetPackagesPerProject(projects, projectReader, options.IncludeTransitive, options.SkipInvalidProjects, options.TargetFramework, options.IncludeSharedProjects, out IReadOnlyCollection<Exception> projectReaderExceptions);
IAsyncEnumerable<ReferencedPackageWithContext> downloadedLicenseInformation =
packagesForProject.SelectMany(p => GetPackageInformations(p, overridePackageInformationArray, cancellationToken));
var results = (await validator.Validate(downloadedLicenseInformation, cancellationToken)).ToList();
Expand Down Expand Up @@ -136,6 +137,7 @@ private static IReadOnlyCollection<ProjectWithReferencedPackages> GetPackagesPer
IEnumerable<string> projects,
ReferencedPackageReader reader,
bool includeTransitive,
bool skipInvalidProjects,
string? targetFramework,
bool includeSharedProjects,
out IReadOnlyCollection<Exception> exceptions)
Expand All @@ -152,6 +154,20 @@ private static IReadOnlyCollection<ProjectWithReferencedPackages> GetPackagesPer
IEnumerable<PackageIdentity> installedPackages = reader.GetInstalledPackages(project, includeTransitive, targetFramework);
result.Add(new ProjectWithReferencedPackages(project, installedPackages));
}
catch (InvalidProjectFileException ipfe)
{
if (!skipInvalidProjects)
{
encounteredExceptions.Add(ipfe);
}
}
catch (ReferencedPackageReaderException rpre) when (rpre.Message.Contains("not compatible with"))
Comment thread
theolivenbaum marked this conversation as resolved.
{
if (!skipInvalidProjects)
{
encounteredExceptions.Add(rpre);
}
}
Comment thread
theolivenbaum marked this conversation as resolved.
catch (Exception e)
{
encounteredExceptions.Add(e);
Expand Down
7 changes: 7 additions & 0 deletions src/NuGetLicense/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ public static RootCommand CreateRootCommand()
Description = "If set, the whole license tree is followed in order to determine all nuget's used by the projects"
};

var skipInvalidProjectsOption = new Option<bool>("-skip", "--skip-invalid-projects")
{
Description = "If set, will skip any invalid projects not supported (for example, .pyproj projects)"
};

var allowedLicensesOption = new Option<string?>("-a", "--allowed-license-types")
{
Description = "Specifies allowed license types. You can provide either a JSON file containing an array of license types, or a semicolon-separated list of license identifiers (e.g., \"MIT;Apache-2.0;BSD-3-Clause\")."
Expand Down Expand Up @@ -103,6 +108,7 @@ public static RootCommand CreateRootCommand()
rootCommand.Options.Add(inputFileOption);
rootCommand.Options.Add(inputJsonFileOption);
rootCommand.Options.Add(includeTransitiveOption);
rootCommand.Options.Add(skipInvalidProjectsOption);
rootCommand.Options.Add(allowedLicensesOption);
rootCommand.Options.Add(ignoredPackagesOption);
rootCommand.Options.Add(licenseMappingOption);
Expand Down Expand Up @@ -138,6 +144,7 @@ public static RootCommand CreateRootCommand()
InputFile = inputFile,
InputJsonFile = inputJsonFile,
IncludeTransitive = parseResult.GetValue(includeTransitiveOption),
SkipInvalidProjects = parseResult.GetValue(skipInvalidProjectsOption),
AllowedLicenses = parseResult.GetValue(allowedLicensesOption),
IgnoredPackages = parseResult.GetValue(ignoredPackagesOption),
LicenseMapping = parseResult.GetValue(licenseMappingOption),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,7 @@ public async Task ValidateAsync_UsesAllConfiguredOptions()
{
InputFile = "/test/project.csproj",
IncludeTransitive = true,
SkipInvalidProjects = false,
TargetFramework = "net8.0",
IncludeSharedProjects = true,
AllowedLicenses = "MIT",
Expand Down