-
Notifications
You must be signed in to change notification settings - Fork 176
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
8 changed files
with
190 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
(ns cider.nrepl.middleware.reload | ||
"Reload changed namespaces. | ||
Alternative to cider.nrepl.middleware.refresh, using clj-reload instead | ||
of tools.namespace." | ||
(:require | ||
[clj-reload.core :as reload] | ||
[clojure.main :refer [repl-caught]] | ||
[clojure.string :as str] | ||
[haystack.analyzer :as analyzer] | ||
[nrepl.middleware.interruptible-eval :refer [*msg*]] | ||
[nrepl.middleware.print :as print] | ||
[nrepl.misc :refer [response-for]] | ||
[nrepl.transport :as transport] | ||
[orchard.misc :as misc])) | ||
|
||
(defn- user-reload | ||
"Resolve clj-reload.core/<sym> from the user project or return fallback." | ||
[sym fallback] | ||
(or (some-> (symbol "clj-reload.core" (str sym)) ;; Don't use mrandorsenized version | ||
resolve) | ||
fallback)) | ||
|
||
(defn- init | ||
"Initialize clj-reload with dirs. | ||
Only used for test, but necessary because of mranderson." | ||
[dirs] | ||
(reload/init {:dirs dirs})) | ||
|
||
(defn respond | ||
[{:keys [transport] :as msg} response] | ||
(transport/send transport (response-for msg response))) | ||
|
||
(defn operation | ||
[msg] | ||
(let [opts {:log-fn (fn [& args] | ||
(respond msg {:progress (str/join " " args)}))} | ||
reload (user-reload 'reload reload/reload) | ||
unload (user-reload 'unload reload/unload)] | ||
(cond | ||
(:all msg) (reload (assoc opts :all true)) | ||
(:clear msg) (unload opts) | ||
:else (reload opts)))) | ||
|
||
(defn- reload-reply | ||
[{:keys [::print/print-fn transport session id] :as msg}] | ||
(let [{:keys [exec]} (meta session)] | ||
(exec id | ||
(fn [] | ||
(try | ||
(operation msg) | ||
(respond msg {:status :ok}) | ||
(catch Throwable error | ||
(respond msg {:status :error | ||
;; TODO assoc :file, :line info if available | ||
:error (analyzer/analyze error print-fn)}) | ||
(binding [*msg* msg | ||
*err* (print/replying-PrintWriter :err msg {})] | ||
(repl-caught error))))) | ||
|
||
(fn [] (respond msg {:status :done}))))) | ||
|
||
(defn handle-reload [handler msg] | ||
(case (:op msg) | ||
"cider.clj-reload/reload" (reload-reply msg) | ||
"cider.clj-reload/reload-all" (reload-reply (assoc msg :all true)) | ||
"cider.clj-reload/reload-clear" (reload-reply (assoc msg :clear true)) | ||
(handler msg))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
(ns cider.nrepl.middleware.reload-test | ||
(:require | ||
[cider.nrepl.middleware.reload :as rl] | ||
[cider.nrepl.test-session :as session] | ||
[clojure.test :refer :all])) | ||
|
||
(use-fixtures :each session/session-fixture) | ||
|
||
(def ^:private dirs-to-reload | ||
;; Limit the scope of what we reload, because (for example) reloading the | ||
;; cider.nrepl.middleware.test-session ns causes *session* in that ns to be | ||
;; unloaded, which breaks session-fixture, and hence all of the below tests. | ||
["test/clj/cider/nrepl/middleware/util"]) | ||
|
||
;; Calling init from reload ns to work around mrandersonized version | ||
;; See cider.nrepl.middleware.refresh-test for another test that suffers from this. | ||
(#'rl/init dirs-to-reload) | ||
|
||
(deftest user-reload | ||
(testing "returns fallback if clojure.tools.namespace isn't loaded" | ||
(with-redefs [resolve (constantly nil)] | ||
(is (= :foo (#'rl/user-reload 'reload :foo)))))) | ||
|
||
(deftest reload-op-test | ||
(testing "reload op works" | ||
(let [response (session/message {:op "cider.clj-reload/reload"})] | ||
;; There is nothing to reload since the files did not change, | ||
;; but the message does come from clj-reload.core/reload. | ||
;; It's two separate messages, but in (:progress response) they are | ||
;; concatenated. | ||
(is (= "Nothing to unloadNothing to reload" (:progress response))) | ||
(is (= #{"done" "ok"} (:status response)))))) | ||
|
||
(deftest reload-all-op-test | ||
(testing "reload-all op works" | ||
(let [response (session/message {:op "cider.clj-reload/reload-all"})] | ||
(is (seq (:progress response))) | ||
(is (= #{"done" "ok"} (:status response)))))) | ||
|
||
(deftest reload-clear-op-test | ||
(testing "reload-all op works" | ||
(let [response (session/message {:op "cider.clj-reload/reload-clear"})] | ||
(is (seq (:progress response))) | ||
(is (= "Nothing to unload" (:progress response))) | ||
(is (= #{"done" "ok"} (:status response)))))) | ||
|