-
Notifications
You must be signed in to change notification settings - Fork 9
/
enrich_classpath.clj
386 lines (358 loc) · 16.7 KB
/
enrich_classpath.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
(ns cider.enrich-classpath
(:refer-clojure :exclude [time])
(:require
[cemerick.pomegranate.aether]
[cider.enrich-classpath.collections :refer [add-exclusions-if-classified divide-by ensure-no-lists flatten-deps maybe-normalize safe-sort]]
[cider.enrich-classpath.jdk-sources :as jdk-sources]
[cider.enrich-classpath.locks :refer [read-file! write-file!]]
[cider.enrich-classpath.logging :refer [debug info warn]]
[cider.enrich-classpath.source-analysis :refer [bad-source?]]
[clojure.java.io :as io]
[clojure.string :as string]
[clojure.walk :as walk]
[fipp.clojure])
(:import
(java.io File)
(java.net InetAddress URI UnknownHostException)
(java.util.concurrent ExecutionException)
(java.util.regex Pattern)))
(def ^String cache-filename
(-> "user.home"
System/getProperty
(File. ".enrich-classpath-cache")
(str)))
(defn serialize
"Turns any contained coll into a vector, sorting it.
This ensures that stable values are peristed to the file caches."
[x]
{:pre [(map? x)]
:post [(vector? %)]}
(->> x
(mapv (fn outer [[k v]]
[(ensure-no-lists k)
(some->> v
(mapv (fn inner [[kk vv]]
[(ensure-no-lists kk) (some->> vv
(map ensure-no-lists)
safe-sort
vec)]))
(safe-sort)
vec)]))
safe-sort
vec))
(defn deserialize
"Undoes the work of `#'serialize`.
Note that only certain vectors must be turned back into hashmaps - others must remain as-is."
[x]
{:post [(map? %)]}
(assert (vector? x) (class x))
(->> x
(map (fn [[k v]]
[k (if (and v (empty? v))
[]
(some->> v
(map (fn [[kk vv]]
[kk (some->> vv
set)]))
(into {})))]))
(into {})))
(defn safe-read-string [x]
{:pre [(string? x)]
:post [(vector? %)]}
(if (string/blank? x)
(do
(assert (not-any? (comp zero? byte) x)
"No ASCII NUL characters should be persisted")
[])
(try
(read-string x)
(catch Exception e
(throw (ex-info "Couldn't read-string this string" {:x x}))))))
(defn ppr-str [x]
(with-out-str
(fipp.clojure/pprint x)))
(defn make-merge-fn [cache-atom]
{:pre [@cache-atom]}
(fn [^String prev-val]
{:pre [(string? prev-val)]
:post [(string? %)]}
(let [old-map (-> prev-val safe-read-string deserialize)
ks (keys old-map)
new-map (merge old-map @cache-atom)
serialized (serialize new-map)
reserialized (deserialize serialized)]
(doseq [k ks]
(assert (find reserialized k)
(str "Expected newly serialized value to not drop existing key: " (pr-str k)))
(when (seq (get old-map k))
(assert (seq (get reserialized k))
(str "Expected overriden key to not empty out a previous value:" (pr-str {:k k
:o (get old-map k)
:r (get reserialized k)})))))
(ppr-str serialized))))
(defn resolve-with-timeout! [coordinates repositories]
{:pre [(vector? coordinates)
(-> coordinates count #{1})]}
(try
(deref (future
(cemerick.pomegranate.aether/resolve-dependencies :coordinates coordinates
:repositories repositories))
;; timing out should be very rare, it's not here for a strong reason
27500
::timed-out)
(catch ExecutionException e
(-> e .getCause throw))))
(defn maybe-add-exclusions* [x]
(->> x
(walk/postwalk (fn [item]
(cond-> item
(and (vector? item)
(some #{:classifier} item))
add-exclusions-if-classified)))))
(def maybe-add-exclusions (memoize maybe-add-exclusions*))
(defn resolve! [cache-atom repositories classifiers x]
(let [v (or (get @cache-atom x)
(get @cache-atom (maybe-normalize x))
(try
(let [x (maybe-add-exclusions x)
_ (debug (str ::resolving " " (pr-str x)))
v (resolve-with-timeout! x repositories)
[x] x]
(if (= v ::timed-out)
(do
(info (str ::timed-out " " x))
[])
(let [matching-artifact? (and (find v x)
(-> x (get 3) classifiers))
good-artifact? (and matching-artifact?
(not (bad-source? x)))]
(when good-artifact?
(info (str ::found " " (pr-str x))))
(if (and matching-artifact? (not good-artifact?))
(do
(info (str ::omitting-empty-source " " (pr-str x)))
[])
;; ensure the cache gets set to something:
(doto v assert)))))
(catch AbstractMethodError e
;; Catches:
;; "Tried to use insecure HTTP repository without TLS:
;; This is almost certainly a mistake; for details see
;; https://github.com/technomancy/leiningen/blob/master/doc/FAQ.md"
;; (apparently it's a bit hard to add some kind of conditional for only catching *that* AbstractMethodError,
;; but AbstractMethodErrors are rare enough that we can simply assume they have a single possible cause)
[])
(catch Exception e
(if (#{(Class/forName "org.eclipse.aether.resolution.DependencyResolutionException")
(Class/forName "org.eclipse.aether.transfer.ArtifactNotFoundException")
(Class/forName "org.eclipse.aether.resolution.ArtifactResolutionException")}
(class e))
[]
(do
(-> e .printStackTrace)
nil)))
(finally
(debug (str ::resolved " " (pr-str x))))))]
(when v
(swap! cache-atom assoc x v))
v))
(defn derivatives [classifiers managed-dependencies memoized-resolve! [dep version & args :as original]]
(let [version (or version
(->> managed-dependencies
(filter (fn [[a]]
(= dep a)))
first
second))]
(if-not version ;; handles managed-dependencies
[original]
(let [transitive (->> (memoized-resolve! [(assoc-in original [1] version)])
flatten-deps)]
(->> transitive
(mapcat (fn [[dep version :as original]]
(assert version (pr-str original))
(->> classifiers
(mapv (partial vector dep version :classifier))
(into [original])
(distinct)
(remove (comp nil? second))
(filter (fn [x]
(->> (memoized-resolve! [x])
flatten-deps
(filter #{(maybe-add-exclusions x)})
first
some?))))))
(distinct)
(vec))))))
(defn matches-version? [deps [s-or-j-name s-or-j-version :as s-or-j]]
(let [[_ matching-version :as matching] (->> deps
(filter (fn [[matching-name]]
(= matching-name
s-or-j-name)))
first)]
(if matching
(= s-or-j-version matching-version)
true)))
(defn choose-one-artifact
"Prevents Lein `:pedantic` faults by picking one source."
[deps managed-dependencies equivalent-deps]
{:post [(if %
(contains? (set equivalent-deps) %)
true)]}
(let [pred (fn [xs [inner-dep inner-version classifier-keyword classifier]]
{:pre [inner-dep
inner-version
(#{:classifier} classifier-keyword)
(string? classifier)]}
(->> xs
(some (fn [[dep version]]
{:pre [dep]}
(and (= dep inner-dep)
(= version inner-version))))))]
(or (->> equivalent-deps
(filter (partial pred deps))
(first))
(->> equivalent-deps
(filter (partial pred managed-dependencies))
(first))
(->> equivalent-deps
(sort-by second)
(last)))))
(def parallelism-factor
"A reasonable factor for parallel Maven resolution, which tries to maximise efficiency
while keeping thread count low which seems detrimental for both us and the Maven servers."
(if (find-ns 'lein-monolith.task.each)
1
4))
(defn acceptable-repository? [[_ {:keys [url] :as x}]]
(and (->> x keys (not-any? #{:password}))
;; Some domains may be behind under a VPN we are disconnected from:
(try
(when-let [{:keys [host scheme]} (some-> url URI. bean)]
(if-not (#{"https"} scheme)
false
(do
(InetAddress/getByName host)
true)))
(catch UnknownHostException _
false))))
(def tools-jar-path
"tools.jar is useful for Orchard in JDK8."
(memoize (fn []
(let [tools-file (-> "java.home" System/getProperty (io/file ".." "lib" "tools.jar"))]
(when (.exists tools-file)
(-> tools-file .getCanonicalPath))))))
(defn add [{:keys [repositories
managed-dependencies
java-source-paths
resource-paths
source-paths
test-paths]
{:keys [classifiers]
plugin-repositories :repositories
:or {classifiers #{"javadoc" "sources"}}} :enrich-classpath
:as project}]
(debug (str [::classifiers classifiers]))
(write-file! cache-filename (fn [s]
(or (not-empty s) "[]")))
(let [classifiers (set classifiers)
repositories (or (not-empty plugin-repositories)
repositories)
repositories (into {}
(filter acceptable-repository?)
repositories)
initial-cache-value (-> cache-filename read-file! safe-read-string deserialize)
cache-atom (atom initial-cache-value)
add-dependencies (fn [deps]
(let [memoized-resolve! (memoize (partial resolve! cache-atom repositories classifiers))
additions (->> deps
(divide-by parallelism-factor)
(pmap (fn [work]
(->> work
(mapcat (partial derivatives
classifiers
managed-dependencies
memoized-resolve!))
;; ensure the work is done within the pmap thread:
(vec))))
(apply concat)
(distinct)
(filter (fn [[_ _ _ x]]
(classifiers x)))
(filter (partial matches-version? deps))
(group-by (fn [[dep version _ classifier]]
[dep classifier]))
(vals)
(map (partial choose-one-artifact deps managed-dependencies))
(mapv (fn [x]
(conj x :exclusions '[[*]]))))]
(when-not (= initial-cache-value @cache-atom)
(write-file! cache-filename
(make-merge-fn cache-atom)))
;; order can be sensitive
(into additions deps)))
jdk8? (re-find #"^1\.8\." (System/getProperty "java.version"))
add-tools? (and jdk8?
(tools-jar-path))]
(cond-> project
true (update :dependencies add-dependencies)
add-tools? (update :jar-exclusions conj (-> (tools-jar-path)
Pattern/quote
re-pattern))
add-tools? (update :resource-paths (fn [rp]
(into [(tools-jar-path)] rp)))
(seq java-source-paths) (update :resource-paths (fn [rp]
(let [corpus (->> java-source-paths
(filterv (fn [jsp]
(let [s #{jsp}]
(and (not-any? s rp)
(not-any? s source-paths)
(not-any? s test-paths))))))]
(if (seq corpus)
(into corpus rp)
rp))))
true (update :resource-paths into (jdk-sources/resources-to-add)))))
(defmacro time
{:style/indent 1}
[atom-obj expr]
`(let [start# (. System (nanoTime))
ret# ~expr]
(reset! ~atom-obj (format "Completed in %.2f minutes." (-> System
(. (nanoTime))
(- start#)
(double)
(/ 1000000.0)
(/ 60000.0))))
ret#))
(defn wrap-failsafe
"Wraps `f` in a 'failsafe' way, protecting it against exceptions and overly slow executions."
[f ^long timeout]
{:pre [(ifn? f)
(pos? timeout)
(integer? timeout)]}
(fn [project]
(try
(let [v (deref (future
(f project))
(* 1000 timeout)
::timed-out)]
(if-not (= v ::timed-out)
v
(do
(warn (str ::timed-out))
project)))
(catch ExecutionException e
(-> e .getCause .printStackTrace)
project))))
(defn middleware
[{{:keys [failsafe timeout]
:or {failsafe true
timeout 215}} :enrich-classpath
:as project}
& args]
(let [a (atom nil)
f (cond-> add
failsafe (wrap-failsafe timeout))
v (time a
(f project))]
(debug @a)
v))