Skip to content

Commit 16aabae

Browse files
authored
dev: add dev-jvm and dev-cljs bb tasks (#297)
Convenience dev tasks to launch nREPL servers.
1 parent 8ecd552 commit 16aabae

File tree

5 files changed

+104
-0
lines changed

5 files changed

+104
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,4 @@
1616
/faddle.clj
1717
/fiddle/
1818
/.cljdoc-preview
19+
/.cljs_node_repl

bb.edn

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
;; commands
1818
download-deps {:task download-deps/-main :doc "bring down Clojure deps"}
1919
apply-import-vars {:task apply-import-vars/-main :doc "(check|gen-code) - export APIs statically from templates"}
20+
dev-jvm {:task dev-repl/dev-jvm :doc "launch jvm nREPL for development, --help for usage"}
21+
dev-cljs {:task dev-repl/dev-cljs :doc "launch cljs nREPL for development, --help for usage"}
2022
lint {:task lint/-main :doc "[--rebuild] lint source code using clj-kondo, eastwood"}
2123
-lint-kondo {:task lint-kondo/-main :doc "[--rebuild]"}
2224
-lint-eastwood {:task lint-eastwood/-main}

deps.edn

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,27 @@
2626
;;
2727
:cljs {:extra-deps {org.clojure/clojurescript {:mvn/version "1.11.132"}}}
2828

29+
;;
30+
;; REPL to support bb dev-jvm & dev-cljs tasks, see script/dev_repl.clj
31+
;;
32+
:nrepl
33+
{:extra-deps {nrepl/nrepl {:mvn/version "1.2.0"}
34+
cider/cider-nrepl {:mvn/version "0.49.1"}}
35+
:jvm-opts ["-XX:-OmitStackTraceInFastThrow"]}
36+
37+
:nrepl/jvm
38+
{:extra-deps {refactor-nrepl/refactor-nrepl {:mvn/version "3.10.0"}}
39+
:main-opts ["-m" "nrepl.cmdline"
40+
"--middleware" "[refactor-nrepl.middleware/wrap-refactor cider.nrepl/cider-middleware]"
41+
"-i"]}
42+
43+
:nrepl/cljs ;; note shadow-cljs does its own thing, this is for a REPL with
44+
;; support for plain old ClojureScript
45+
{:extra-deps {cider/piggieback {:mvn/version "0.5.3"}}
46+
:main-opts ["-m" "nrepl.cmdline"
47+
"--middleware" "[cider.nrepl/cider-middleware cider.piggieback/wrap-cljs-repl]"
48+
"-i"]}
49+
2950
;;
3051
;; Linting
3152
;;

doc/02-developer-guide.adoc

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,26 @@ bb apply-import-vars check
166166
The check command will exit with 0 if no changes are required, otherwise it will exit with 1.
167167
Our build script will run the check command and fail the build if there are any pending changes that have not been applied.
168168

169+
== REPLs
170+
171+
To launch a nREPL server:
172+
173+
----
174+
bb dev-jvm
175+
----
176+
177+
From your IDE, cider connect clj to this REPL server.
178+
179+
180+
For a nREPL server that also includes ClojureScript support:
181+
182+
----
183+
bb dev-cljs
184+
----
185+
186+
From your IDE, cider connect cljs to this REPL server.
187+
188+
169189
== Testing During Development
170190
Your personal preference will likely be different, but during maintenance and refactoring, I found running tests continuously for Clojure and ClojureScript helpful.
171191

script/dev_repl.clj

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
(ns dev-repl
2+
(:require [babashka.cli :as cli]
3+
[babashka.process :as process]
4+
[lread.status-line :as status]))
5+
6+
(def cli-spec {:help {:desc "This usage help"}
7+
8+
;; cider nrepl pass through opts
9+
:host {:ref "<ADDR>"
10+
:alias :h
11+
:default "127.0.0.1"
12+
:desc "Host address"}
13+
:bind {:ref "<ADDR>"
14+
:alias :b
15+
:default "127.0.0.1"
16+
:desc "Bind address"}
17+
:port {:ref "<symbols>"
18+
:coerce :int
19+
:default 0
20+
:alias :p
21+
:desc "Port, 0 for auto-select"}})
22+
23+
(defn- usage-help[]
24+
(status/line :head "Usage help")
25+
(status/line :detail (cli/format-opts {:spec cli-spec :order [:host :bind :port :help]})))
26+
27+
(defn- usage-fail [msg]
28+
(status/line :error msg)
29+
(usage-help)
30+
(System/exit 1))
31+
32+
(defn- parse-opts [args]
33+
(let [opts (cli/parse-opts args {:spec cli-spec
34+
:restrict true
35+
:error-fn (fn [{:keys [msg]}]
36+
(usage-fail msg))})]
37+
(when-let [extra-gunk (-> (meta opts) :org.babashka/cli)]
38+
(usage-fail (str "unrecognized on the command line: " (pr-str extra-gunk))))
39+
opts))
40+
41+
42+
(defn launch-repl [flavor args]
43+
(let [opts (parse-opts args)]
44+
(if (:help opts)
45+
(usage-help)
46+
(do (status/line :head "Launching Clojure %s nREPL" (name flavor))
47+
(process/exec "clj" (str "-M:1.11:test-common:nrepl:nrepl/" (case flavor
48+
:cljs "cljs:cljs"
49+
:jvm "jvm"))
50+
"-h" (:host opts)
51+
"-b" (:bind opts)
52+
"-p" (:port opts))))))
53+
54+
;; Entry points
55+
(defn dev-jvm [& args]
56+
(launch-repl :jvm args))
57+
58+
59+
(defn dev-cljs [& args]
60+
(launch-repl :cljs args))

0 commit comments

Comments
 (0)