|
80 | 80 | (when (find-ns ns-name)
|
81 | 81 | (alias alias-sym ns-name))))
|
82 | 82 |
|
83 |
| -(defn- do-refresh [scan-opts after-sym] |
84 |
| - (when after-sym |
85 |
| - (assert (symbol? after-sym) ":after value must be a symbol") |
86 |
| - (assert (namespace after-sym) |
87 |
| - ":after value must be a namespace-qualified symbol")) |
88 |
| - (let [current-ns-name (ns-name *ns*) |
89 |
| - current-ns-refers (referred *ns*) |
90 |
| - current-ns-aliases (aliased *ns*)] |
91 |
| - (alter-var-root #'refresh-tracker dir/scan-dirs refresh-dirs scan-opts) |
92 |
| - (alter-var-root #'refresh-tracker remove-disabled) |
93 |
| - (print-pending-reloads refresh-tracker) |
94 |
| - (alter-var-root #'refresh-tracker reload/track-reload) |
95 |
| - (in-ns current-ns-name) |
96 |
| - (let [result (print-and-return refresh-tracker)] |
97 |
| - (if (= :ok result) |
98 |
| - (if after-sym |
99 |
| - (if-let [after (ns-resolve *ns* after-sym)] |
100 |
| - (after) |
101 |
| - (throw (Exception. |
102 |
| - (str "Cannot resolve :after symbol " after-sym)))) |
103 |
| - result) |
104 |
| - ;; There was an error, recover as much as we can: |
105 |
| - (do (when-not (or (false? (::unload (meta *ns*))) |
| 83 | +(defn scan |
| 84 | + "Scans directories for files which have changed since the last time |
| 85 | + 'scan' or 'refresh' was run; updates the dependency tracker |
| 86 | + with new/changed/deleted files. |
| 87 | + |
| 88 | + Optional argument is map of options: |
| 89 | + |
| 90 | + :platform Either clj (default) or cljs, both defined in |
| 91 | + clojure.tools.namespace.find, controls file extensions |
| 92 | + and reader options. |
| 93 | + |
| 94 | + :add-all? If true, assumes all extant files are modified regardless |
| 95 | + of filesystem timestamps. |
| 96 | + |
| 97 | + Returns map with keys: |
| 98 | + |
| 99 | + ::track/unload list of namespace symbols that will be unloaded |
| 100 | + ::track/load list of namespace symbols that will be loaded" |
| 101 | + ([] |
| 102 | + (scan nil)) |
| 103 | + ([options] |
| 104 | + (alter-var-root #'refresh-tracker |
| 105 | + #(-> % |
| 106 | + (dir/scan-dirs refresh-dirs options) |
| 107 | + (remove-disabled))))) |
| 108 | + |
| 109 | +(defn refresh-scanned |
| 110 | + "Reloads namespaces in dependency order. Does not scan directories again, |
| 111 | + expected to be used after 'scan'. |
| 112 | + |
| 113 | + Returns :ok or an error; sets the latest exception to |
| 114 | + clojure.core/*e (if *e is thread-bound). |
| 115 | + |
| 116 | + The directories to be scanned are controlled by 'set-refresh-dirs'; |
| 117 | + defaults to all directories on the Java classpath. |
| 118 | + |
| 119 | + Options are key-value pairs. Valid options are: |
| 120 | + |
| 121 | + :after Namespace-qualified symbol naming a zero-argument |
| 122 | + function to be invoked after a successful refresh. This |
| 123 | + symbol will be resolved *after* all namespaces have |
| 124 | + been reloaded." |
| 125 | + [& options] |
| 126 | + (let [{:keys [after]} options] |
| 127 | + (when after |
| 128 | + (assert (symbol? after) ":after value must be a symbol") |
| 129 | + (assert (namespace after) |
| 130 | + ":after value must be a namespace-qualified symbol")) |
| 131 | + (let [current-ns-name (ns-name *ns*) |
| 132 | + current-ns-refers (referred *ns*) |
| 133 | + current-ns-aliases (aliased *ns*)] |
| 134 | + (print-pending-reloads refresh-tracker) |
| 135 | + (alter-var-root #'refresh-tracker reload/track-reload) |
| 136 | + (in-ns current-ns-name) |
| 137 | + (let [result (print-and-return refresh-tracker)] |
| 138 | + (if (= :ok result) |
| 139 | + (if after |
| 140 | + (if-let [after (ns-resolve *ns* after)] |
| 141 | + (after) |
| 142 | + (throw (Exception. |
| 143 | + (str "Cannot resolve :after symbol " after)))) |
| 144 | + result) |
| 145 | + ;; There was an error, recover as much as we can: |
| 146 | + (do (when-not (or (false? (::unload (meta *ns*))) |
106 | 147 | (false? (::load (meta *ns*))))
|
107 |
| - (recover-ns current-ns-refers current-ns-aliases)) |
| 148 | + (recover-ns current-ns-refers current-ns-aliases)) |
108 | 149 | ;; Return the Exception to the REPL:
|
109 |
| - result))))) |
| 150 | + result)))))) |
110 | 151 |
|
111 | 152 | (defn disable-unload!
|
112 | 153 | "Adds metadata to namespace (or *ns* if unspecified) telling
|
|
142 | 183 | been reloaded."
|
143 | 184 | [& options]
|
144 | 185 | (let [{:keys [after]} options]
|
145 |
| - (do-refresh {:platform find/clj} after))) |
| 186 | + (scan {:platform find/clj}) |
| 187 | + (apply refresh-scanned options))) |
146 | 188 |
|
147 | 189 | (defn refresh-all
|
148 | 190 | "Scans source code directories for all Clojure source files and
|
|
159 | 201 | been reloaded."
|
160 | 202 | [& options]
|
161 | 203 | (let [{:keys [after]} options]
|
162 |
| - (do-refresh {:platform find/clj :add-all? true} after))) |
| 204 | + (scan {:platform find/clj |
| 205 | + :add-all? true}) |
| 206 | + (apply refresh-scanned options))) |
163 | 207 |
|
164 | 208 | (defn set-refresh-dirs
|
165 | 209 | "Sets the directories which are scanned by 'refresh'. Supports the
|
|
0 commit comments