Skip to content

Commit e3ffc53

Browse files
committed
integrate rebel-as-nrepl-client
Signed-off-by: Sean Corfield <[email protected]>
1 parent 1b470d1 commit e3ffc53

File tree

3 files changed

+31
-11
lines changed

3 files changed

+31
-11
lines changed

README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ TL;DR: add the following dependency and then start a REPL with `clj -M:dev/repl`
2727
```
2828
There is also a `bin/repl` bash script that runs `clojure -M:1.12:portal:test:cider-nrepl:rebel:dev/repl`
2929
to start an nREPL server with CIDER middleware, and then a Rebel Readline
30-
interactive REPL, with Portal available (and `clojure.tools.logging`, if
31-
present, patched to `tap>` all log messages for Portal).
30+
interactive REPL, as a client to that nREPL server, with Portal available (and `clojure.tools.logging`, if
31+
present, patched to `tap>` all log messages for Portal, also `logging4j2` -- my log4j2 wrapper).
3232

3333
I recently added the `:allow-attach-self` alias which sets the JVM property
3434
`-Djdk.attach.allowAttachSelf` for JDK 21+ so that
@@ -89,15 +89,15 @@ There are aliases to pull in and start various REPL-related tools:
8989
* `clojure -M:nrepl:portal:dev/repl` -- ...with Portal (& middleware) or
9090
* `clojure -M:cider-nrepl:dev/repl` -- CIDER nREPL server or
9191
* `clojure -M:cider-nrepl:portal:dev/repl` -- ...with Portal (& middleware) or
92-
* `clojure -M:rebel:nrepl:dev/repl` -- Rebel Readline REPL + basic nREPL server or
92+
* `clojure -M:rebel:nrepl:dev/repl` -- Rebel Readline nREPL client + basic nREPL server or
9393
* `clojure -M:rebel:nrepl:portal:dev/repl` -- ...with Portal (& middleware) or
94-
* `clojure -M:rebel:cider-nrepl:dev/repl` -- Rebel Readline REPL + CIDER nREPL server or
94+
* `clojure -M:rebel:cider-nrepl:dev/repl` -- Rebel Readline nREPL client + CIDER nREPL server or
9595
* `clojure -M:rebel:cider-nrepl:portal:dev/repl` -- ...with Portal (& middleware) or
9696
* Also works with Figwheel Main (now that I've started doing ClojureScript!):
9797
* `clojure -M:portal:fig:build:dev/repl` or
9898
* `:classes` -- adds the `classes` folder to your classpath to pick up compiled code (e.g., see https://clojure.org/guides/dev_startup_time)
9999
* `:socket` -- starts a Socket REPL on port 50505; can be combined with other aliases since this is just a JVM option
100-
* `:rebel` -- starts a [Rebel Readline](https://github.com/bhauman/rebel-readline) REPL
100+
* `:rebel` -- starts a [Rebel Readline](https://github.com/bhauman/rebel-readline) REPL; note that this also loads the Rebel Readline nREPL client library
101101
* `:nrepl` -- starts a (headless) [nREPL server](https://nrepl.org/) on a random available port; `clojure -M:nrepl`
102102
* `:cider-nrepl` -- starts a (headless) CIDER-enhanced [nREPL server](https://nrepl.org/) on a random available port; `clojure -M:cider-nrepl`
103103

deps.edn

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,9 @@
8484
:socket {:jvm-opts ["-Dclojure.server.repl={:address,\"0.0.0.0\",:port,50505,:accept,clojure.core.server/repl}"]}
8585
;; - see https://github.com/bhauman/rebel-readline
8686
;; - start a Rebel Readline REPL:
87-
:rebel {:extra-deps {com.bhauman/rebel-readline {:mvn/version "0.1.5"}}
87+
:rebel {:extra-deps {com.bhauman/rebel-readline {:mvn/version "0.1.5"}
88+
;; in case we want to use Rebel as an nREPL client:
89+
com.bhauman/rebel-readline-nrepl {:mvn/version "0.1.6"}}
8890
;; see https://github.com/fusesource/jansi/issues/301
8991
:jvm-opts ["--enable-native-access=ALL-UNNAMED"]
9092
:main-opts ["-m" "rebel-readline.main"]}

src/org/corfield/dev/repl.clj

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,15 @@
7373
(catch Throwable _))
7474
(apply log*-fn logger level more)))))
7575

76+
#_{:clj-kondo/ignore [:unused-private-var]}
77+
(defn- connect-to-nrepl
78+
"Credit to https://github.com/alexander-yakushev for this approach."
79+
[_host port _config]
80+
(let [start-repl (resolve 'rebel-readline.nrepl.main/start-repl)]
81+
(println "Connecting Rebel Readline to nREPL server on port"
82+
(str port "..."))
83+
(start-repl {:port port})))
84+
7685
(defn -main
7786
"If Jedi Time is on the classpath, require it (so that Java Time
7887
objects will support datafy/nav).
@@ -92,7 +101,7 @@
92101
* if Figwheel Main is on the classpath then start that, else
93102
* if Rebel Readline is on the classpath then start that, else
94103
* start a plain ol' Clojure REPL."
95-
[& args]
104+
[& _]
96105
;; jedi-time?
97106
(try
98107
(require 'jedi-time.core)
@@ -167,11 +176,16 @@
167176
["Figwheel Main" #(figgy "-b" "dev" "-r")])
168177
(catch Throwable _))
169178
(try ; Rebel Readline?
170-
(let [rebel-main (requiring-resolve 'rebel-readline.main/-main)]
179+
(let [rebel-main (requiring-resolve 'rebel-readline.main/-main)
180+
rebel-nrepl (try (require 'rebel-readline.nrepl.main)
181+
true
182+
(catch Throwable _))]
171183
(try
172184
(require 'nrepl.cmdline)
173185
;; both Rebel Readline and nREPL are on the classpath!
174-
[(str "Rebel Readline + nREPL Server"
186+
[(str "Rebel Readline "
187+
(when rebel-nrepl "Client ")
188+
"+ nREPL Server"
175189
(when (seq middleware)
176190
(str " with " (str/join ", " (map first middleware)))))
177191
(fn []
@@ -181,7 +195,9 @@
181195
"-m" "nrepl.cmdline"
182196
(into (or mw-args [])
183197
["--interactive"
184-
"-f" "rebel-readline.main/-main"])))]
198+
"-f" (if rebel-nrepl
199+
"org.corfield.dev.repl/connect-to-nrepl"
200+
"rebel-readline.main/-main")])))]
185201
(catch Throwable _
186202
;; only Rebel Readline is on the classpath:
187203
["Rebel Readline" rebel-main])))
@@ -202,7 +218,9 @@
202218
(System/exit 0)))
203219

204220
(in-ns 'user)
205-
(defn uptime []
221+
(require 'clojure.string) ; to satisfy clj-kondo :)
222+
#_{:clj-kondo/ignore [:unused-private-var]}
223+
(defn- uptime []
206224
(-> (java.lang.management.ManagementFactory/getRuntimeMXBean)
207225
(.getUptime)
208226
(java.time.Duration/ofMillis)

0 commit comments

Comments
 (0)