Skip to content

Commit df31c43

Browse files
authored
Merge pull request #45 from pmonks/dev
Release 2.0.279
2 parents 0dc92fa + 93e90da commit df31c43

File tree

7 files changed

+103
-36
lines changed

7 files changed

+103
-36
lines changed

.github/workflows/docs.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ jobs:
3030
run: clojure -Srepro -J-Dclojure.main.report=stderr -T:build docs
3131

3232
- name: Deploy docs
33-
uses: peaceiris/actions-gh-pages@v3
33+
uses: peaceiris/actions-gh-pages@v4
3434
with:
3535
github_token: ${{ secrets.GITHUB_TOKEN }}
3636
keep_files: true # Required so that we don't clobber the NVD report (published separately)

.github/workflows/vulnerabilities.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ jobs:
3333
- name: Deploy NVD vulnerability report
3434
# if: ${{ ( success() || failure() ) && github.ref == 'refs/heads/release' }} # Only deploy report from release branch, and regardless of whether the job succeeded or failed
3535
if: ${{ ( success() || failure() ) }}
36-
uses: peaceiris/actions-gh-pages@v3
36+
uses: peaceiris/actions-gh-pages@v4
3737
with:
3838
github_token: ${{ secrets.GITHUB_TOKEN }}
3939
keep_files: true # Required so that we don't clobber the API docs (published separately)

src/lice_comb/impl/spdx.clj

+2-2
Original file line numberDiff line numberDiff line change
@@ -128,8 +128,8 @@
128128
(let [original-name (unidentified->name id)]
129129
(str "Unidentified ("
130130
(if (s/blank? original-name)
131-
"-original name not available-")
132-
original-name
131+
"-original name not available-"
132+
original-name)
133133
")"))))
134134

135135
(defn init!

src/lice_comb/impl/utils.clj

+2-2
Original file line numberDiff line numberDiff line change
@@ -105,14 +105,14 @@
105105
"Encodes the given string to Base62/UTF-8."
106106
[^String s]
107107
(when s
108-
(base62/encode (.getBytes s (java.nio.charset.StandardCharsets/UTF_8)))))
108+
(base62/encode (.getBytes s java.nio.charset.StandardCharsets/UTF_8))))
109109

110110
(defn base62-decode
111111
"Decodes the given Base62/UTF-8 string."
112112
[^String s]
113113
(when s
114114
(if (re-matches #"\p{Alnum}*" s)
115-
(java.lang.String. ^bytes (base62/decode s) (java.nio.charset.StandardCharsets/UTF_8))
115+
(java.lang.String. ^bytes (base62/decode s) java.nio.charset.StandardCharsets/UTF_8)
116116
(throw (ex-info (str "Invalid BASE62 value provided: " s) {}))))) ; Because clj-base62 has crappy error messages
117117

118118
(defn valid-http-uri?

src/lice_comb/maven.clj

+79-28
Original file line numberDiff line numberDiff line change
@@ -45,22 +45,73 @@
4545

4646
(def ^:private separator java.io.File/separator)
4747

48-
(def ^:private local-maven-repo-d
49-
(delay
50-
(try
51-
; The command:
52-
; mvn help:evaluate -Dexpression=settings.localRepository -q -DforceStdout
53-
; determines where the local repository is located.
54-
(let [sh-result (sh/sh "mvn" "help:evaluate" "-Dexpression=settings.localRepository" "-q" "-DforceStdout")]
55-
(if (zero? (:exit sh-result))
56-
(s/trim (:out sh-result))
57-
(str (System/getProperty "user.home") (str separator ".m2" separator "repository"))))
58-
(catch java.io.IOException _
59-
(str (System/getProperty "user.home") (str separator ".m2" separator "repository"))))))
60-
61-
; TODO: make this configurable
62-
(def ^:private remote-maven-repos {"central" "https://repo1.maven.org/maven2"
63-
"clojars" "https://repo.clojars.org"})
48+
(def default-local-maven-repo
49+
"A String containing a file path for the default local Maven artifact
50+
cache that the library uses. Attempts to use this Maven client command to
51+
determine this value:
52+
53+
mvn help:evaluate -Dexpression=settings.localRepository -q -DforceStdout
54+
55+
but falls back on a \"best guess\" if the Maven client isn't installed or
56+
cannot be executed."
57+
(try
58+
; The command:
59+
; mvn help:evaluate -Dexpression=settings.localRepository -q -DforceStdout
60+
; determines where the local repository is located.
61+
(let [sh-result (sh/sh "mvn" "help:evaluate" "-Dexpression=settings.localRepository" "-q" "-DforceStdout")]
62+
(if (zero? (:exit sh-result))
63+
(s/trim (:out sh-result))
64+
(str (System/getProperty "user.home") (str separator ".m2" separator "repository"))))
65+
(catch java.io.IOException _
66+
(str (System/getProperty "user.home") (str separator ".m2" separator "repository")))))
67+
68+
(def ^:private local-maven-repo-a (atom default-local-maven-repo))
69+
70+
(defn local-maven-repo
71+
"The current local Maven repo in use, as a String containing a file path."
72+
[]
73+
@local-maven-repo-a)
74+
75+
(defn set-local-maven-repo!
76+
"Sets the local Maven repo to use from this point onward. The argument is a
77+
String containing a file path that must be a readable directory that exists
78+
(throws ex-info if these conditions are not met)."
79+
[dir]
80+
(let [d (io/file dir)]
81+
(if (and (.exists d)
82+
(.isDirectory d)
83+
(.canRead d))
84+
(swap! local-maven-repo-a (constantly dir))
85+
(throw (ex-info (str dir " either does not exist, is not a directory, or is not readable.") {}))))
86+
nil)
87+
88+
(def default-remote-maven-repos
89+
"A map containing the default remote Maven artifact repositories that the
90+
library uses. Each key is a string that's the short identifier of the repo
91+
(e.g. \"clojars\"), and each value is the base URL of that artifact repository
92+
(e.g. \"https://repo.clojars.org\")."
93+
{"central" "https://repo1.maven.org/maven2"
94+
"clojars" "https://repo.clojars.org"})
95+
96+
(def ^:private remote-maven-repos-a (atom default-remote-maven-repos))
97+
98+
(defn remote-maven-repos
99+
"The current remote Maven repos in use, as a map in the format described in
100+
`default-remote-maven-repos`."
101+
[]
102+
@remote-maven-repos-a)
103+
104+
(defn set-remote-maven-repos!
105+
"Sets the remote Maven repos to use from this point onward. The argument is a
106+
map in the format described in `default-remote-maven-repos`.
107+
108+
For most use cases you should merge `default-remote-maven-repos` with whatever
109+
additional repos you wish to provide (the rare exceptions being situations
110+
such as a dev environment that contains a custom Maven artifact repository
111+
that proxies/caches Maven Central and/or Clojars)."
112+
[repos]
113+
(swap! remote-maven-repos-a (constantly repos))
114+
nil)
64115

65116
(xml/alias-uri 'pom "http://maven.apache.org/POM/4.0.0")
66117

@@ -103,37 +154,37 @@
103154
"Returns a java.net.URI pointing to the maven-metadata.xml for the given GA,
104155
or nil if one cannot be found. The returned URI is guaranteed to be
105156
resolvable - either to a file that exists in the local Maven cache, or to an
106-
HTTP-accessible resource on a remote Maven repository (i.e. Maven Central or
157+
HTTP-accessible resource on a remote Maven repository (e.g. Maven Central,
107158
Clojars) that resolves."
108159
([{:keys [group-id artifact-id]}] (ga->metadata-uri group-id artifact-id))
109160
([group-id artifact-id]
110161
(when (and (not (s/blank? group-id))
111162
(not (s/blank? artifact-id)))
112163
(let [ga-path (str (s/replace group-id "." "/") "/" artifact-id)
113-
local-metadata-paths (map #(str ga-path "/maven-metadata-" % ".xml") (keys remote-maven-repos))]
164+
local-metadata-paths (map #(str ga-path "/maven-metadata-" % ".xml") (keys @remote-maven-repos-a))]
114165
(if-let [local-metadata-file (first (filter #(and (.exists ^java.io.File %) (.isFile ^java.io.File %))
115-
(map #(io/file (str @local-maven-repo-d "/" %)) (map #(s/replace % "/" separator) local-metadata-paths))))]
166+
(map #(io/file (str @local-maven-repo-a "/" %)) (map #(s/replace % "/" separator) local-metadata-paths))))]
116167
(.toURI ^java.io.File local-metadata-file)
117-
(when-let [remote-uri (first (filter lcihttp/uri-resolves? (map #(str % "/" ga-path "/maven-metadata.xml") (vals remote-maven-repos))))]
168+
(when-let [remote-uri (first (filter lcihttp/uri-resolves? (map #(str % "/" ga-path "/maven-metadata.xml") (vals @remote-maven-repos-a))))]
118169
(java.net.URI. remote-uri)))))))
119170

120171
(defn gav->metadata-uri
121172
"Returns a java.net.URI pointing to the maven-metadata.xml for the given GAV,
122173
or nil if one cannot be found. The returned URI is guaranteed to be
123174
resolvable - either to a file that exists in the local Maven cache, or to an
124-
HTTP-accessible resource on a remote Maven repository (i.e. Maven Central or
175+
HTTP-accessible resource on a remote Maven repository (e.g. Maven Central,
125176
Clojars) that resolves."
126177
([{:keys [group-id artifact-id version]}] (gav->metadata-uri group-id artifact-id version))
127178
([group-id artifact-id version]
128179
(when (and (not (s/blank? group-id))
129180
(not (s/blank? artifact-id))
130181
(not (s/blank? version)))
131182
(let [gav-path (str (s/replace group-id "." "/") "/" artifact-id "/" version)
132-
local-metadata-paths (map #(str gav-path "/maven-metadata-" % ".xml") (keys remote-maven-repos))]
183+
local-metadata-paths (map #(str gav-path "/maven-metadata-" % ".xml") (keys @remote-maven-repos-a))]
133184
(if-let [local-metadata-file (first (filter #(and (.exists ^java.io.File %) (.isFile ^java.io.File %))
134-
(map #(io/file (str @local-maven-repo-d "/" %)) (map #(s/replace % "/" separator) local-metadata-paths))))]
185+
(map #(io/file (str @local-maven-repo-a "/" %)) (map #(s/replace % "/" separator) local-metadata-paths))))]
135186
(.toURI ^java.io.File local-metadata-file)
136-
(when-let [remote-uri (first (filter lcihttp/uri-resolves? (map #(str % "/" gav-path "/maven-metadata.xml") (vals remote-maven-repos))))]
187+
(when-let [remote-uri (first (filter lcihttp/uri-resolves? (map #(str % "/" gav-path "/maven-metadata.xml") (vals @remote-maven-repos-a))))]
137188
(java.net.URI. remote-uri)))))))
138189

139190
(defn ga-latest-version
@@ -187,7 +238,7 @@
187238
"Returns a java.net.URI pointing to the POM for the given GAV, or nil if one
188239
cannot be found. The returned URI is guaranteed to be resolvable - either to
189240
a file that exists in the local Maven cache, or to an HTTP-accessible resource
190-
on a remote Maven repository (i.e. Maven Central or Clojars) that resolves.
241+
on a remote Maven repository (e.g. Maven Central, Clojars) that resolves.
191242
192243
If version is not provided, determines the latest version (which may be a
193244
SNAPSHOT) and uses that."
@@ -202,11 +253,11 @@
202253
version (if (release-version? version) (ga-release-version group-id artifact-id) version)
203254
file-version (resolve-snapshot-version group-id artifact-id version)
204255
gav-path (str (s/replace group-id "." "/") "/" artifact-id "/" version "/" artifact-id "-" file-version ".pom")
205-
local-pom (io/file (str @local-maven-repo-d separator (s/replace gav-path "/" separator)))]
256+
local-pom (io/file (str @local-maven-repo-a separator (s/replace gav-path "/" separator)))]
206257
(if (and (.exists local-pom)
207258
(.isFile local-pom))
208259
(.toURI local-pom)
209-
(when-let [remote-uri (first (filter lcihttp/uri-resolves? (map #(str % "/" gav-path) (vals remote-maven-repos))))]
260+
(when-let [remote-uri (first (filter lcihttp/uri-resolves? (map #(str % "/" gav-path) (vals @remote-maven-repos-a))))]
210261
(java.net.URI. remote-uri)))))))
211262

212263
(defmulti pom->expressions-info
@@ -309,5 +360,5 @@
309360
allow explicit control of the cost of initialisation to callers who need it."
310361
[]
311362
(lcmtch/init!)
312-
@local-maven-repo-d
363+
@local-maven-repo-a
313364
nil)

src/lice_comb/utils.clj

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
:spdx-matching-guidelines "SPDX matching guidelines"
3232
:spdx-listed-name "SPDX listed name (case insensitive match)"
3333
:spdx-listed-uri "SPDX listed URI (relaxed matching)"
34-
:expression-inference "inferred SPDX expression"
34+
:expression-inference "inferred license expression"
3535
:regex-matching "regular expression matching"
3636
:unidentified "fallback to unidentified LicenseRef"
3737
:manual-verification "manual verification"})

test/lice_comb/maven_test.clj

+17-1
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,10 @@
1818

1919
(ns lice-comb.maven-test
2020
(:require [clojure.test :refer [deftest testing is use-fixtures]]
21+
[clojure.java.io :as io]
2122
[lice-comb.test-boilerplate :refer [fixture valid=]]
2223
[lice-comb.impl.spdx :as lcis]
23-
[lice-comb.maven :refer [init! pom->expressions gav->expressions]]))
24+
[lice-comb.maven :refer [init! pom->expressions gav->expressions default-local-maven-repo default-remote-maven-repos set-remote-maven-repos!]]))
2425

2526
(use-fixtures :once fixture)
2627

@@ -84,3 +85,18 @@
8485
(is (valid= #{"EPL-1.0"} (gav->expressions "org.clojure" "clojure" "RELEASE"))) ; Maven Central, RELEASE version
8586
(is (valid= #{"EPL-1.0"} (gav->expressions "org.clojure" "clojure" "1.12.0-alpha5"))) ; Maven Central, custom suffix
8687
(is (valid= #{"Apache-2.0"} (gav->expressions "org.springframework" "spring-core" "6.1.0"))))) ; Maven Central
88+
89+
(deftest custom-remote-repos-tests
90+
; Erase jboss-common-core stuff from the local Maven cache, if it's present
91+
(let [jboss-maven-cache-dir (io/file (str default-local-maven-repo java.io.File/separator "org" java.io.File/separator "jboss" java.io.File/separator "jboss-common-core"))]
92+
(when (.exists jboss-maven-cache-dir)
93+
(run! #(.delete %) (reverse (file-seq jboss-maven-cache-dir)))))
94+
95+
(testing "Default repos"
96+
(is (nil? (gav->expressions "org.jboss" "jboss-common-core" "2.5.0.Final-redhat-1")))) ; This artifact is only found in the JBoss artifact repository
97+
(testing "Add a non-default repo"
98+
(is (nil? (set-remote-maven-repos! (merge default-remote-maven-repos {"jboss" "https://maven.repository.redhat.com/ga"}))))
99+
(is (valid= #{"Apache-2.0"} (gav->expressions "org.jboss" "jboss-common-core" "2.5.0.Final-redhat-1"))))
100+
(testing "Remove the non-default repo"
101+
(is (nil? (set-remote-maven-repos! default-remote-maven-repos)))
102+
(is (nil? (gav->expressions "org.jboss" "jboss-common-core" "2.5.0.Final-redhat-1")))))

0 commit comments

Comments
 (0)