Skip to content

Commit 73c9529

Browse files
committed
Watch improvements for launchpad
1 parent 63fbf17 commit 73c9529

File tree

3 files changed

+63
-20
lines changed

3 files changed

+63
-20
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,13 @@
22

33
## Added
44

5+
- [watcher] Support for custom basis-fn
6+
- [watcher] Check for aliases in `:extra` deps file
7+
58
## Fixed
69

10+
- [watcher] Fix watcher stop! function
11+
712
## Changed
813

914
# 0.2.37 (2022-08-26 / 34be62f)

repl_session/poke.clj

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
(ns poke
22
(:require [lambdaisland.classpath :as licp]
3-
[clojure.string :as str])
3+
[clojure.string :as str]
4+
[clojure.java.io :as io])
45
(:import clojure.lang.DynamicClassLoader))
56

67
(licp/classpath-chain)
@@ -28,3 +29,10 @@
2829
(recur (.getParent loader))))))
2930

3031
(has-dcl? (licp/parent (licp/root-loader)))
32+
33+
(keep (fn [{:local/keys [root]}]
34+
(when (and root (.exists (io/file root "shadow-cljs.edn")))
35+
root))
36+
(vals (:libs (licp/read-basis))))
37+
38+
(io/resource "public/index.html")

src/lambdaisland/classpath/watch_deps.clj

Lines changed: 49 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,41 @@
88
[nextjournal.beholder :as beholder])
99
(:import java.util.regex.Pattern
1010
java.nio.file.LinkOption
11-
java.nio.file.Paths))
11+
java.nio.file.Paths
12+
java.nio.file.Path))
1213

1314
(def watcher (atom nil))
1415

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+
1521
(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)))
1726

1827
(def process-root-path (canonical-path "."))
1928

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]}]
2146
(locking watcher
2247
(when (and (= :modify type)
2348
;; Before we used "." as the watch path, resulting in a
@@ -28,13 +53,13 @@
2853
;; We now turn `"."` into a canonical path before starting the
2954
;; watcher, which means we get fully qualified filenames for both
3055
;; in this equality check.
31-
(= path deps-path))
56+
(some #{path} deps-paths))
3257
(try
3358
(println "[watch-deps] ✨ Reloading"
3459
(str (.relativize process-root-path path))
3560
"")
3661
(let [added-paths (remove (set (map str (cp/system-classpath)))
37-
(:classpath-roots (deps/create-basis opts)))]
62+
(:classpath-roots (basis opts)))]
3863
(when (not (seq added-paths))
3964
(println "[watch-deps] No new libraries to add."))
4065
(doseq [path added-paths]
@@ -60,31 +85,36 @@
6085
(when w
6186
(println "Stopping existing `deps.edn' watchers")
6287
(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)
7099
(doall
71-
(for [root roots]
100+
(for [[root deps-paths] roots]
72101
(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))))))))
75104

76105
(defn stop!
77106
"Stop a previously started watcher"
78-
[opts]
107+
[& _]
79108
(swap! watcher
80109
(fn [w]
81-
(when w
82-
(beholder/stop w))
110+
(run! beholder/stop w)
83111
nil)))
84112

85113
(comment
86-
(start! {:aliases [:dev]})
114+
(start! {:aliases [:dev]
115+
:extra "deps.local.edn"})
87116

117+
(stop!)
88118
(deps/create-basis {:aliases [:backend]
89119
:extra '{cider/cider-nrepl #:mvn{:version "0.28.5"}
90120
refactor-nrepl/refactor-nrepl #:mvn{:version "3.5.2"}}})

0 commit comments

Comments
 (0)