-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathlive_reload.clj
More file actions
132 lines (116 loc) · 4.8 KB
/
live_reload.clj
File metadata and controls
132 lines (116 loc) · 4.8 KB
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
(ns scicloj.clay.v2.live-reload
(:require [clojure.set :as set]
[babashka.fs :as fs]
[clojure.string :as str]
[nextjournal.beholder :as beholder]
[scicloj.clay.v2.server :as server]))
(def empty-state {:watchers {}
:file-specs {}})
(defonce *state (atom empty-state))
(defn subdir? [dir root]
(and (not= dir root)
(fs/starts-with? dir root)))
(defn subdirs
"Return a subset of `paths1` that are subdirectories of any `paths2`."
[paths1 paths2]
(reduce (fn [acc path]
(if (some #(subdir? path %) paths2)
(conj acc path)
acc))
#{}
paths1))
(defn roots
"Remove from paths any subdirectories."
[paths]
(set/difference paths (subdirs paths paths)))
(defn dirs-to-watch
"Returns any parents of file-paths that are not in watched-dirs."
[watched-dirs dirs file-paths]
(-> (map (comp str fs/parent) file-paths)
(set)
(set/union watched-dirs dirs)
(roots)
(set/difference watched-dirs)))
(defn watched-dirs []
(-> @*state :watchers keys set))
(defn watched-files []
(-> @*state :file-specs keys set))
(defn file-spec [file]
(get-in @*state [:file-specs file]))
(defn watch-files! [files spec]
(swap! *state update :file-specs into
(for [file files]
[file spec])))
(defn watch-dirs!
"Start watching file changes in `dirs` with make."
[dirs make-fn spec]
{:pre [(empty? (set/intersection (set dirs) (watched-dirs)))]}
(when (seq dirs)
(println "Clay watching:" (pr-str dirs))
(println " Editing .clj files in a watched directory will cause them to be rendered"))
(swap! *state update :watchers into
(for [dir dirs]
[dir (beholder/watch (fn watch-callback [{:as event :keys [type path]}]
(let [path (str path)
ext (str/lower-case (fs/extension path))]
(println "Clay file event:" type path)
(when (#{:create :modify} type)
(cond
(= ext "clj")
(make-fn (-> (or (file-spec path)
(assoc spec :source-path path))))
(= ext "cljs")
(do (server/scittle-eval-string! (slurp path))
(println "Clay scittle:" path))))))
dir)])))
(defn stop-watching-dirs!
"Stop watching file changes in all `dirs`."
[dirs]
(doseq [dir dirs]
(beholder/stop (get-in @*state [:watchers dir])))
(swap! *state update :watchers #(apply dissoc %1 %2) dirs))
;; make/make! calls start!, which sets up callbacks to make/make!
;; this is a circular dependency, so we pass make/make! in (dependency injection) instead of referring to it directly
;; source-paths and base-source-path may come from configuration rather than the spec
(defn start!
"Watch directories of a spec"
[make-fn {:as orig-spec :keys [base-source-path watch-dirs]} source-paths]
(let [spec (dissoc orig-spec :live-reload) ;; don't need to re-watch on triggered makes
files (set (map (comp str fs/canonicalize)
(remove (some-fn nil? fs/directory?)
source-paths)))
;; when nothing was specified to watch, watch the base-source-path
watch-dirs (if (and (empty? files)
(empty? watch-dirs)
base-source-path)
#{base-source-path}
(or watch-dirs
["notebooks"]))
dirs (set (map (comp str fs/canonicalize)
watch-dirs))
watch (dirs-to-watch (watched-dirs) dirs files)
current (fs/canonicalize ".")]
(doseq [dir watch]
(when (not (fs/exists? dir))
(fs/create-dir dir)
(println "Clay created a directory:" dir)
(println (str "Now you can create a Clojure file: " dir "/my_notebook.clj"))))
(watch-files! files spec)
;; if we started watching a parent directory, stop watching the subdirs
(stop-watching-dirs! (subdirs (watched-dirs) watch))
;; watch new dirs for notebook changes
(watch-dirs! watch make-fn spec)
(when (empty? files)
(server/update-page! orig-spec))
[:watching (mapv #(str (fs/relativize current %)) (watched-dirs))]))
(defn stop!
"Stop all directory watchers."
[]
(stop-watching-dirs! (watched-dirs))
(reset! *state empty-state)
[:watching nil])
(defn toggle!
[make-fn spec source-paths]
(if (empty? (get @*state :watchers))
(start! make-fn spec source-paths)
(stop!)))