Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add alternative regex qualifier #101

Merged
merged 15 commits into from
Apr 11, 2022
Merged
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
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ Usage: dotnet-project-licenses [options]
| `--outfile` | Output filename |
| `-f, --output-directory` | Set Output Directory/Folder |
| `--projects-filter` | Simple json file of a text array of projects to skip. Supports Ends with matching such as 'Tests.csproj, Tests.vbproj, Tests.fsproj' |
| `--packages-filter` | Simple json file of a text array of packages to skip. Or a regular expression defined between two forward slashes '`/regex/`'. |
| `--packages-filter` | Simple json file of a text array of packages to skip. Or a regular expression defined between two forward slashes '`/regex/`' or two hashes '`#regex#`'. |
| `-u, --unique` | (Default: false) Unique licenses list by Id/Version |
| `-p, --print` | (Default: true) Print licenses. |
| `--export-license-texts` | Exports the raw license texts |
Expand Down Expand Up @@ -105,6 +105,11 @@ dotnet-project-licenses -i projectFolder -o -j -f ~/Projects/github --outfile ~/
dotnet-project-licenses -i projectFolder --export-license-texts --packages-filter '/Microsoft.*/'
```

### Prints licenses used by a compiled solution excluding all System packages
```ps
dotnet-project-licenses -i projectSolution.sln --use-project-assets-json --packages-filter '#System\..*#'
```

### Use a proxy server when getting nuget package information via http requests

```ps
Expand Down
3 changes: 2 additions & 1 deletion src/Methods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,8 @@ private async Task<string> ResolvePackageVersionFromNugetServerAsync(string name

private async Task<IEnumerable<string>> GetVersionsFromNugetServerAsync(string packageName)
{
using var request = new HttpRequestMessage(HttpMethod.Get, $"{packageName}/index.json");
// Linux request fails with NotFound if URL has any uppercase letters, thus, converting it all to lowercase
using var request = new HttpRequestMessage(HttpMethod.Get, $"{packageName}/index.json".ToLowerInvariant());
try
{
using var response = await _httpClient.SendAsync(request);
Expand Down
2 changes: 1 addition & 1 deletion src/NugetUtility.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<RepositoryType>git</RepositoryType>
<PackageId>dotnet-project-licenses</PackageId>
<ToolCommandName>dotnet-project-licenses</ToolCommandName>
<Version>2.3.12</Version>
<Version>2.3.13</Version>
<Authors>Tom Chavakis</Authors>
<Company>-</Company>
<Title>.NET Core Tool to print a list of the licenses of a projects</Title>
Expand Down
8 changes: 4 additions & 4 deletions src/PackageOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ namespace NugetUtility
{
public class PackageOptions
{
private readonly Regex UserRegexRegex = new Regex("^\\/(.+)\\/$");
private readonly Regex UserRegexRegex = new Regex("^([/#])(.+)\\1$");

private ICollection<string> _allowedLicenseTypes = new Collection<string>();
private ICollection<LibraryInfo> _manualInformation = new Collection<LibraryInfo>();
Expand Down Expand Up @@ -49,7 +49,7 @@ public class PackageOptions
[Option("projects-filter", Default = null, HelpText = "Simple json file of a text array of projects to skip. Supports Ends with matching such as 'Tests.csproj'")]
public string ProjectsFilterOption { get; set; }

[Option("packages-filter", Default = null, HelpText = "Simple json file of a text array of packages to skip, or a regular expression defined between two forward slashes.")]
[Option("packages-filter", Default = null, HelpText = "Simple json file of a text array of packages to skip, or a regular expression defined between two forward slashes or two hashes.")]
public string PackagesFilterOption { get; set; }

[Option('u', "unique", Default = false, HelpText = "Unique licenses list by Id/Version")]
Expand Down Expand Up @@ -150,8 +150,8 @@ public ICollection<string> ProjectFilter
// Check if the input is a regular expression that is defined between two forward slashes '/';
if (UserRegexRegex.IsMatch(PackagesFilterOption))
{
var userRegexString = UserRegexRegex.Replace(PackagesFilterOption, "$1");
// Try parse regular expression between forward slashes
var userRegexString = UserRegexRegex.Replace(PackagesFilterOption, "$2");
// Try parse regular expression between forward slashes or hashes
try
{
var parsedExpression = new Regex(userRegexString, RegexOptions.IgnoreCase);
Expand Down
21 changes: 19 additions & 2 deletions tests/NugetUtility.Tests/PackageOptionsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,20 @@ public void ProxyOption_ProxySystemAuth_Should_Be_True()
options.ProxySystemAuth.Should().Be(true);
}

[Test]
[TestCase("/.*/")]
[TestCase(@"#System\..*#")]
public void PackagesFilterOption_RegexPackagesFilter_Should_Support_Hashes_And_Slashes(string option)
{
var options = new PackageOptions
{
PackagesFilterOption = option,
};

var regex = options.PackageRegex;
regex.Should().NotBeNull();
}

[Test]
public void PackagesFilterOption_IncorrectRegexPackagesFilter_Should_Throw_ArgumentException()
{
Expand All @@ -66,11 +80,14 @@ public void PackagesFilterOption_IncorrectRegexPackagesFilter_Should_Throw_Argum
}

[Test]
public void PackagesFilterOption_IncorrectPackagesFilterPath_Should_Throw_FileNotFoundException () {
[TestCase(@"../../../DoesNotExist.json")]
[TestCase("/.*validregexinvalidpath#")]
[TestCase("#invalidpath/")]
public void PackagesFilterOption_IncorrectPackagesFilterPath_Should_Throw_FileNotFoundException (string option) {

var options = new PackageOptions
{
PackagesFilterOption = @"../../../DoesNotExist.json",
PackagesFilterOption = option,
};

Assert.Throws(typeof(FileNotFoundException), () => { var regex = options.PackageFilter; });
Expand Down