/
alpha.clj
815 lines (710 loc) · 31.2 KB
/
alpha.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
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
; Copyright (c) Rich Hickey. All rights reserved.
; The use and distribution terms for this software are covered by the
; Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php)
; which can be found in the file epl-v10.html at the root of this distribution.
; By using this software in any fashion, you are agreeing to be bound by
; the terms of this license.
; You must not remove this notice, or any other, from this software.
(ns clojure.tools.deps.alpha
(:require
[clojure.java.io :as jio]
[clojure.pprint :refer [pprint]]
[clojure.set :as set]
[clojure.string :as str]
[clojure.tools.deps.alpha.util.concurrent :as concurrent]
[clojure.tools.deps.alpha.util.dir :as dir]
[clojure.tools.deps.alpha.util.io :as io]
[clojure.tools.deps.alpha.util.session :as session]
[clojure.tools.deps.alpha.extensions :as ext]
[clojure.walk :as walk])
(:import
[clojure.lang EdnReader$ReaderException]
[clojure.lang PersistentQueue]
[java.io File InputStreamReader BufferedReader]))
;(set! *warn-on-reflection* true)
;;;; deps.edn reading
(defn- io-err
^Throwable [fmt ^File f]
(let [path (.getAbsolutePath f)]
(ex-info (format fmt path) {:path path})))
(defn- slurp-edn-map
"Read the file specified by the path-segments, slurp it, and read it as edn."
[^File f]
(let [val (try (io/slurp-edn f)
(catch EdnReader$ReaderException e (throw (io-err (str (.getMessage e) " (%s)") f)))
(catch RuntimeException t
(if (str/starts-with? (.getMessage t) "EOF while reading")
(throw (io-err "Error reading edn, delimiter unmatched (%s)" f))
(throw (io-err (str "Error reading edn. " (.getMessage t) " (%s)") f)))))]
(if (map? val)
val
(throw (io-err "Expected edn map in: %s" f)))))
;; all this canonicalization is deprecated and will eventually be removed
(defn- canonicalize-sym
([s]
(canonicalize-sym s nil))
([s file-name]
(if (simple-symbol? s)
(let [cs (as-> (name s) n (symbol n n))]
(io/printerrln "DEPRECATED: Libs must be qualified, change" s "=>" cs
(if file-name (str "(" file-name ")") ""))
cs)
s)))
(defn- canonicalize-exclusions
[{:keys [exclusions] :as coord} file-name]
(if (seq (filter simple-symbol? exclusions))
(assoc coord :exclusions (mapv #(canonicalize-sym % file-name) exclusions))
coord))
(defn- canonicalize-dep-map
[deps-map file-name]
(when deps-map
(reduce-kv (fn [acc lib coord]
(let [new-lib (if (simple-symbol? lib) (canonicalize-sym lib file-name) lib)
new-coord (canonicalize-exclusions coord file-name)]
(assoc acc new-lib new-coord)))
{} deps-map)))
(defn- canonicalize-all-syms
([deps-edn]
(canonicalize-all-syms deps-edn nil))
([deps-edn file-name]
(walk/postwalk
(fn [x]
(if (map? x)
(reduce (fn [xr k]
(if-let [xm (get xr k)]
(assoc xr k (canonicalize-dep-map xm file-name))
xr))
x #{:deps :default-deps :override-deps :extra-deps :classpath-overrides})
x))
deps-edn)))
(defn slurp-deps
"Read a single deps.edn file from disk and canonicalize symbols,
return a deps map."
[^File dep-file]
(-> dep-file slurp-edn-map (canonicalize-all-syms (.getPath dep-file))))
(defn root-deps
"Read the root deps.edn resource from the classpath at the path
clojure/tools/deps/deps.edn"
[]
(let [url (jio/resource "clojure/tools/deps/deps.edn")]
(io/read-edn (BufferedReader. (InputStreamReader. (.openStream url))))))
(defn user-deps-path
"Use the same logic as clj to calculate the location of the user deps.edn.
Note that it's possible no file may exist at this location."
[]
(let [config-env (System/getenv "CLJ_CONFIG")
xdg-env (System/getenv "XDG_CONFIG_HOME")
home (System/getProperty "user.home")
config-dir (cond config-env config-env
xdg-env (str xdg-env File/separator "clojure")
:else (str home File/separator ".clojure"))]
(str config-dir File/separator "deps.edn")))
(defn find-edn-maps
"Finds and returns standard deps edn maps in a map with keys
:root-edn, :user-edn, :project-edn
If no project-edn is supplied, use the deps.edn in current directory"
([]
(find-edn-maps nil))
([project-edn-file]
(let [user-loc (jio/file (user-deps-path))
project-loc (jio/file (if project-edn-file project-edn-file (str dir/*the-dir* File/separator "deps.edn")))]
(cond-> {:root-edn (root-deps)}
(.exists user-loc) (assoc :user-edn (slurp-deps user-loc))
(.exists project-loc) (assoc :project-edn (slurp-deps project-loc))))))
(defn- merge-or-replace
"If maps, merge, otherwise replace"
[& vals]
(when (some identity vals)
(reduce (fn [ret val]
(if (and (map? ret) (map? val))
(merge ret val)
(or val ret)))
nil vals)))
(defn merge-edns
"Merge multiple deps edn maps from left to right into a single deps edn map."
[deps-edn-maps]
(apply merge-with merge-or-replace (remove nil? deps-edn-maps)))
;;;; Aliases
;; per-key binary merge-with rules
(def ^:private last-wins (comp last #(remove nil? %) vector))
(def ^:private append (comp vec concat))
(def ^:private append-unique (comp vec distinct concat))
(def ^:private merge-alias-rules
{:deps merge ;; FUTURE: remove
:replace-deps merge ;; formerly :deps
:extra-deps merge
:override-deps merge
:default-deps merge
:classpath-overrides merge
:paths append-unique ;; FUTURE: remove
:replace-paths append-unique ;; formerly :paths
:extra-paths append-unique
:jvm-opts append
:main-opts last-wins
:exec-fn last-wins
:exec-args merge-or-replace
:ns-aliases merge
:ns-default last-wins})
(defn- choose-rule [alias-key val]
(or (merge-alias-rules alias-key)
(if (map? val)
merge
(fn [_v1 v2] v2))))
(defn- merge-alias-maps
"Like merge-with, but using custom per-alias-key merge function"
[& ms]
(reduce
#(reduce
(fn [m [k v]] (update m k (choose-rule k v) v))
%1 %2)
{} ms))
(defn combine-aliases
"Find, read, and combine alias maps identified by alias keywords from
a deps edn map into a single args map."
[edn-map alias-kws]
(->> alias-kws
(map #(get-in edn-map [:aliases %]))
(apply merge-alias-maps)))
(defn lib-location
"Find the file path location of where a lib/coord would be located if procured
without actually doing the procuring!"
[lib coord deps-config]
(ext/lib-location lib coord deps-config))
;;;; expand-deps
;; exclusions tree
(defn- excluded?
[exclusions path lib]
(let [lib-name (first (str/split (name lib) #"\$"))
base-lib (symbol (namespace lib) lib-name)]
(loop [search path]
(when (seq search)
(if (get-in exclusions [search base-lib])
true
(recur (pop search)))))))
(defn- update-excl
"Update exclusions and cut based on whether this is a new lib/version,
a new instance of an existing lib/version, or not including."
[lib use-coord coord-id use-path include reason exclusions cut]
(let [coord-excl (when-let [e (:exclusions use-coord)] (set e))]
(cond
;; if adding new lib/version, include all non-excluded children
include
(if (nil? coord-excl)
{:exclusions' exclusions, :cut' cut, :child-pred (constantly true)}
{:exclusions' (assoc exclusions use-path coord-excl)
:cut' (assoc cut [lib coord-id] coord-excl)
:child-pred (fn [lib] (not (contains? coord-excl lib)))})
;; if seeing same lib/ver again, narrow exclusions to intersection of prior and new.
;; only include new unexcluded children (old excl set minus new excl set)
;; as others were already enqueued when first added
(= reason :same-version)
(let [exclusions' (if (seq coord-excl) (assoc exclusions use-path coord-excl) exclusions)
cut-coord (get cut [lib coord-id]) ;; previously cut from this lib, so were not enqueued
new-cut (set/intersection coord-excl cut-coord)
enq-only (set/difference cut-coord new-cut)]
{:exclusions' exclusions'
:cut' (assoc cut [lib coord-id] new-cut)
:child-pred (set enq-only)})
:else ;; otherwise, no change
{:exclusions' exclusions, :cut' cut})))
;; version map
;; {lib {:versions {coord-id coord} ;; all version coords
;; :paths {coord-id #{paths}} ;; paths to coord-ids
;; :select coord-id ;; current selection
;; :top true} ;; if selection is top dep
(defn- add-version
"Add a new version of a lib to the version map"
[vmap lib coord path coord-id]
(-> (or vmap {})
(assoc-in [lib :versions coord-id] coord)
(update-in [lib :paths]
(fn [coord-paths]
(merge-with into {coord-id #{path}} coord-paths)))))
(defn- select-version
"Mark a particular coord as selected in version map"
[vmap lib coord-id top?]
(update-in vmap [lib] merge (cond-> {:select coord-id}
top? (assoc :top true))))
(defn- selected-version
"Get currently selected version of lib"
[vmap lib]
(get-in vmap [lib :select]))
(defn- selected-coord
"Get currently selected coord of lib"
[vmap lib]
(get-in vmap [lib :versions (selected-version vmap lib)]))
(defn- selected-paths
"Get paths to selected coord of lib"
[vmap lib]
(get-in vmap [lib :paths (selected-version vmap lib)]))
(defn- parent-missing?
"Is any part of the parent path missing from the selected lib/versions?
This can happen if a newer version was found, orphaning previously selected children."
[vmap parent-path]
(when (seq parent-path)
(loop [path parent-path]
(if (seq path)
(let [lib (last path)
check-path (vec (butlast path))
{:keys [paths select]} (get vmap lib)]
(if (contains? (get paths select) check-path)
(recur check-path)
true))
false))))
(defn- deselect-orphans
"For the given paths, deselect any libs whose only selected version paths are in omitted-paths"
[vmap omitted-paths]
(reduce-kv
(fn [ret lib {:keys [select paths]}]
(let [lib-paths (get paths select)]
;; if every selected path for this lib has omitted paths as prefixes, deselect
(if (every? (fn [p] (some #(= % (take (count %) p)) omitted-paths)) lib-paths)
(update-in ret [lib] dissoc :select)
ret)))
vmap
vmap))
(defn- dominates?
"Is new-coord newer than old-coord?"
[lib new-coord old-coord config]
(pos? (ext/compare-versions lib new-coord old-coord config)))
(defn- include-coord?
"This is the key decision-making function when considering a lib/coord node in
the traversal graph. It returns :include (whether to include this lib/coord), a
:reason why it was included or not, and an updated :vmap (may have new version added
and/or new selected version for a lib)"
[vmap lib coord coord-id path exclusions config]
(cond
;; lib is a top dep and this is it => select
(empty? path)
{:include true, :reason :new-top-dep,
:vmap (-> vmap
(add-version lib coord path coord-id)
(select-version lib coord-id true))}
;; lib is excluded in this path => omit
(excluded? exclusions path lib)
{:include false, :reason :excluded, :vmap vmap}
;; lib is a top dep and this isn't it => omit
(get-in vmap [lib :top])
{:include false, :reason :use-top, :vmap vmap}
;; lib's parent path is not included => omit
(parent-missing? vmap path)
{:include false, :reason :parent-omitted, :vmap vmap}
;; new lib or no selection => select
(not (selected-version vmap lib))
{:include true, :reason :new-dep,
:vmap (-> vmap
(add-version lib coord path coord-id)
(select-version lib coord-id false))}
;; existing lib, same version => omit (but update vmap, may need to enqueue newly unexcluded children)
(= coord-id (selected-version vmap lib))
{:include false, :reason :same-version, :vmap (add-version vmap lib coord path coord-id)}
;; existing lib, newer version => select
(dominates? lib coord (selected-coord vmap lib) config)
{:include true, :reason :newer-version,
:vmap (-> vmap
(add-version lib coord path coord-id)
(deselect-orphans (set (map #(conj % lib) (selected-paths vmap lib))))
(select-version lib coord-id false))}
;; existing lib, older version => omit
:else
{:include false, :reason :older-version, :vmap vmap}))
(defn- canonicalize-deps
[deps config]
(reduce
(fn [m [lib coord]] (conj m (ext/canonicalize lib coord config)))
[] deps))
(defn- trace+
"Add an entry to the trace if needed"
[trace? trace parents lib coord use-coord coord-id override-coord include reason]
(when trace?
(let [entry (cond-> {:path parents, :lib lib, :coord use-coord, :orig-coord coord, :coord-id coord-id
:include include, :reason reason}
override-coord (assoc :override-coord override-coord))]
(conj trace entry))))
(defn- next-path
[pendq q on-error]
(let [[fchild & rchildren] pendq]
(if fchild
{:path fchild, :pendq rchildren, :q' q}
(let [next-q (peek q)
q' (pop q)]
(if (map? next-q)
(let [{:keys [pend-children ppath child-pred]} next-q
result @pend-children]
(when (instance? Throwable result)
(on-error result))
(next-path (->> result (filter (fn [[lib _coord]] (child-pred lib))) (map #(conj ppath %))) q' on-error))
{:path next-q, :q' q'})))))
(defn- expand-deps
"Dep tree expansion, returns version map"
[deps default-deps override-deps config executor trace?]
(letfn [(err-handler [throwable]
(do
(concurrent/shutdown-on-error executor)
(throw ^Throwable throwable)))
(children-task [lib use-coord use-path child-pred]
{:pend-children
(let [{:deps/keys [manifest root]} use-coord]
(dir/with-dir (if root (jio/file root) dir/*the-dir*)
(concurrent/submit-task executor
#(try
(canonicalize-deps (ext/coord-deps lib use-coord manifest config) config)
(catch Throwable t t)))))
:ppath use-path
:child-pred child-pred})]
(loop [pendq nil ;; a resolved child-lookup thunk to look at first
q (into (PersistentQueue/EMPTY) (map vector deps)) ;; queue of nodes or child-lookups
version-map nil ;; track all seen versions of libs and which version is selected
exclusions nil ;; tracks exclusions marked in the tree
cut nil ;; tracks cuts made of child nodes based on exclusions
trace []] ;; trace expansion
(let [{:keys [path pendq q']} (next-path pendq q err-handler)]
(if path
(let [[lib coord] (peek path)
parents (pop path)
use-path (conj parents lib)
override-coord (get override-deps lib)
choose-coord (cond override-coord override-coord
coord coord
:else (get default-deps lib))
use-coord (merge choose-coord (ext/manifest-type lib choose-coord config))
coord-id (ext/dep-id lib use-coord config)
{:keys [include reason vmap]} (include-coord? version-map lib use-coord coord-id parents exclusions config)
;_ (println "loop" lib coord-id "include=" include "reason=" reason)
{:keys [exclusions' cut' child-pred]} (update-excl lib use-coord coord-id use-path include reason exclusions cut)
new-q (if child-pred (conj q' (children-task lib use-coord use-path child-pred)) q')]
(recur pendq new-q vmap exclusions' cut'
(trace+ trace? trace parents lib coord use-coord coord-id override-coord include reason)))
(cond-> version-map trace? (with-meta {:trace {:log trace, :vmap version-map, :exclusions exclusions}})))))))
(defn- cut-orphans
"Remove any selected lib that does not have a selected parent path"
[version-map]
(reduce-kv
(fn [vmap lib {:keys [select]}]
(if (nil? select)
(dissoc vmap lib)
vmap))
version-map version-map))
(defn- lib-paths
"Convert version map to lib map"
[version-map]
(reduce-kv
(fn [ret lib {:keys [select versions paths]}]
(let [coord (get versions select)
parent-paths (get paths select)
parents (->> parent-paths (map last) (remove nil?) vec)]
(assoc ret lib (cond-> coord
(seq parents) (assoc :dependents parents)
(seq parent-paths) (assoc :parents parent-paths)))))
{} version-map))
(defn- download-libs
[executor lib-map config]
(let [lib-futs (reduce-kv
(fn [fs lib coord]
(let [fut (concurrent/submit-task
executor
#(try
(ext/coord-paths lib coord (:deps/manifest coord) config)
(catch Throwable t t)))]
(assoc fs lib fut)))
{} lib-map)]
(reduce-kv (fn [lm lib fut]
(let [result @fut]
(if (instance? Throwable result)
(do
(concurrent/shutdown-on-error executor)
(throw ^Throwable result))
(assoc-in lm [lib :paths] result))))
lib-map lib-futs)))
(defn resolve-deps
"Takes a deps configuration map and resolves the transitive dependency graph
from the initial set of deps. args-map is a map with several keys (all
optional) that can modify the results of the transitive expansion:
:extra-deps - a map from lib to coord of deps to add to the main deps
:override-deps - a map from lib to coord of coord to use instead of those in the graph
:default-deps - a map from lib to coord of deps to use if no coord specified
:trace - boolean. If true, the returned lib map will have metadata with :trace log
:threads - long. If provided, sets the number of concurrent download threads
Returns a lib map (map of lib to coordinate chosen)."
{:arglists '([deps-map args-map])}
([deps-map args-map]
(let [{:keys [extra-deps default-deps override-deps threads trace]} args-map
n (or threads concurrent/processors)
executor (concurrent/new-executor n)
deps (merge (:deps deps-map) extra-deps)
version-map (-> deps
(canonicalize-deps deps-map)
(expand-deps default-deps override-deps deps-map executor trace)
cut-orphans)
lib-map (lib-paths version-map)
lib-map' (download-libs executor lib-map deps-map)]
(with-meta lib-map' (meta version-map))))
;; deprecated arity, retained for backwards compatibility
([deps-map args-map settings]
(resolve-deps deps-map (merge args-map settings))))
(defn- make-tree
[lib-map]
(let [{roots false, nonroots true} (group-by #(-> % val :dependents boolean) lib-map)]
(loop [q (into (PersistentQueue/EMPTY) roots)
remaining nonroots
tree {}]
(let [[lib coord :as node] (peek q)
q' (pop q)]
(if node
(let [{children true, not-used false} (group-by #(-> % val :dependents set (contains? lib)) remaining)]
(recur (into q' children) not-used (assoc tree lib (assoc coord :children (map key children)))))
tree)))))
(defn print-tree
"Print lib-map tree to the console"
[lib-map]
(let [tree (make-tree lib-map)]
(letfn [(print-node [lib indent]
(let [{:keys [children] :as coord} (get tree lib)]
(println (str indent (ext/coord-summary lib coord)))
(doseq [child-lib children]
(print-node child-lib (str indent " ")))))]
(doseq [[lib coord] tree :when (-> coord :dependents nil?)]
(print-node lib "")))))
(defn- chase-key
"Given an aliases set and a keyword k, return a flattened vector of path
entries for that k, resolving recursively if needed, or nil."
[aliases k]
(let [path-coll (get aliases k)]
(when (seq path-coll)
(into [] (mapcat #(if (string? %) [[% {:path-key k}]] (chase-key aliases %))) path-coll))))
(defn- flatten-paths
[{:keys [paths aliases] :as deps-edn-map} {:keys [extra-paths] :as classpath-args}]
(let [aliases' (assoc aliases :paths paths :extra-paths extra-paths)]
(into [] (mapcat #(chase-key aliases' %)) (remove nil? [:extra-paths :paths]))))
(defn- validate-paths
[paths]
(doto paths
(->> (map first)
(run! #(when (not (dir/sub-path? (jio/file %)))
(io/printerrln "WARNING: Use of :paths external to the project has been deprecated, please remove:" %))))))
(defn- tree-paths
"Given a lib map, return a vector of all vector paths to included libs in the tree.
Libs are often included at multiple paths."
[lib-map]
(loop [paths []
[lib & libs] (keys lib-map)]
(if lib
(let [parent-paths (:parents (get lib-map lib))]
(recur
(reduce (fn [paths parent-path] (conj paths (conj parent-path lib))) paths parent-paths)
libs))
paths)))
(defn- sort-paths
[lib-paths]
"Given a vector of lib paths, sort in canonical order -
top of tree to bottom, alpha sort at same level"
(->> lib-paths sort (sort-by count) vec))
(defn- flatten-libs
[lib-map {:keys [classpath-overrides] :as classpath-args}]
(let [override-libs (merge-with (fn [coord path] (assoc coord :paths [path])) lib-map classpath-overrides)
lib-order (->> override-libs tree-paths sort-paths (map peek) distinct)
lib-paths (mapcat
#(map vector (get-in override-libs [% :paths]) (repeat {:lib-name %}))
lib-order)]
lib-paths))
(defn make-classpath-map
"Takes a merged deps edn map and a lib map. Extracts the paths for each chosen
lib coordinate, and assembles a classpath map. The classpath-args is a map with
keys that can be used to modify the classpath building operation:
:extra-paths - extra classpath paths to add to the classpath
:classpath-overrides - a map of lib to path, where path is used instead of the coord's paths
Returns a map:
:classpath map of path entry (string) to a map describing where its from, either a :lib-name or :path-key entry.
:classpath-roots coll of the classpath keys in classpath order"
[deps-edn-map lib-map classpath-args]
(let [flat-paths (->> classpath-args (flatten-paths deps-edn-map) validate-paths)
flat-libs (flatten-libs lib-map classpath-args)
all-paths (concat flat-paths flat-libs)
cp (mapv first all-paths)
cp-map (reduce (fn [m [path-str src]] (assoc m path-str src)) {} all-paths)]
{:classpath-roots cp
:classpath cp-map}))
(defn join-classpath
"Takes a coll of string classpath roots and creates a platform sensitive classpath"
[roots]
(str/join File/pathSeparator roots))
(defn make-classpath
"Takes a lib map, and a set of explicit paths. Extracts the paths for each chosen
lib coordinate, and assembles a classpath string using the system path separator.
The classpath-args is a map with keys that can be used to modify the classpath
building operation:
:extra-paths - extra classpath paths to add to the classpath
:classpath-overrides - a map of lib to path, where path is used instead of the coord's paths
Returns the classpath as a string."
{:deprecated "0.9.745"}
[lib-map paths classpath-args]
(-> (make-classpath-map {:paths paths} lib-map classpath-args) :classpath-roots join-classpath))
(defn tool
"Transform project edn for tool by applying tool args (keys = :paths, :deps) and
returning an updated project edn."
[project-edn tool-args]
(let [{:keys [replace-deps replace-paths deps paths]} tool-args]
;; FUTURE: stop supporting :deps + :paths in aliases
(when deps
(io/printerrln "WARNING: Use of :deps in aliases is deprecated - use :replace-deps instead"))
(when paths
(io/printerrln "WARNING: Use of :paths in aliases is deprecated - use :replace-paths instead"))
(cond-> project-edn
(or deps replace-deps) (merge {:deps (merge deps replace-deps)})
(or paths replace-paths) (merge {:paths (vec (concat paths replace-paths))}))))
(defn calc-basis
"Calculates and returns the runtime basis from a master deps edn map, modifying
resolve-deps and make-classpath args as needed.
master-edn - a master deps edn map
args - an optional map of arguments to constituent steps, keys:
:resolve-args - map of args to resolve-deps, with possible keys:
:extra-deps
:override-deps
:default-deps
:threads - number of threads to use during deps resolution
:trace - flag to record a trace log
:classpath-args - map of args to make-classpath-map, with possible keys:
:extra-paths
:classpath-overrides
Returns the runtime basis, which is the initial deps edn map plus these keys:
:resolve-args - the resolve args passed in, if any
:classpath-args - the classpath args passed in, if any
:libs - lib map, per resolve-deps
:classpath - classpath map per make-classpath-map
:classpath-roots - vector of paths in classpath order"
([master-edn]
(calc-basis master-edn nil))
([master-edn {:keys [resolve-args classpath-args]}]
(session/with-session
(let [libs (resolve-deps master-edn resolve-args)
cp (make-classpath-map master-edn libs classpath-args)]
(cond->
(merge master-edn {:libs libs} cp)
resolve-args (assoc :resolve-args resolve-args)
classpath-args (assoc :classpath-args classpath-args))))))
;; Load extensions
(load "/clojure/tools/deps/alpha/extensions/maven")
(load "/clojure/tools/deps/alpha/extensions/local")
(load "/clojure/tools/deps/alpha/extensions/git")
(load "/clojure/tools/deps/alpha/extensions/deps")
(load "/clojure/tools/deps/alpha/extensions/pom")
(comment
(require '[clojure.tools.deps.alpha.util.maven :as mvn])
(def ex-svc (concurrent/new-executor 2))
(calc-basis
{:mvn/repos mvn/standard-repos
:paths ["src" "test"]
:deps {'org.clojure/clojure {:mvn/version "1.9.0"}
'org.clojure/core.memoize {:mvn/version "0.5.8"}}
:aliases {:x {:extra-paths ["a" "b"]}}}
{:classpath-args {:extra-paths ["a" "b"]}})
(expand-deps {'org.clojure/clojure {:mvn/version "1.9.0"}}
nil nil {:mvn/repos mvn/standard-repos} ex-svc true)
(expand-deps {'org.apache.xmlgraphics/batik-transcoder {:mvn/version "1.7"}}
nil nil {:mvn/repos mvn/standard-repos} ex-svc true)
(expand-deps {'org.clojure/clojure {:mvn/version "1.9.0"}
'org.clojure/core.memoize {:mvn/version "0.5.8"}}
nil nil {:mvn/repos mvn/standard-repos} ex-svc true)
(expand-deps {'org.clojure/clojure {:mvn/version "1.9.0"}
'org.clojure/clojurescript {:mvn/version "1.9.946"}
'reagent/reagent {:mvn/version "0.6.0"}}
nil nil {:mvn/repos mvn/standard-repos} ex-svc true)
(expand-deps {'org.clojure/core.async {:mvn/version "0.3.426"}
'reagent/reagent {:mvn/version "0.6.0"}}
nil nil {:mvn/repos mvn/standard-repos} ex-svc true)
(expand-deps {'org.clojure/tools.deps.alpha {:mvn/version "0.4.277"}}
nil nil {:mvn/repos mvn/standard-repos} ex-svc true)
(print-tree
(resolve-deps {:deps {'org.clojure/clojure {:mvn/version "1.8.0"}
'org.clojure/core.memoize {:mvn/version "0.5.8"}}
:mvn/repos mvn/standard-repos} nil))
(make-classpath-map
{:paths ["src"]}
(resolve-deps {:deps {'org.clojure/clojure {:mvn/version "1.9.0-alpha17"}
'org.clojure/core.memoize {:mvn/version "0.5.8"}}
:mvn/repos mvn/standard-repos} nil)
{:extra-paths ["test"]})
(clojure.pprint/pprint
(resolve-deps {:deps {'org.clojure/tools.analyzer.jvm {:mvn/version "0.6.9"}}
:mvn/repos mvn/standard-repos} nil))
(clojure.pprint/pprint
(resolve-deps {:deps {'cheshire/cheshire {:mvn/version "5.7.0"}}
:mvn/repos mvn/standard-repos} nil))
;; top deps win
(clojure.pprint/pprint
(resolve-deps {:deps {'org.clojure/clojure {:mvn/version "1.2.0"}
'cheshire/cheshire {:mvn/version "5.8.0"}}
:mvn/repos mvn/standard-repos} nil))
;; deps replacement
(clojure.pprint/pprint
(resolve-deps {:deps {'cheshire/cheshire {:mvn/version "5.8.0"}}
:mvn/repos mvn/standard-repos}
{:deps {'org.clojure/tools.gitlibs {:mvn/version "0.2.64"}}}))
;; deps addition
(clojure.pprint/pprint
(resolve-deps {:deps {'cheshire/cheshire {:mvn/version "5.8.0"}}
:mvn/repos mvn/standard-repos}
{:extra-deps {'org.clojure/tools.gitlibs {:mvn/version "0.2.64"}}}))
;; validate paths
(make-classpath-map
{:paths ["../foo/src"]}
(resolve-deps {:deps {} :mvn/repos mvn/standard-repos} nil)
nil)
;; override-deps
(make-classpath-map
{:paths ["src"]}
(resolve-deps
{:deps {'org.clojure/core.memoize {:mvn/version "0.5.8"}}
:mvn/repos mvn/standard-repos}
{:override-deps {'org.clojure/clojure {:mvn/version "1.3.0"}}})
nil)
(make-classpath-map
{:paths ["src"]}
(resolve-deps
{:deps {'org.clojure/tools.deps.alpha {:mvn/version "0.1.40"}
'org.clojure/clojure {:mvn/version "1.9.0-alpha17"}}
:mvn/repos mvn/standard-repos} nil) nil)
;; extra paths
(make-classpath-map
{:paths ["src"]}
(resolve-deps
{:deps {'org.clojure/clojure {:mvn/version "1.10.0"}}
:mvn/repos mvn/standard-repos} nil)
{:extra-paths ["extra"]})
;; classpath overrides
(make-classpath-map
{}
(resolve-deps
{:deps {'org.clojure/tools.deps.alpha {:mvn/version "0.1.40"}
'org.clojure/clojure {:mvn/version "1.9.0-alpha17"}}
:mvn/repos mvn/standard-repos} nil)
'{:classpath-overrides {org.clojure/clojure "foo"}})
(resolve-deps
{:deps '{org.clojure/clojure {:mvn/version "1.9.0"}
org.clojure/clojurescript {:mvn/version "1.9.946"}
reagent/reagent {:mvn/version "0.6.0"}}
:mvn/repos mvn/standard-repos}
nil)
;; err case
(resolve-deps {:deps {'bogus/bogus {:mvn/version "1.2.3"}}
:mvn/repos mvn/standard-repos} nil)
(resolve-deps {:deps {'bogus/bogus "1.2.3"}
:mvn/repos mvn/standard-repos} nil)
(require
'[clojure.tools.deps.alpha.extensions.git]
'[clojure.tools.deps.alpha.extensions.deps])
(resolve-deps
{:deps {'foo/bar {:git/url "https://github.com/clojure/core.async.git"
:sha "ecea2539a724a415b15e50f12815b4ab115cfd35"}}}
nil)
(require '[clojure.tools.deps.alpha.util.session :as session])
(time
(do
(session/with-session
(resolve-deps
{:deps {'com.google.cloud/google-cloud-monitoring {:mvn/version "1.78.0"}}
:mvn/repos (merge mvn/standard-repos
{"datomic-cloud" {:url "s3://datomic-releases-1fc2183a.s3-us-east-1.amazonaws.com/maven/releases"}})}
nil))
nil))
)