-
Notifications
You must be signed in to change notification settings - Fork 9
/
enrich_classpath.clj
492 lines (459 loc) · 23 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
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
(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.jar :refer [jar-for!]]
[cider.enrich-classpath.jdk :as jdk]
[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?]]
[cider.enrich-classpath.version :as version]
[cider.enrich-classpath.xdg :as xdg]
[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
(-> xdg/cache-root
(File. "enrich-classpath-cache")
(str)))
(defn serialize
"Turns any contained coll into a vector, sorting it.
This ensures that stable values are persisted to the file caches."
[x]
{:pre [(map? x)]
:post [(vector? %)]}
(with-meta (->> 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)
{:enrich-classpath/version version/data-version}))
(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))
(with-meta (if (-> x meta version/outdated-data-version?)
{}
(into {}
(map (fn [[k v]]
[k (if (and v (empty? v))
[]
(some->> v
(map (fn [[kk vv]]
[kk (some->> vv
set)]))
(into {})))]))
x))
{:enrich-classpath/version version/data-version}))
(defn safe-read-string [x]
{:pre [(string? x)]
:post [(vector? %)]}
(if (string/blank? x)
(do
(assert (reduce (fn [_ c]
(if (-> c byte zero?)
(reduced false)
true))
true
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
{:print-meta true})))
(def default-cache-contents (ppr-str (with-meta []
{:enrich-classpath/version version/data-version})))
(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})]}
(let [repositories (or (not-empty repositories)
[["central" {:url "https://repo1.maven.org/maven2/" :snapshots false}]
["clojars" {:url "https://repo.clojars.org/"}]])]
(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
(info (str ::failed-to-resolve " " x))
(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
;; only print stacktraces on unexpected errors.
;; Else they will be printed for every Clojars-only artifact not found on Central,
;; or of any source/javadoc artifact that simply doesn't exist.
(-> e .printStackTrace)
nil)))
(finally
(debug (str ::resolved " " (pr-str x))))))]
(when v
(swap! cache-atom assoc x v))
v))
(defn into-distinct [prio non-prio]
(into prio
(remove (set prio))
non-prio))
(defn derivatives [classifiers managed-dependencies memoized-resolve! [dep version & args :as original]]
(let [version (or version
;; note that managed-dependencies's version only takes precedence
;; if version was nil. Those are Leiningen's precedence rules which we also honor.
(->> managed-dependencies
(filter (fn [[a]]
(= dep a)))
first
second))]
(if-not version ;; handles managed-dependencies
[original]
(let [original-with-version (assoc original 1 version)
resolution (memoized-resolve! [original-with-version])
original-file (some-> resolution (find original-with-version) key meta :file)
transitive (flatten-deps resolution)
f (fn [[dep version :as original]]
(assert version (pr-str original))
(->> classifiers
(mapv (partial vector dep version :classifier))
(into-distinct [original]) ;; favor the one with meta
(remove (comp nil? second))
;; If a *transitive* dependency is explicitly specified by :managed-dependencies,
;; the managed version should take precedence (because that's their whole point):
(remove (fn [[d v]]
(->> managed-dependencies
(some (fn [[dd vv]]
(and (= d dd)
v
vv
(not= v vv)))))))
(keep (fn [x]
;; Keep only classified artifacts that exist...
(let [v (->> (memoized-resolve! [x])
flatten-deps
(filter #{(maybe-add-exclusions x)})
first)]
(when (some? v)
;; ...and attach their meta (currently unused),
;; especially for `:file`:
(with-meta x
(let [{:keys [file]} (meta v)]
(merge {:enrich-classpath/original-file original-file}
(when file
{:file (str file)}))))))))))]
(into []
(comp (mapcat f)
(distinct))
transitive)))))
(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 additions->files [additions]
(let [xs (into []
(comp (map meta)
(map (juxt :enrich-classpath/original-file :file)))
additions)
#_#_originals (vec (into #{}
(keep first)
xs))
files (mapv second xs)]
;; I don't know why I originally coded it like this.
;; Original files would result in .class artifacts added again to the classpath.
;; Maybe my original thinking was that the shortened .jar would replace the entire classpath string?
#_(into originals files)
files))
(defn rinto [x y]
(into y x))
(defn add [{:keys [repositories
dependencies
managed-dependencies
java-source-paths
resource-paths
source-paths
test-paths]
{:keys [classifiers
shorten
only-present-results?] ;; if true, the project is not modified - instead the results are assoced to :enrich-classpath
plugin-repositories :repositories
:or {classifiers #{"javadoc" "sources"}
shorten false
only-present-results? false}} :enrich-classpath
:as project}]
(debug (str [::classifiers classifiers]))
(write-file! cache-filename (fn [s]
(or (not-empty s)
default-cache-contents)))
(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)))
additions))
add-tools? (and (jdk/jdk8?)
(tools-jar-path))
enriched-deps-from-dependencies (into []
(comp (remove (set dependencies))
(filter (fn [[_ _ classifier]]
classifier))
(distinct))
(add-dependencies dependencies))
tentative-dependencies-set (into dependencies enriched-deps-from-dependencies)
enriched-deps-from-managed-deps (into []
(comp (remove (set managed-dependencies))
(filter (fn [[_ _ classifier]]
classifier))
(remove (fn [[dep version]]
(->> tentative-dependencies-set
(some (fn [[d v]]
(and (= d dep)
v
(not= version v)))))))
(distinct))
(add-dependencies managed-dependencies))
java-source-paths-to-add (fn [rp]
(->> 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 only-present-results?
(-> project
(assoc-in [:enrich-classpath :results :tools] [(tools-jar-path)])
(assoc-in [:enrich-classpath :results :dependencies] [(jar-for! (additions->files enriched-deps-from-dependencies))])
(assoc-in [:enrich-classpath :results :managed-dependencies] [(jar-for! (additions->files enriched-deps-from-managed-deps))])
(assoc-in [:enrich-classpath :results :java-source-paths] (java-source-paths-to-add resource-paths))
(assoc-in [:enrich-classpath :results :jdk-sources] (jdk-sources/resources-to-add)))
(cond-> project
(not shorten) (update :dependencies rinto enriched-deps-from-dependencies)
(not shorten) (update :dependencies rinto enriched-deps-from-managed-deps)
add-tools? (update :jar-exclusions conj (-> (tools-jar-path)
Pattern/quote
re-pattern))
add-tools? (update :resource-paths rinto [(tools-jar-path)])
shorten (update :resource-paths
rinto
(into []
(remove nil?)
[(jar-for! (additions->files enriched-deps-from-dependencies))
(jar-for! (additions->files enriched-deps-from-managed-deps))]))
(seq java-source-paths) (update :resource-paths (fn [rp]
(let [corpus (java-source-paths-to-add rp)]
(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))