|
8 | 8 | [nextjournal.beholder :as beholder]) |
9 | 9 | (:import java.util.regex.Pattern |
10 | 10 | java.nio.file.LinkOption |
11 | | - java.nio.file.Paths)) |
| 11 | + java.nio.file.Paths |
| 12 | + java.nio.file.Path)) |
12 | 13 |
|
13 | 14 | (def watcher (atom nil)) |
14 | 15 |
|
| 16 | +(defn path ^Path [root & args] |
| 17 | + (if (and (instance? Path root) (not (seq args))) |
| 18 | + root |
| 19 | + (Paths/get (str root) (into-array String args)))) |
| 20 | + |
15 | 21 | (defn canonical-path [p] |
16 | | - (.toRealPath (Paths/get p (into-array String [])) (into-array LinkOption []))) |
| 22 | + (.toRealPath (path p) (into-array LinkOption []))) |
| 23 | + |
| 24 | +(defn parent-path [p] |
| 25 | + (.getParent (path p))) |
17 | 26 |
|
18 | 27 | (def process-root-path (canonical-path ".")) |
19 | 28 |
|
20 | | -(defn- on-event [deps-path opts {:keys [type path]}] |
| 29 | +(defn basis |
| 30 | + "Default function for (re-)computing the tools.deps basis, which we then use to |
| 31 | + update the classpath. Delegates to [[deps/create-basis]], with one addition: |
| 32 | + if you include an `:extra` option which points at a file (string), then we |
| 33 | + also look in that file for a `:lambdaisland.classpath/aliases`, which are |
| 34 | + additional alias keys to load. This allows having a `deps.local.edn`, where |
| 35 | + you can change the aliases in use without restarting." |
| 36 | + [opts] |
| 37 | + (if-let [f (:basis-fn opts)] |
| 38 | + (f opts) |
| 39 | + (deps/create-basis |
| 40 | + (if (string? (:extra opts)) |
| 41 | + (update opts :aliases into (:lambdaisland.classpath/aliases |
| 42 | + (deps/slurp-deps (io/file (:extra opts))))) |
| 43 | + opts)))) |
| 44 | + |
| 45 | +(defn- on-event [deps-paths opts {:keys [type path]}] |
21 | 46 | (locking watcher |
22 | 47 | (when (and (= :modify type) |
23 | 48 | ;; Before we used "." as the watch path, resulting in a |
|
28 | 53 | ;; We now turn `"."` into a canonical path before starting the |
29 | 54 | ;; watcher, which means we get fully qualified filenames for both |
30 | 55 | ;; in this equality check. |
31 | | - (= path deps-path)) |
| 56 | + (some #{path} deps-paths)) |
32 | 57 | (try |
33 | 58 | (println "[watch-deps] ✨ Reloading" |
34 | 59 | (str (.relativize process-root-path path)) |
35 | 60 | "✨") |
36 | 61 | (let [added-paths (remove (set (map str (cp/system-classpath))) |
37 | | - (:classpath-roots (deps/create-basis opts)))] |
| 62 | + (:classpath-roots (basis opts)))] |
38 | 63 | (when (not (seq added-paths)) |
39 | 64 | (println "[watch-deps] No new libraries to add.")) |
40 | 65 | (doseq [path added-paths] |
|
60 | 85 | (when w |
61 | 86 | (println "Stopping existing `deps.edn' watchers") |
62 | 87 | (run! beholder/stop w)) |
63 | | - (let [basis (deps/create-basis opts) |
64 | | - roots (cons (str process-root-path) |
65 | | - (when (:include-local-roots? opts) |
66 | | - (->> (vals (:libs basis)) |
67 | | - (keep :local/root) |
68 | | - (map canonical-path) |
69 | | - (map str))))] |
| 88 | + (let [basis (basis opts) |
| 89 | + deps-paths (cond-> [(path process-root-path "deps.edn")] |
| 90 | + (:include-local-roots? opts) |
| 91 | + (into (->> (vals (:libs basis)) |
| 92 | + (keep :local/root) |
| 93 | + (map canonical-path) |
| 94 | + (map #(path % "deps.edn")))) |
| 95 | + (string? (:extra opts)) |
| 96 | + (conj (canonical-path (:extra opts)))) |
| 97 | + roots (group-by parent-path deps-paths)] |
| 98 | + (prn roots) |
70 | 99 | (doall |
71 | | - (for [root roots] |
| 100 | + (for [[root deps-paths] roots] |
72 | 101 | (beholder/watch |
73 | | - (partial #'on-event (Paths/get root (into-array String ["deps.edn"])) opts) |
74 | | - root))))))) |
| 102 | + (partial #'on-event deps-paths opts) |
| 103 | + (str root)))))))) |
75 | 104 |
|
76 | 105 | (defn stop! |
77 | 106 | "Stop a previously started watcher" |
78 | | - [opts] |
| 107 | + [& _] |
79 | 108 | (swap! watcher |
80 | 109 | (fn [w] |
81 | | - (when w |
82 | | - (beholder/stop w)) |
| 110 | + (run! beholder/stop w) |
83 | 111 | nil))) |
84 | 112 |
|
85 | 113 | (comment |
86 | | - (start! {:aliases [:dev]}) |
| 114 | + (start! {:aliases [:dev] |
| 115 | + :extra "deps.local.edn"}) |
87 | 116 |
|
| 117 | + (stop!) |
88 | 118 | (deps/create-basis {:aliases [:backend] |
89 | 119 | :extra '{cider/cider-nrepl #:mvn{:version "0.28.5"} |
90 | 120 | refactor-nrepl/refactor-nrepl #:mvn{:version "3.5.2"}}}) |
|
0 commit comments