Skip to content

Commit

Permalink
Merge pull request #12 from danielklecha/master
Browse files Browse the repository at this point in the history
upgrade to .NET 8, update dependencies, update search for nupkg and nuspec
  • Loading branch information
bugproof authored Mar 3, 2024
2 parents 71abe73 + 17ccb46 commit 5123297
Show file tree
Hide file tree
Showing 10 changed files with 179 additions and 196 deletions.
10 changes: 6 additions & 4 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,16 @@ jobs:
name: Build, pack and publish
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/setup-dotnet@v1
- name: Checkout code
uses: actions/checkout@v3
- name: Setup .NET
uses: actions/setup-dotnet@v3
with:
dotnet-version: '6.0.x'
dotnet-version: '8.0.x'

- name: Publish on version change
id: publish_nuget
uses: alirezanet/publish-nuget@v3.0.4
uses: alirezanet/publish-nuget@v3.1.0
with:
# Filepath of the project to be packaged, relative to root of repository
PROJECT_FILE_PATH: src/DotnetThirdPartyNotices/DotnetThirdPartyNotices.csproj
Expand Down
30 changes: 30 additions & 0 deletions src/DotnetThirdPartyNotices/Directory.Packages.props
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<Project>
<PropertyGroup>
<ManagePackageVersionsCentrally>true</ManagePackageVersionsCentrally>
<CentralPackageTransitivePinningEnabled>true</CentralPackageTransitivePinningEnabled>
</PropertyGroup>
<ItemGroup>
<PackageVersion Include="Microsoft.Build" Version="17.9.5" />
<PackageVersion Include="Microsoft.Build.Framework" Version="17.9.5" />
<PackageVersion Include="Microsoft.Build.Locator" Version="1.7.1" />
<PackageVersion Include="Microsoft.Build.Tasks.Core" Version="17.9.5" />
<PackageVersion Include="Microsoft.Build.Utilities.Core" Version="17.9.5" />
<PackageVersion Include="Serilog" Version="3.1.1" />
<PackageVersion Include="Serilog.Sinks.Console" Version="5.0.1" />
<PackageVersion Include="System.CommandLine" Version="2.0.0-beta4.22272.1" />
<PackageVersion Include="Microsoft.NET.StringTools" Version="17.9.5" />
<PackageVersion Include="System.CodeDom" Version="8.0.0" />
<PackageVersion Include="System.Collections.Immutable" Version="8.0.0" />
<PackageVersion Include="System.Configuration.ConfigurationManager" Version="8.0.0" />
<PackageVersion Include="System.Diagnostics.EventLog" Version="8.0.0" />
<PackageVersion Include="System.Formats.Asn1" Version="8.0.0" />
<PackageVersion Include="System.Reflection.Metadata" Version="8.0.0" />
<PackageVersion Include="System.Reflection.MetadataLoadContext" Version="8.0.0" />
<PackageVersion Include="System.Resources.Extensions" Version="8.0.0" />
<PackageVersion Include="System.Security.Cryptography.Pkcs" Version="8.0.0" />
<PackageVersion Include="System.Security.Cryptography.ProtectedData" Version="8.0.0" />
<PackageVersion Include="System.Security.Cryptography.Xml" Version="8.0.0" />
<PackageVersion Include="System.Security.Principal.Windows" Version="5.0.0" />
<PackageVersion Include="System.Threading.Tasks.Dataflow" Version="8.0.0" />
</ItemGroup>
</Project>
22 changes: 11 additions & 11 deletions src/DotnetThirdPartyNotices/DotnetThirdPartyNotices.csproj
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
<LangVersion>default</LangVersion>
<PackAsTool>true</PackAsTool>
<ToolCommandName>dotnet-thirdpartynotices</ToolCommandName>
<PackageId>DotnetThirdPartyNotices</PackageId>
<Description>A .NET tool to generate file with third party legal notices</Description>
<PackageVersion>0.2.7</PackageVersion>
<PackageVersion>0.2.8</PackageVersion>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<RepositoryType>git</RepositoryType>
<RepositoryUrl>https://github.com/bugproof/DotnetThirdPartyNotices</RepositoryUrl>
Expand All @@ -22,14 +22,14 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Build" Version="17.2.0" ExcludeAssets="runtime" />
<PackageReference Include="Microsoft.Build.Locator" Version="1.4.1" />
<PackageReference Include="Microsoft.Build.Framework" Version="17.2.0" ExcludeAssets="runtime" />
<PackageReference Include="Microsoft.Build.Tasks.Core" Version="17.2.0" ExcludeAssets="runtime" />
<PackageReference Include="Microsoft.Build.Utilities.Core" Version="17.2.0" ExcludeAssets="runtime" />
<PackageReference Include="Serilog" Version="2.11.0" />
<PackageReference Include="Serilog.Sinks.Console" Version="4.0.1" />
<PackageReference Include="System.CommandLine" Version="2.0.0-beta4.22272.1" />
<PackageReference Include="Microsoft.Build" ExcludeAssets="runtime" />
<PackageReference Include="Microsoft.Build.Locator" />
<PackageReference Include="Microsoft.Build.Framework" ExcludeAssets="runtime" />
<PackageReference Include="Microsoft.Build.Tasks.Core" ExcludeAssets="runtime" />
<PackageReference Include="Microsoft.Build.Utilities.Core" ExcludeAssets="runtime" />
<PackageReference Include="Serilog" />
<PackageReference Include="Serilog.Sinks.Console" />
<PackageReference Include="System.CommandLine" />
</ItemGroup>

</Project>
17 changes: 2 additions & 15 deletions src/DotnetThirdPartyNotices/Extensions/ProjectExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,7 @@ private static IEnumerable<ResolvedFileInfo> ResolveFilesUsingResolveAssemblyRef

if (item.GetMetadataValue("ResolvedFrom") == "{HintPathFromItem}" && item.GetMetadataValue("HintPath").StartsWith("..\\packages"))
{
var packagePath = Utils.GetPackagePathFromAssemblyPath(assemblyPath);
if (packagePath == null)
throw new ApplicationException($"Cannot find package path from assembly path ({assemblyPath})");

var nuPkgFileName = Directory.GetFiles(packagePath, "*.nupkg", SearchOption.TopDirectoryOnly).Single();

var nuSpec = NuSpec.FromNupkg(nuPkgFileName);
var nuSpec = NuSpec.FromAssemble(assemblyPath) ?? throw new ApplicationException( $"Cannot find package path from assembly path ({assemblyPath})" );
resolvedFileInfo.NuSpec = nuSpec;
resolvedFileInfos.Add(resolvedFileInfo);
}
Expand Down Expand Up @@ -97,14 +91,7 @@ private static IEnumerable<ResolvedFileInfo> ResolveFilesUsingComputeFilesToPubl
// Skip if it's not a NuGet package
continue;
}

var packagePath = Utils.GetPackagePathFromAssemblyPath(assemblyPath);
if (packagePath == null)
throw new ApplicationException($"Cannot find package path from assembly path ({assemblyPath})");

// TODO: don't think this is reliable because I'm not sure if .nuspec will always be there, or if it will always be named tha way
var nuSpecFilePath = Path.Combine(packagePath, $"{packageName}.nuspec"); // Directory.GetFiles(packageFolder, "*.nuspec", SearchOption.TopDirectoryOnly).SingleOrDefault();
var nuSpec = NuSpec.FromFile(nuSpecFilePath);
var nuSpec = NuSpec.FromAssemble( assemblyPath ) ?? throw new ApplicationException( $"Cannot find package path from assembly path ({assemblyPath})" ); ;

var relativePath = item.GetMetadataValue("RelativePath");
var resolvedFileInfo = new ResolvedFileInfo
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
Expand Down Expand Up @@ -62,27 +63,41 @@ public static async Task<string> ResolveLicense(this ResolvedFileInfo resolvedFi
if (resolvedFileInfo == null) throw new ArgumentNullException(nameof(resolvedFileInfo));
string license = null;
if (resolvedFileInfo.NuSpec != null)
license = await ResolveLicense(resolvedFileInfo.NuSpec);
license = await ResolveLicenseFromNuspec(resolvedFileInfo);

return license ?? await ResolveLicenseFromFileVersionInfo(resolvedFileInfo.VersionInfo);
return license ?? await ResolveLicense(resolvedFileInfo.VersionInfo);
}

private static readonly Dictionary<string, string> LicenseCache = new();

private static async Task<string> ResolveLicense(NuSpec nuSpec)
private static async Task<string> ResolveLicenseFromNuspec( ResolvedFileInfo resolvedFileInfo )
{
var nuSpec = resolvedFileInfo.NuSpec;
if (LicenseCache.ContainsKey(nuSpec.Id))
return LicenseCache[nuSpec.Id];

var licenseUrl = nuSpec.LicenseUrl;
var repositoryUrl = nuSpec.RepositoryUrl;
var projectUrl = nuSpec.ProjectUrl;

if (!string.IsNullOrEmpty(nuSpec.LicenseRelativePath))
{
if (LicenseCache.TryGetValue(nuSpec.LicenseRelativePath, out string value))
return value;
var license3 = await ResolveLicenseFromRelativePath(resolvedFileInfo.VersionInfo, nuSpec.LicenseRelativePath);
if (license3 != null)
{
LicenseCache[nuSpec.Id] = license3;
LicenseCache[nuSpec.LicenseRelativePath] = license3;
return license3;
}
}

// Try to get the license from license url
if (!string.IsNullOrEmpty(licenseUrl))
if (!string.IsNullOrEmpty(nuSpec.LicenseUrl))
{
if (LicenseCache.ContainsKey(licenseUrl))
return LicenseCache[licenseUrl];
if (LicenseCache.TryGetValue(licenseUrl, out string value))
return value;

var license = await ResolveLicenseFromLicenseUri(new Uri(nuSpec.LicenseUrl));
if (license != null)
Expand All @@ -96,8 +111,8 @@ private static async Task<string> ResolveLicense(NuSpec nuSpec)
// Try to get the license from repository url
if (!string.IsNullOrEmpty(repositoryUrl))
{
if (LicenseCache.ContainsKey(repositoryUrl ))
return LicenseCache[repositoryUrl];
if (LicenseCache.TryGetValue(repositoryUrl, out string value))
return value;
var license = await ResolveLicenseFromRepositoryUri(new Uri(repositoryUrl));
if (license != null)
{
Expand All @@ -108,17 +123,32 @@ private static async Task<string> ResolveLicense(NuSpec nuSpec)
}

// Otherwise try to get the license from project url
if (string.IsNullOrEmpty(projectUrl)) return null;
if (string.IsNullOrEmpty(projectUrl))
{
if (LicenseCache.TryGetValue(projectUrl, out string value))
return value;

if (LicenseCache.ContainsKey(projectUrl))
return LicenseCache[projectUrl];
var license2 = await ResolveLicenseFromProjectUri(new Uri(projectUrl));
if (license2 != null)
{
LicenseCache[nuSpec.Id] = license2;
LicenseCache[nuSpec.ProjectUrl] = license2;
return license2;
}
}

var license2 = await ResolveLicenseFromProjectUri(new Uri(projectUrl));
if (license2 == null) return null;
return null;
}

LicenseCache[nuSpec.Id] = license2;
LicenseCache[nuSpec.ProjectUrl] = license2;
return license2;
private static async Task<string> ResolveLicense(FileVersionInfo fileVersionInfo)
{
if (LicenseCache.ContainsKey(fileVersionInfo.FileName))
return LicenseCache[fileVersionInfo.FileName];
var license = await ResolveLicenseFromFileVersionInfo(fileVersionInfo);
if(license == null)
return null;
LicenseCache[fileVersionInfo.FileName] = license;
return license;
}

private static async Task<string> ResolveLicenseFromLicenseUri(Uri licenseUri)
Expand Down Expand Up @@ -149,6 +179,15 @@ private static async Task<string> ResolveLicenseFromRepositoryUri(Uri repository
return await repositoryUri.GetPlainText();
}

private static async Task<string> ResolveLicenseFromRelativePath(FileVersionInfo fileVersionInfo, string relativePath)
{
var packagePath = Utils.GetPackagePath( fileVersionInfo.FileName );
var licenseFullPath = Path.Combine( packagePath, relativePath );
if (!licenseFullPath.EndsWith(".txt") && !licenseFullPath.EndsWith( ".md" ) || !File.Exists( licenseFullPath ))
return null;
return await File.ReadAllTextAsync( licenseFullPath );
}

private static async Task<string> ResolveLicenseFromProjectUri(Uri projectUri)
{
if (TryFindProjectUriLicenseResolver(projectUri, out var projectUriLicenseResolver))
Expand Down
5 changes: 4 additions & 1 deletion src/DotnetThirdPartyNotices/GithubService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,10 @@ public async Task<string> GetLicenseContentFromRepositoryPath(string repositoryP
repositoryPath = repositoryPath.TrimEnd('/');
if (repositoryPath.EndsWith(".git"))
repositoryPath = repositoryPath[..^4];
var json = await _httpClient.GetStringAsync($"repos{repositoryPath}/license");
var response = await _httpClient.GetAsync($"repos{repositoryPath}/license");
if (!response.IsSuccessStatusCode)
return null;
var json = await response.Content.ReadAsStringAsync();
var jsonDocument = JsonDocument.Parse(json);

var rootElement = jsonDocument.RootElement;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,23 @@ namespace DotnetThirdPartyNotices.LicenseResolvers;

internal class LocalPackageLicenseResolver : IFileVersionInfoLicenseResolver
{
public bool CanResolve( FileVersionInfo fileVersionInfo ) => true;
public bool CanResolve( FileVersionInfo fileVersionInfo ) => GetLicensePath(fileVersionInfo) != null;

public async Task<string> Resolve( FileVersionInfo fileVersionInfo )
public async Task<string> Resolve(FileVersionInfo fileVersionInfo)
{
var packageName = Path.GetFileNameWithoutExtension(fileVersionInfo.FileName);
var directoryParts = Path.GetDirectoryName(fileVersionInfo.FileName ).Split('\\', StringSplitOptions.RemoveEmptyEntries);
for ( var i = 0; i < directoryParts.Length; i++ )
var licensePath = GetLicensePath(fileVersionInfo);
if (licensePath == null)
return null;
return await File.ReadAllTextAsync( licensePath );
}

private string GetLicensePath( FileVersionInfo fileVersionInfo )
{
var directoryPath = Utils.GetPackagePath( fileVersionInfo.FileName ) ?? Path.GetDirectoryName( fileVersionInfo.FileName );
return Directory.EnumerateFiles( directoryPath, "license.*", new EnumerationOptions
{
var directoryPath = string.Join('\\', directoryParts.SkipLast(i));
var licensePath = Directory.EnumerateFiles(directoryPath, "license.txt", SearchOption.TopDirectoryOnly)
.FirstOrDefault();
if (licensePath != null)
return await File.ReadAllTextAsync(licensePath);
if (directoryPath.EndsWith($"\\{packageName}", StringComparison.OrdinalIgnoreCase))
break;
}
return null;
MatchCasing = MatchCasing.CaseInsensitive,
RecurseSubdirectories = false
} ).FirstOrDefault( x => x.EndsWith( "\\license.txt", StringComparison.OrdinalIgnoreCase ) || x.EndsWith( "\\license.md", StringComparison.OrdinalIgnoreCase ) );
}
}
Loading

0 comments on commit 5123297

Please sign in to comment.