Skip to content

Commit

Permalink
Add a simple nrepl-server task in cider.tasks (#532)
Browse files Browse the repository at this point in the history
It forwards to cider.nrepl.main/init passing options -b|--bind and -p|--port.
  • Loading branch information
arichiardi authored and bbatsov committed Jun 16, 2018
1 parent eb2b5a2 commit 4a6be9f
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 18 deletions.
9 changes: 5 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ it on the command line through the `cider.tasks/add-middleware` task
functionality):

```
boot -d org.clojure/tools.nrepl:0.2.12 -d cider/cider-nrepl:0.x.y-SNAPSHOT -i "(require 'cider.tasks)" cider.tasks/add-middleware -m cider.nrepl.middleware.apropos/wrap-apropos -m cider.nrepl.middleware.version/wrap-version repl -s wait
boot -d org.clojure/tools.nrepl:0.2.12 -d cider/cider-nrepl:0.x.y-SNAPSHOT -i "(require 'cider.tasks)" cider.tasks/add-middleware -m cider.nrepl.middleware.apropos/wrap-apropos -m cider.nrepl.middleware.version/wrap-version cider.tasks/nrepl-server wait
```

Or for all of their projects by adding a `~/.boot/profile.boot` file like so:
Expand All @@ -164,14 +164,15 @@ Or for all of their projects by adding a `~/.boot/profile.boot` file like so:
(set-env! :dependencies '[[org.clojure/tools.nrepl "0.2.12"]
[cider/cider-nrepl "0.x.y-SNAPSHOT"]])

(require '[cider.tasks :refer [add-middleware]])
(require '[cider.tasks :refer [add-middleware nrepl-server]])

(task-options! add-middleware {:middleware '[cider.nrepl.middleware.apropos/wrap-apropos
cider.nrepl.middleware.version/wrap-version]})
```

And then launching `boot add-middleware repl -s wait`. Note that this
is not necessary when using the standard `cider-jack-in`.
And then launching `boot add-middleware nrepl-server wait`.

Note that this is not necessary when using the standard `cider-jack-in`.

For more information visit [boot-clj wiki](https://github.com/boot-clj/boot/wiki/Cider-REPL).

Expand Down
19 changes: 18 additions & 1 deletion src/cider/tasks.clj
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,25 @@
symbol."
[m middleware MIDDLEWARE #{sym} "Name of the middleware to inject"]
(if-let [default-middleware (resolve 'boot.repl/*default-middleware*)]
(do (util/dbug* "Boot's default middleware: %s\n" (vec @@default-middleware))
(do (util/dbug* "Current middleware: %s\n" (vec @@default-middleware))
(swap! @default-middleware concat middleware)
(util/dbug* "After cider-nrepl injection: %s\n" (vec @@default-middleware)))
(util/dbug "Cannot resolve boot.repl/*default-middleware*, skipping middleware injection...\n"))
identity)

(deftask nrepl-server
"Start a nREPL server.
Optionally accepts port and host.
Note that the boot.repl/*default-middleware* atom is read for the list of the
middleware symbols."
[b bind ADDR str "The address server listens on."
p port PORT int "The port to listen on and/or connect to."]
(let [default-mws @@(resolve 'boot.repl/*default-middleware*)]
(util/dbug* "nREPL middleware: %s\n" (vec default-mws))
(boot.core/with-pass-thru [_]
(require 'cider-nrepl.main)
((resolve 'cider-nrepl.main/init) {:middleware default-mws
:port port
:bind bind}))))
44 changes: 31 additions & 13 deletions src/cider_nrepl/main.clj
Original file line number Diff line number Diff line change
Expand Up @@ -8,32 +8,50 @@
(require (symbol (namespace thing)))
(resolve thing))

(def resolve-mw-xf
(comp (map require-and-resolve)
(keep identity)))

(defn- handle-seq-var
[var]
(let [x @var]
(if (sequential? x)
(map require-and-resolve x)
(into [] resolve-mw-xf x)
[var])))

(def mw-xf
(comp (map symbol)
resolve-mw-xf
(mapcat handle-seq-var)))

(defn- ->mw-list
[middleware-var-strs]
(mapcat (comp handle-seq-var require-and-resolve symbol)
middleware-var-strs))
(into [] mw-xf middleware-var-strs))

(defn start-nrepl
[opts]
(let [{:keys [handler middleware bind port]} opts

handler (cond-> (or handler nrepl.server/default-handler)
middleware (apply (->mw-list middleware)))

(defn- start-nrepl
[handler]
(let [nrepl-port (io/file ".nrepl-port")
{:keys [server-socket port] :as server}
(nrepl.server/start-server :handler handler)
bind (-> server-socket (.getInetAddress) (.getHostName))]
(doto nrepl-port
(nrepl.server/start-server :handler handler
:bind bind
:port port)

bind
(-> server-socket (.getInetAddress) (.getHostName))]
(doto (io/file ".nrepl-port")
(spit port)
(.deleteOnExit))
(println (format "nREPL server started on port %d on host %s - nrepl://%s:%d" port bind bind port))
server))

(defn init
[middleware-syms]
(start-nrepl (apply nrepl.server/default-handler (->mw-list middleware-syms)))
;; Return nil so the value doesn't print
nil)
([]
(init nil))
([opts]
(start-nrepl opts)
;; Return nil so the value doesn't print
nil))

0 comments on commit 4a6be9f

Please sign in to comment.