This repository has been archived by the owner on Dec 26, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
core.clj
88 lines (69 loc) · 2.11 KB
/
core.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
(ns gradle-licenses.core
"Parse JSON generated by gradle-license-plugin into Clojure native data structure"
(:gen-class)
(:require
;;[clojure.spec.test.alpha :refer [instrument]]
[cheshire.core :as json]
[clojure.spec.alpha :as s]
[clojure.string :as str]))
;; Specs
(s/def ::str string?)
(s/def ::nilable-str (s/nilable string?))
(s/def ::package ::nilable-str)
(s/def ::license ::nilable-str)
(s/def ::path string?)
(s/def ::options (s/map-of keyword? any?))
(s/def ::dependency (s/map-of string? any?))
(s/def ::license (s/cat
:license ::str
:license_url ::str))
(s/def ::license-vec (s/* ::license))
(s/def ::data
(s/cat :package ::package :license ::license))
;; Const
(def key-dependency "dependency")
(def key-licenses "licenses")
(def key-license "license")
;; Helpers
(s/fdef path->string
:args ::path
:ret ::str)
(defn path->string
"Return a string with the file contents"
[path]
(slurp path))
(s/fdef license-vec->license
:args ::license-vec
:ret ::nilable-str)
(defn license-vec->license
"Convert a vector of licenses into nilable license string"
[license-vec]
(let [license-names (map #(get % key-license) license-vec)
license-str (str/join ", " license-names)
result (not-empty license-str)]
result))
(s/fdef dependency->map
:args ::dependency
:ret ::data)
(defn dependency->map
"Convert dependency item from the parsed JSON into a map"
[dependency]
(let [package-str (get dependency key-dependency)
license-vec (get dependency key-licenses)
license-str (license-vec->license license-vec)]
{:package package-str :license license-str}))
;; Entrypoint
(s/fdef gradle-json->data
:args (s/cat
:path ::path
:options ::options)
:ret (s/* ::data))
(defn gradle-json->data
"Parse gradle-license-plugin JSON into vector of {:package PACKAGE :license LICENSE} maps"
#_:clj-kondo/ignore
[path options]
(let [content (path->string path)
dependencies (json/parse-string content)
result (map dependency->map dependencies)]
result))
;; (instrument `gradle-json->data)