Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/issue-15'
Browse files Browse the repository at this point in the history
  • Loading branch information
aaronpowell committed May 4, 2020
2 parents fefc4eb + 608d5ce commit 4417652
Show file tree
Hide file tree
Showing 3 changed files with 118 additions and 66 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog for `dotnet-delice`

## Unreleased

### Changed

- Detecting dotnet tool references and handling them (issue #15)

## [1.4.0] - 2020-01-10

### Changed
Expand Down
126 changes: 65 additions & 61 deletions src/DotNetDelice.Licensing/LicenseBuilder.fs
Original file line number Diff line number Diff line change
Expand Up @@ -34,80 +34,84 @@ let rec private findPackage paths identity logger =
| pkg -> Some(pkg, head)
| [] -> None


let getPackageLicense (projectSpec: PackageSpec) checkGitHub token checkLicenseContent (lib: LockFileLibrary) =
let identity = PackageIdentity(lib.Name, lib.Version)

let checkLicenseContents' name url =
if checkLicenseContent then checkLicenseContents name url
else None

let nugetPaths =
[| projectSpec.RestoreMetadata.PackagesPath |]
|> Seq.append projectSpec.RestoreMetadata.FallbackFolders
|> Seq.toList

match findPackage nugetPaths identity MemoryLogger.Instance with
| None ->
{ PackageName = lib.Name
PackageVersion = lib.Version
Type = lib.Type }
|> PackageNotFound
| Some(pId, path) ->
let licenseMetadata =
{ Type = None
Version = None
Url = pId.Nuspec.GetLicenseUrl()
PackageName = lib.Name
PackageVersion = lib.Version }
match pId.Nuspec.GetLicenseMetadata() with
| null ->
let url = pId.Nuspec.GetLicenseUrl()
match checkGitHub, knownLicenseCache.TryFind url with
| (_, Some cachedLicense) ->
let private buildLicenseFromPackage checkGitHub token checkLicenseContents' (identity: PackageIdentity) packagePath
(pId: LocalPackageInfo) path =
let licenseMetadata =
{ Type = None
Version = None
Url = pId.Nuspec.GetLicenseUrl()
PackageName = identity.Id
PackageVersion = identity.Version }
match pId.Nuspec.GetLicenseMetadata() with
| null ->
let url = pId.Nuspec.GetLicenseUrl()
match checkGitHub, knownLicenseCache.TryFind url with
| (_, Some cachedLicense) ->
{ licenseMetadata with
Type = Some cachedLicense.Expression
Version = None }
|> Licensed
| (true, None) ->
match checkLicenseViaGitHub token url with
| Some cachedLicense ->
{ licenseMetadata with
Type = Some cachedLicense.Expression
Version = None }
|> Licensed
| (true, None) ->
match checkLicenseViaGitHub token url with
| Some cachedLicense ->
{ licenseMetadata with
Type = Some cachedLicense.Expression
Version = None }
|> Licensed
| None ->
match checkLicenseContents' lib.Name url with
| Some cachedLicense ->
{ licenseMetadata with
Type = Some cachedLicense.Expression
Version = None }
|> Licensed
| None -> licenseMetadata |> LegacyLicensed
| (false, None) ->
match checkLicenseContents' lib.Name url with
| None ->
match checkLicenseContents' identity.Id url with
| Some cachedLicense ->
{ licenseMetadata with
Type = Some cachedLicense.Expression
Version = None }
|> Licensed
| None -> licenseMetadata |> LegacyLicensed
| license when license.Type = LicenseType.File ->
match Path.Combine(path, lib.Path, license.License)
|> File.ReadAllText
|> findMatchingLicense with
| Some licenseSpdx ->
| (false, None) ->
match checkLicenseContents' identity.Id url with
| Some cachedLicense ->
{ licenseMetadata with
Type = Some licenseSpdx
Version = Some license.Version }
|> Licensed
| None ->
{ licenseMetadata with
Type = Some license.License
Version = Some license.Version }
Type = Some cachedLicense.Expression
Version = None }
|> Licensed
| license ->
| None -> licenseMetadata |> LegacyLicensed
| license when license.Type = LicenseType.File ->
match Path.Combine(path, packagePath, license.License)
|> File.ReadAllText
|> findMatchingLicense with
| Some licenseSpdx ->
{ licenseMetadata with
Type = Some licenseSpdx
Version = Some license.Version }
|> Licensed
| None ->
{ licenseMetadata with
Type = Some license.License
Version = Some license.Version }
|> Licensed
| license ->
{ licenseMetadata with
Type = Some license.License
Version = Some license.Version }
|> Licensed

let getPackageLicense (projectSpec: PackageSpec) checkGitHub token checkLicenseContent packageName packageVersion
packageType =
let identity = PackageIdentity(packageName, packageVersion)

let checkLicenseContents' name url =
if checkLicenseContent then checkLicenseContents name url else None

let nugetPaths =
[| projectSpec.RestoreMetadata.PackagesPath |]
|> Seq.append projectSpec.RestoreMetadata.FallbackFolders
|> Seq.toList

match findPackage nugetPaths identity MemoryLogger.Instance with
| None ->
{ PackageName = identity.Id
PackageVersion = identity.Version
Type = packageType }
|> PackageNotFound
| Some(pId, path) ->
buildLicenseFromPackage checkGitHub token checkLicenseContents' identity
((sprintf "%s/%A" identity.Id identity.Version).ToLower()) pId path
52 changes: 47 additions & 5 deletions src/DotNetDelice/App.fs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,31 @@ let findProject path =
let getLicenses checkGitHub token checkLicenseContent (projectSpec: PackageSpec) =
let file = Path.Combine(projectSpec.RestoreMetadata.OutputPath, "project.assets.json")
let lockFile = LockFileUtilities.GetLockFile(file, NullLogger.Instance)
lockFile.Libraries |> Seq.map (getPackageLicense projectSpec checkGitHub token checkLicenseContent)
lockFile.Libraries
|> Seq.map
(fun lib ->
getPackageLicense projectSpec checkGitHub token checkLicenseContent lib.Name lib.Version lib.Type)

let getLicensesForTool checkGitHub token checkLicenseContent (projectSpec: PackageSpec) =
let package =
projectSpec.TargetFrameworks
|> Seq.map (fun fx ->
let dep = fx.Dependencies |> Seq.head
let pkg =
NuGet.Protocol.LocalFolderUtility.GetPackageV3
(projectSpec.RestoreMetadata.PackagesPath, dep.Name, dep.LibraryRange.VersionRange.MinVersion,
MemoryLogger.Instance)
pkg)
|> Seq.head

let depGroups = package.Nuspec.GetDependencyGroups()

depGroups
|> Seq.collect (fun dg -> dg.Packages)
|> Seq.map
(fun p ->
getPackageLicense projectSpec checkGitHub token checkLicenseContent p.Id p.VersionRange.MinVersion
"package")

[<HelpOption>]
type Cli() =
Expand Down Expand Up @@ -98,18 +122,36 @@ type Cli() =
"delice only supports SDK project files (.NET Core, NETStandard, etc.), not legacy MSBuild ones (common for .NET Framework)."
| Some dependencyGraph ->
let getLicenses' = getLicenses this.CheckGitHub this.GitHubToken this.CheckLicenseContent
if this.Json then
let projects =
dependencyGraph.Projects
|> Seq.filter (fun projectSpec -> projectSpec.RestoreMetadata.ProjectStyle <> ProjectStyle.Unknown)
|> Seq.filter (fun projectSpec ->
projectSpec.RestoreMetadata.ProjectStyle <> ProjectStyle.Unknown
&& projectSpec.RestoreMetadata.ProjectStyle <> ProjectStyle.DotnetCliTool)

if this.Json then
let toolLicenseResults =
dependencyGraph.Projects
|> Seq.filter (fun ps -> ps.RestoreMetadata.ProjectStyle = ProjectStyle.DotnetCliTool)
|> Seq.map (fun projectSpec ->
getLicensesForTool this.CheckGitHub this.GitHubToken this.CheckLicenseContent projectSpec
|> jsonBuilder projectSpec.Name)
projects
|> Seq.map (fun projectSpec -> getLicenses' projectSpec |> jsonBuilder projectSpec.Name)
|> Seq.append toolLicenseResults
|> jsonPrinter this.JsonOutput
else
dependencyGraph.Projects
|> Seq.filter (fun projectSpec -> projectSpec.RestoreMetadata.ProjectStyle <> ProjectStyle.Unknown)
projects
|> Seq.iter (fun projectSpec ->
getLicenses' projectSpec |> prettyPrint projectSpec.Name
printfn "")

dependencyGraph.Projects
|> Seq.filter (fun ps -> ps.RestoreMetadata.ProjectStyle = ProjectStyle.DotnetCliTool)
|> Seq.iter (fun projectSpec ->
getLicensesForTool this.CheckGitHub this.GitHubToken this.CheckLicenseContent projectSpec
|> prettyPrint projectSpec.Name
printfn "")

let unknownProjectStyles =
dependencyGraph.Projects
|> Seq.filter (fun projectSpec -> projectSpec.RestoreMetadata.ProjectStyle = ProjectStyle.Unknown)
Expand Down

0 comments on commit 4417652

Please sign in to comment.