Skip to content

Commit

Permalink
add mechanism to exclude projects (#71)
Browse files Browse the repository at this point in the history
  • Loading branch information
sensslen authored Jul 25, 2024
1 parent 42c3660 commit 7a2bd93
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ Usage: nuget-license [options]
| `-t, --include-transitive` | When set, the analysis includes transitive packages (dependencies of packages that are directly installed to the project) |
| `-a, --allowed-license-types` | File containing all allowed licenses in JSON format. If omitted, all licenses are considered to be allowed. |
| `-ignore, --ignored-packages` | File containing a JSON formatted array containing package names, that should be ignored when validating licenses. Package names specified can contain simple Wildcard characters (*) which are used to match any number of characters. Note that even though a package is ignored, it's transitive dependencies are still validated. This Option is useful e.g. to exclude homegrown nuget packages from validation. |
| `-exclude-projects, --exclude-projects-matching` | This option allows to specify project name(s) to exclude from the analysis. This can be useful to exclude test projects from the analysis when supplying a solution file as input. Wildcard characters (*) are supported to specify ranges of ignored projects. The input can either be a file name containing a list of project names in json format or a plain string that is then used as a single enty. |
| `-include-ignored, --include-ignored-packages` | This flag allows to explicitly include ignored packages in the output. |
| `-mapping, --licenseurl-to-license-mappings` | When used, this option allows to add to 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. |
Expand Down
26 changes: 24 additions & 2 deletions src/NuGetUtility/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using NuGet.Configuration;
using NuGet.Protocol.Core.Types;
using NuGetUtility.Extension;
using NuGetUtility.Extensions;
using NuGetUtility.LicenseValidator;
using NuGetUtility.Output;
using NuGetUtility.Output.Json;
Expand Down Expand Up @@ -84,9 +85,14 @@ public class Program

[Option(LongName = "include-ignored-packages",
ShortName = "include-ignored",
Description = "If this option is set, the packages matching the ignore regexes are also printed to the output by specifying that they were explicitly ignored.")]
Description = "If this option is set, the packages that are ignored from validation are still included in the output.")]
public bool IncludeIgnoredPackages { get; } = false;

[Option(LongName = "exclude-projects-matching",
ShortName = "exclude-projects",
Description = "This option allows to specify project name(s) to exclude from the analysis. This can be useful to exclude test projects from the analysis when supplying a solution file as input. Wildcard characters (*) are supported to specify ranges of ignored projects. The input can either be a file name containing a list of project names in json format or a plain string that is then used as a single entry.")]
public string? ExcludedProjects { get; } = null;

private static string GetVersion()
=> typeof(Program).Assembly.GetCustomAttribute<AssemblyInformationalVersionAttribute>()?.InformationalVersion ?? string.Empty;

Expand All @@ -110,7 +116,8 @@ private async Task<int> OnExecuteAsync(CancellationToken cancellationToken)
ignoredPackages);
var projectReaderExceptions = new List<Exception>();

IEnumerable<string> projects = inputFiles.SelectMany(projectCollector.GetProjects);
string[] excludedProjects = GetExcludedProjects();
IEnumerable<string> projects = inputFiles.SelectMany(projectCollector.GetProjects).Where(p => !Array.Exists(excludedProjects, ignored => p.Like(ignored)));
IEnumerable<ProjectWithReferencedPackages> packagesForProject = projects.Select(p =>
{
IEnumerable<PackageIdentity>? installedPackages = null;
Expand Down Expand Up @@ -245,6 +252,21 @@ private string[] GetIgnoredPackages()
return JsonSerializer.Deserialize<string[]>(File.ReadAllText(IgnoredPackages))!;
}

private string[] GetExcludedProjects()
{
if (ExcludedProjects == null)
{
return Array.Empty<string>();
}

if (File.Exists(ExcludedProjects))
{
return JsonSerializer.Deserialize<string[]>(File.ReadAllText(ExcludedProjects))!;
}

return new[] { ExcludedProjects };
}

private string[] GetInputFiles()
{
if (InputFile != null)
Expand Down

0 comments on commit 7a2bd93

Please sign in to comment.