Skip to content

Commit 1bc7bdc

Browse files
inwalkeddubbbatsov
authored andcommitted
[Fix #2698] Infer available figwheel builds automatically
This commit also expands the documentation for working with figwheel.
1 parent 03c0a23 commit 1bc7bdc

File tree

4 files changed

+107
-18
lines changed

4 files changed

+107
-18
lines changed

cider.el

+15-4
Original file line numberDiff line numberDiff line change
@@ -762,13 +762,24 @@ Figwheel for details."
762762
:safe (lambda (s) (or (null s) (stringp s)))
763763
:package-version '(cider . "0.18.0"))
764764

765+
(defun cider--figwheel-main-get-builds ()
766+
"Extract build names from the <build-id>.cljs.edn config files in the project root."
767+
(let ((builds (directory-files (clojure-project-dir) nil ".*\\.cljs\\.edn")))
768+
(mapcar (lambda (f) (string-match "^\\(.*\\)\\.cljs\\.edn" f)
769+
(match-string 1 f)) builds)))
770+
765771
(defun cider-figwheel-main-init-form ()
766772
"Produce the figwheel-main ClojureScript init form."
767773
(let ((form "(do (require 'figwheel.main) (figwheel.main/start %s))")
768-
(options (string-trim
769-
(or cider-figwheel-main-default-options
770-
(read-from-minibuffer "Select figwheel-main build (e.g. :dev): ")))))
771-
(format form (cider-normalize-cljs-init-options options))))
774+
(builds (cider--figwheel-main-get-builds)))
775+
(cond
776+
(cider-figwheel-main-default-options
777+
(format form (cider-normalize-cljs-init-options (string-trim cider-figwheel-main-default-options))))
778+
779+
(builds
780+
(format form (cider-normalize-cljs-init-options (completing-read "Select figwheel-main build: " builds))))
781+
782+
(t (user-error "No figwheel-main build files (<build-id>.cljs.edn) were found")))))
772783

773784
(defcustom cider-custom-cljs-repl-init-form nil
774785
"The form used to start a custom ClojureScript REPL.

doc/modules/ROOT/pages/basics/clojurescript.adoc

+70-7
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ to connect to an already running nREPL server using
3030

3131
To setup piggieback, add the following dependencies to your project
3232
(`project.clj` in a Leiningen based project or `build.boot` in a Boot
33-
project):
33+
project or `deps.edn`):
3434

3535
[source,clojure]
3636
----
@@ -56,11 +56,21 @@ or in `build.boot`:
5656
repl {:middleware '[cider.piggieback/wrap-cljs-repl]})
5757
----
5858

59+
or in `deps.edn`:
60+
61+
[source,clojure]
62+
----
63+
{:aliases { :cider-cljs { :main-opts
64+
["-m" "nrepl.cmdline" "--middleware"
65+
"[cider.nrepl/cider-middleware,cider.piggieback/wrap-cljs-repl]"]}}}
66+
----
67+
5968
== Starting a ClojureScript REPL
6069

61-
Open a file in your project and type kbd:[M-x]
62-
`cider-jack-in-cljs` kbd:[RET]. This will start up the nREPL
63-
server and create a ClojureScript REPL buffer.
70+
Open a ClojureScript file in your project and type kbd:[M-x]
71+
`cider-jack-in-cljs` kbd:[RET]. With the correct configuration
72+
and after answering a few prompts this will start up the
73+
nREPL server and create a ClojureScript REPL buffer.
6474

6575
NOTE: Prior to CIDER 0.18, `cider-jack-in-cljs` would create both a Clojure and
6676
a ClojureScript REPL. In CIDER 0.18+ if you want to create both REPLs
@@ -99,7 +109,7 @@ You can also modify the known ClojureScript REPLs on a per-project basis using
99109

100110
[source,lisp]
101111
----
102-
;; replace the list of REPLs types and set some default
112+
;; replace the list of REPL types and set some default
103113
((nil
104114
(cider-default-cljs-repl . super-cljs)
105115
(cider-cljs-repl-types . ((super-cljs "(do (foo) (bar))")))))
@@ -130,7 +140,7 @@ ClojureScript REPL, you can invoke
130140
== Setting up a ClojureScript REPL
131141

132142
The following sections describe the configurations for several common
133-
CloudScript REPL use cases.
143+
ClojureScript REPL use cases.
134144

135145
=== Browser-Connected ClojureScript REPL
136146

@@ -233,6 +243,11 @@ them to your favourite build tool is up to you.
233243

234244
You can also use https://github.com/bhauman/figwheel-main[Figwheel-main] with CIDER.
235245

246+
==== Leiningen figwheel-main setup
247+
248+
NOTE: This setup is only necessary if `cider-inject-dependencies-at-jack-in` has
249+
been changed from its default setting of `enabled`, as described more fully above.
250+
236251
. Add this to your dev `:dependencies` (not needed for `cider-jack-in-cljs`):
237252
+
238253
[source,clojure]
@@ -248,9 +263,57 @@ You can also use https://github.com/bhauman/figwheel-main[Figwheel-main] with CI
248263
----
249264
+
250265
. Start the REPL with `cider-jack-in-cljs` (kbd:[C-c C-x (C-)j (C-)s]). When CIDER prompts about the ClojureScript REPL type,
251-
type `figwheel-main`.
266+
select `figwheel-main`.
252267
. Select the Figwheel build to run when prompted for it. (e.g. `:dev`).
253268

269+
==== Clojure CLI figwheel-main setup
270+
271+
. Ensure your `deps.edn` contains these dependencies available from an
272+
alias such as `fig`. These should already be present if you used the
273+
https://github.com/bhauman/figwheel-main-template[figwheel-main-template]
274+
with https://github.com/seancorfield/clj-new[clj-new] to generate your
275+
deps.edn.
276+
+
277+
[source,clojure]
278+
----
279+
{ :aliases {:fig {:extra-deps
280+
{com.bhauman/rebel-readline-cljs {:mvn/version "0.1.4"}
281+
com.bhauman/figwheel-main {:mvn/version "0.2.3"}}}}}
282+
----
283+
+
284+
. Add this option to your project's `.dir-locals.el` before opening the
285+
ClojureScript file in a buffer from which you'll be starting the REPL.
286+
(If the file is already open you can either close and open it again or
287+
use `revert-buffer`.)
288+
+
289+
[source,lisp]
290+
----
291+
((clojurescript-mode . ((cider-clojure-cli-global-options . "-A:fig"))))
292+
----
293+
+
294+
. Start the REPL with `cider-jack-in-cljs` (kbd:[C-c C-x (C-)j (C-)s]).
295+
296+
NOTE: If you didn't setup `.dir-locals.el` you can edit the command-line
297+
in the minibuffer and insert "-A:fig" at the beginning after the `clojure`
298+
executable. To do this prepend calling `cider-jack-in-cljs` with the
299+
universal argument (e.g. C-u).
300+
301+
. When CIDER prompts for the ClojureScript REPL type, select `figwheel-main`.
302+
. When CIDER prompts for the build name, select one of the build name
303+
options which were generated by finding files named <build>.cljs.edn in
304+
the project root directory.
305+
306+
WARNING: If instead of selecting one of the build name options provided,
307+
you enter either a non-existent build name (no matching .cljs.edn file)
308+
or an empty build name, CIDER will forward that on to figwheel-main which
309+
will report an error.
310+
311+
NOTE: For more advanced usage, you can provide any options supported
312+
by `figwheel.main/start` at either the prompt for the build name or
313+
using the `cider-figwheel-main-default-options`.
314+
See https://github.com/bhauman/figwheel-main/blob/master/src/figwheel/main/api.clj[figwheel.main.api/start]
315+
for details.
316+
254317
=== Using shadow-cljs
255318

256319
Provided you've configured your project correctly, you can simply use

doc/modules/ROOT/pages/basics/middleware_setup.adoc

+18-3
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,12 @@ automatically injects this middleware and other dependencies as required.
1212
NOTE: In the past, if you were setting up CIDER, you might have had to
1313
modify `profiles.clj` or `profile.boot`. CIDER now handles
1414
everything automatically and you don't need to add anything
15-
special to these files.
15+
special to these files. The same is true of your `deps.edn` file.
1616

1717
If you prefer a standalone REPL, you will need to invoke
1818
`cider-connect` instead of `cider-jack-in` and manually add the
1919
dependencies to your Clojure project (explained in the following
20-
section).
20+
sections).
2121

2222
== Setting Up a Standalone REPL
2323

@@ -68,7 +68,22 @@ For more information visit https://github.com/boot-clj/boot/wiki/Cider-REPL[boot
6868

6969
=== Using tools.deps
7070

71-
TODO
71+
You can add the following aliases to your deps.edn in order to launch
72+
a standalone Clojure(Script) nREPL server with CIDER middleware from
73+
the commandline with something like `clj -A:cider-clj`. Then from emacs
74+
run `cider-connect` or `cider-connect-cljs`.
75+
76+
[source,clojure]
77+
----
78+
:cider-clj {:extra-deps {cider/cider-nrepl {:mvn/version "0.22.3"}}
79+
:main-opts ["-m" "nrepl.cmdline" "--middleware" "[cider.nrepl/cider-middleware]"]}
80+
81+
:cider-cljs {:extra-deps {org.clojure/clojurescript {:mvn/version "1.10.339"}
82+
cider/cider-nrepl {:mvn/version "0.22.3"}
83+
cider/piggieback {:mvn/version "0.4.1"}}
84+
:main-opts ["-m" "nrepl.cmdline" "--middleware"
85+
"[cider.nrepl/cider-middleware,cider.piggieback/wrap-cljs-repl]"]}
86+
----
7287

7388
=== Using Gradle
7489

test/cider-tests.el

+4-4
Original file line numberDiff line numberDiff line change
@@ -55,16 +55,16 @@
5555
;; not necessary as of this writing, but it can't hurt
5656
(setq-local cider-figwheel-main-default-options nil))
5757
(it "leaves keywords alone"
58-
(spy-on 'read-from-minibuffer :and-return-value " :prod ")
58+
(spy-on 'completing-read :and-return-value " :prod ")
5959
(expect (cider-figwheel-main-init-form) :to-equal "(do (require 'figwheel.main) (figwheel.main/start :prod))"))
6060
(it "leaves maps alone"
61-
(spy-on 'read-from-minibuffer :and-return-value " {:c 3 :d 4}")
61+
(spy-on 'completing-read :and-return-value " {:c 3 :d 4}")
6262
(expect (cider-figwheel-main-init-form) :to-equal "(do (require 'figwheel.main) (figwheel.main/start {:c 3 :d 4}))"))
6363
(it "leaves s-exprs alone"
64-
(spy-on 'read-from-minibuffer :and-return-value "(keyword \"dev\") ")
64+
(spy-on 'completing-read :and-return-value "(keyword \"dev\") ")
6565
(expect (cider-figwheel-main-init-form) :to-equal "(do (require 'figwheel.main) (figwheel.main/start (keyword \"dev\")))"))
6666
(it "prepends colon to plain names"
67-
(spy-on 'read-from-minibuffer :and-return-value "prod ")
67+
(spy-on 'completing-read :and-return-value "prod ")
6868
(expect (cider-figwheel-main-init-form) :to-equal "(do (require 'figwheel.main) (figwheel.main/start :prod))"))))
6969

7070
(describe "cider-project-type"

0 commit comments

Comments
 (0)