From 7a2bd93fbe042298fd368aabb3d72d16761125f5 Mon Sep 17 00:00:00 2001 From: Simon Ensslen Date: Thu, 25 Jul 2024 14:06:25 +0200 Subject: [PATCH] add mechanism to exclude projects (#71) --- README.md | 1 + src/NuGetUtility/Program.cs | 26 ++++++++++++++++++++++++-- 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 7c32c024..65bc66f7 100644 --- a/README.md +++ b/README.md @@ -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. | diff --git a/src/NuGetUtility/Program.cs b/src/NuGetUtility/Program.cs index 9da89604..85abc631 100644 --- a/src/NuGetUtility/Program.cs +++ b/src/NuGetUtility/Program.cs @@ -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; @@ -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()?.InformationalVersion ?? string.Empty; @@ -110,7 +116,8 @@ private async Task OnExecuteAsync(CancellationToken cancellationToken) ignoredPackages); var projectReaderExceptions = new List(); - IEnumerable projects = inputFiles.SelectMany(projectCollector.GetProjects); + string[] excludedProjects = GetExcludedProjects(); + IEnumerable projects = inputFiles.SelectMany(projectCollector.GetProjects).Where(p => !Array.Exists(excludedProjects, ignored => p.Like(ignored))); IEnumerable packagesForProject = projects.Select(p => { IEnumerable? installedPackages = null; @@ -245,6 +252,21 @@ private string[] GetIgnoredPackages() return JsonSerializer.Deserialize(File.ReadAllText(IgnoredPackages))!; } + private string[] GetExcludedProjects() + { + if (ExcludedProjects == null) + { + return Array.Empty(); + } + + if (File.Exists(ExcludedProjects)) + { + return JsonSerializer.Deserialize(File.ReadAllText(ExcludedProjects))!; + } + + return new[] { ExcludedProjects }; + } + private string[] GetInputFiles() { if (InputFile != null)