Skip to content

Commit

Permalink
Merge pull request #57 from pmonks/dev
Browse files Browse the repository at this point in the history
Release 2.0.228
  • Loading branch information
pmonks authored Apr 13, 2024
2 parents e57f313 + a52019f commit d807b3f
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 18 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ A Clojure [tools.build](https://github.com/clojure/tools.build) task library for

It also provides the ability to check your (Apache-2.0 licensed) project against the [Apache Software Foundation's 3rd Party License Policy](https://www.apache.org/legal/resolved.html).

Note: `tools-licenses` assumes a "flat" project organisational structure, where each project is defined by a directory containing a `deps.edn` file, and all of it's sub-directories. This may or may not work well with monolithic development models (such as [Polylith](https://polylith.gitbook.io/polylith)), where all source code is managed out of a single, large, deeply-nested directory structure. That said, `tools-licenses` will do _something_, but whether that thing is what you're expecting and/or useful is quite another matter.
Note: `tools-licenses` assumes a "flat" project organisational structure, where each project is defined by a directory containing a `deps.edn` file, and all of its sub-directories. This may or may not work well with monolithic development models (such as [Polylith](https://polylith.gitbook.io/polylith)), where all source code is managed out of a single, large, deeply-nested directory structure. That said, `tools-licenses` will do _something_, but whether that thing is what you're expecting and/or useful is quite another matter.

## Disclaimer

Expand Down Expand Up @@ -84,6 +84,7 @@ Require the namespace in your tools.build script (typically called `build.clj`),
[opts]
(lic/licenses opts))

; And, optionally:
(defn check-asf-policy
"Checks this project's dependencies' licenses against the ASF's 3rd party
license policy (https://www.apache.org/legal/resolved.html).
Expand All @@ -93,7 +94,7 @@ Require the namespace in your tools.build script (typically called `build.clj`),
(lic/check-asf-policy opts))
```

Optionally, you may also wish to configure logging for your `build` alias, since this tool can emit logging output. For example (using log4j2):
You may also wish to configure a logging implementation for your `build` alias, since this tool can emit logging output (mostly from the Java libraries it uses). For example (using log4j2, though you may choose any logging implementation you like that supports [SLF4J](https://www.slf4j.org/)):

```edn
:build
Expand Down
44 changes: 28 additions & 16 deletions src/tools_licenses/tasks.clj
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,14 @@

(defn- human-readable-expression-internal
"Recursive portion of the implementation of human-readable-expression."
[level parse-result]
[license-ref-formatting-fn level parse-result]
(when parse-result
(cond
(sequential? parse-result)
(when (pos? (count parse-result))
(let [op-str (str " " (s/upper-case (name (first parse-result))) " ")]
(str (when (pos? level) "(")
(s/join op-str (map (partial human-readable-expression-internal (inc level)) (rest parse-result))) ; Note: naive (stack consuming) recursion
(s/join op-str (map (partial human-readable-expression-internal license-ref-formatting-fn (inc level)) (rest parse-result))) ; Note: naive (stack consuming) recursion
(when (pos? level) ")"))))
(map? parse-result)
(str
Expand All @@ -67,22 +67,34 @@
(let [reconstituted-license-ref (str (when (:document-ref parse-result) (str "DocumentRef-" (:document-ref parse-result) ":"))
"LicenseRef-" (:license-ref parse-result))]
(if (lcm/lice-comb-license-ref? reconstituted-license-ref)
(ansi/fg-bright :yellow (lcm/id->name reconstituted-license-ref))
(license-ref-formatting-fn (lcm/id->name reconstituted-license-ref))
reconstituted-license-ref)))))))

(defn- human-readable-expression
"Converts an SPDX license expression into a human readable version, which
means decoding lice-comb specific LicenseRefs within the expression to their
human readable name, and colouring them yellow."
[exp]
(when exp
(when-let [parse-tree (sexp/parse exp)]
(s/trim (human-readable-expression-internal 0 parse-tree)))))
([exp] (human-readable-expression (partial ansi/fg-bright :yellow) exp))
([license-ref-formatting-fn exp]
(when exp
(when-let [parse-tree (sexp/parse exp)]
(s/trim (human-readable-expression-internal license-ref-formatting-fn 0 parse-tree))))))

(defn- sort-license-expressions
"Sorts the given license expressions, which involves sorting valid SPDX
expressions normally, and putting LicenseRefs last."
[exps]
(when (seq exps)
(seq (sort-by #(s/lower-case (human-readable-expression identity %)) exps))))

(defn- dep-and-license-expressions
[dep-name license-expressions]
(let [sorted-license-expressions (seq (sort (if (map? license-expressions) (keys license-expressions) license-expressions)))]
(str dep-name " [" (if sorted-license-expressions (s/join ", " (map human-readable-expression sorted-license-expressions)) (ansi/fg-bright :red "No licenses found")) "]")))
(let [sorted-license-expressions (sort-license-expressions (if (map? license-expressions) (keys license-expressions) license-expressions))]
(str dep-name
" [" (if sorted-license-expressions
(s/join ", " (map human-readable-expression sorted-license-expressions))
(ansi/fg-bright :red "No licenses found"))
"]")))

(defn- dep-and-licenses->string
[[dep-ga dep-info :as dep]]
Expand Down Expand Up @@ -114,21 +126,21 @@
(defn- summary-output!
"Emit summary output to stdout."
[proj-expressions-info deps-lib-map-with-info]
(let [proj-expressions (sort (keys proj-expressions-info))
(let [proj-expressions (sort-license-expressions (keys proj-expressions-info))
freqs (frequencies (filter identity (mapcat #(keys (get % :lice-comb/license-info)) (vals deps-lib-map-with-info))))
deps-expressions (seq (sort (keys freqs)))
deps-expressions (sort-license-expressions (keys freqs))
no-license-count (count (filter empty? (map #(:lice-comb/license-info (val %)) deps-lib-map-with-info)))
single-license-count (count (filter #(= (count %) 1) (map #(:lice-comb/license-info (val %)) deps-lib-map-with-info)))
multi-license-count (count (filter #(> (count %) 1) (map #(:lice-comb/license-info (val %)) deps-lib-map-with-info)))]
(print (str "\n" (ansi/bold "This project: ")))
(if (seq proj-expressions)
(println (s/join ", " proj-expressions))
(println "- no license information found -"))
(println (s/join ", " (map human-readable-expression proj-expressions)))
(println (ansi/fg-bright :red "No licenses found")))
(println (ansi/bold "\nLicense Expression # of Deps"
"\n------------------------------------------------------------ ---------"))
(if (or deps-expressions (pos? no-license-count))
(do
(run! #(println (str (fit-width 60 (human-readable-expression %)) " " (fit-width 9 (ansi/default (str (get freqs %))) false))) deps-expressions)
(run! #(println (str (fit-width 60 (human-readable-expression %)) " " (fit-width 9 (ansi/default (str (get freqs %))) false))) deps-expressions)
(when (pos? no-license-count) (println (str (fit-width 60 (ansi/fg-bright :red "No licenses found")) " " (fit-width 9 no-license-count false)))))
(println " - no dependencies found -"))
(println (str (ansi/bold "------------------------------------------------------------ ---------")
Expand All @@ -142,7 +154,7 @@
(defn- detailed-output!
"Emit detailed output to stdout."
[opts proj-expressions-info deps-lib-map-with-info]
(let [expressions (sort (keys proj-expressions-info))
(let [expressions (sort-license-expressions (keys proj-expressions-info))
direct-deps (into {} (remove (fn [[_ v]] (seq (:dependents v))) deps-lib-map-with-info))
transitive-deps (into {} (filter (fn [[_ v]] (seq (:dependents v))) deps-lib-map-with-info))]
(println (str "\n" (ansi/bold "This project:")))
Expand Down Expand Up @@ -185,7 +197,7 @@

(defn- explain-with-licenses!
[dep-expr-info]
(let [exprs (sort (keys dep-expr-info))]
(let [exprs (sort-license-expressions (keys dep-expr-info))]
(println (ansi/bold "Licenses:") (s/join ", " (map human-readable-expression exprs)) "\n")
(println (s/join "\n\n" (map (partial expression-info->string dep-expr-info) exprs)) "\n")))

Expand Down

0 comments on commit d807b3f

Please sign in to comment.