Skip to content

Commit

Permalink
read local license from nuspec
Browse files Browse the repository at this point in the history
  • Loading branch information
danielklecha committed Mar 1, 2024
1 parent 667a776 commit 6c367f8
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 17 deletions.
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 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,21 @@ private static async Task<string> ResolveLicense(NuSpec nuSpec)
}

// Otherwise try to get the license from project url
if (string.IsNullOrEmpty(projectUrl)) return null;

if (LicenseCache.ContainsKey(projectUrl))
return LicenseCache[projectUrl];
if (string.IsNullOrEmpty(projectUrl))
{
if (LicenseCache.TryGetValue(projectUrl, out string value))
return value;

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

LicenseCache[nuSpec.Id] = license2;
LicenseCache[nuSpec.ProjectUrl] = license2;
return license2;
return null;
}

private static async Task<string> ResolveLicense(FileVersionInfo fileVersionInfo)
Expand Down Expand Up @@ -160,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
4 changes: 3 additions & 1 deletion src/DotnetThirdPartyNotices/NuSpec.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public record NuSpec
public string LicenseUrl { get; init; }
public string ProjectUrl { get; init; }
public string RepositoryUrl { get; init; }
public string LicenseRelativePath { get; init; }

private static NuSpec FromTextReader(TextReader streamReader)
{
Expand All @@ -31,7 +32,8 @@ private static NuSpec FromTextReader(TextReader streamReader)
Version = metadata.Element(ns + "version")?.Value,
LicenseUrl = metadata.Element(ns + "licenseUrl")?.Value,
ProjectUrl = metadata.Element(ns + "projectUrl")?.Value,
RepositoryUrl = metadata.Element(ns + "repository")?.Attribute("url")?.Value
RepositoryUrl = metadata.Element(ns + "repository")?.Attribute("url")?.Value,
LicenseRelativePath = metadata.Elements(ns + "license").Where(x => x.Attribute("type")?.Value == "file").FirstOrDefault()?.Value
};
}

Expand Down

0 comments on commit 6c367f8

Please sign in to comment.