Skip to content

Commit 19124d4

Browse files
authored
Merge pull request #3228 from ajoberstar/gradle-inject
Support injecting dependencies into Gradle
2 parents 6f2d3ba + 486a924 commit 19124d4

File tree

7 files changed

+147
-34
lines changed

7 files changed

+147
-34
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
- [#3226](https://github.com/clojure-emacs/cider/pull/3226): Populate completions metadata, making it possible to change the style of completion via `completion-category-override` or `completion-category-defaults`.
88
- [#2946](https://github.com/clojure-emacs/cider/issues/2946): Add custom var `cider-merge-sessions` to allow combining sessions in two different ways: Setting `cider-merge-sessions` to `'host` will merge all sessions associated with the same host within a project. Setting it to `'project` will combine all sessions of a project irrespective of their host.
99
- Support Gradle jack-in via the Gradle wrapper, instead of just a globally installed `gradle` on the `PATH`.
10+
- Gradle projects can now inject dependencies and middleware as with other build tools (dependency injection requires [Clojurephant](https://github.com/clojurephant/clojurephant) 0.7.0-alpha.6 or higher)
1011

1112
## Changes
1213

cider.el

Lines changed: 48 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -577,6 +577,49 @@ and MIDDLEWARES. PARAMS and MIDDLEWARES are passed on to
577577
(cider-boot-dependencies (append (cider--jack-in-required-dependencies) dependencies))
578578
(cider-boot-middleware-task params middlewares)))
579579

580+
(defun cider--gradle-dependency-notation (dependency)
581+
"Returns Gradle's GAV dependency syntax.
582+
For a \"group/artifact\" \"version\") DEPENDENCY list
583+
return as group:artifact:version notation."
584+
(let ((group-artifact (replace-regexp-in-string "/" ":" (car dependency)))
585+
(version (cadr dependency)))
586+
(format "%s:%s" group-artifact version)))
587+
588+
(defun cider--gradle-jack-in-property (dependencies)
589+
"Returns Clojurephant's dependency jack-in property.
590+
For DEPENDENCIES, translates to Gradle's dependency notation
591+
using `cider--gradle-dependency-notation`.''"
592+
(if (seq-empty-p dependencies)
593+
""
594+
(shell-quote-argument
595+
(concat "-Pdev.clojurephant.jack-in.nrepl="
596+
(mapconcat #'cider--gradle-dependency-notation dependencies ",")))))
597+
598+
(defun cider--gradle-middleware-params (middlewares)
599+
"Returns Gradle-formatted middleware params.
600+
Given a list of MIDDLEWARES symbols, this returns
601+
the Gradle parameters expected by Clojurephant's
602+
ClojureNRepl task."
603+
(mapconcat (lambda (middleware)
604+
(shell-quote-argument (concat "--middleware=" middleware)))
605+
middlewares
606+
" "))
607+
608+
(defun cider-gradle-jack-in-dependencies (global-opts params dependencies middlewares)
609+
"Create gradle jack in dependencies.
610+
Does so by concatenating GLOBAL-OPTS, DEPENDENCIES,
611+
and MIDDLEWARES. GLOBAL-OPTS and PARAMS are taken as-is.
612+
DEPENDENCIES are translated into Gradle's typical
613+
group:artifact:version notation and MIDDLEWARES are
614+
prepared as arguments to Clojurephant's ClojureNRepl task."
615+
(concat global-opts
616+
(unless (seq-empty-p global-opts) " ")
617+
(cider--gradle-jack-in-property (append (cider--jack-in-required-dependencies) dependencies))
618+
" "
619+
params
620+
(unless (seq-empty-p params) " ")
621+
(cider--gradle-middleware-params middlewares)))
622+
580623
(defun cider--lein-artifact-exclusions (exclusions)
581624
"Return an exclusions vector described by the elements of EXCLUSIONS."
582625
(if exclusions
@@ -717,10 +760,12 @@ dependencies."
717760
params
718761
(cider-add-clojure-dependencies-maybe
719762
cider-jack-in-dependencies)))
720-
('gradle (concat
763+
('gradle (cider-gradle-jack-in-dependencies
721764
global-opts
722-
(unless (seq-empty-p global-opts) " ")
723-
params))
765+
params
766+
(cider-add-clojure-dependencies-maybe
767+
cider-jack-in-dependencies)
768+
(cider-jack-in-normalized-nrepl-middlewares)))
724769
(_ (error "Unsupported project type `%S'" project-type))))
725770

726771

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

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ 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. The same is true of your `deps.edn` file.
15+
special to these files. The same is true of your `deps.edn` file and
16+
your `build.gradle` (as of Clojurephant 0.7.0-alpha.6).
1617

1718
If you prefer a standalone REPL, you will need to invoke
1819
`cider-connect` instead of `cider-jack-in` and manually add the
@@ -45,7 +46,7 @@ A minimal `profiles.clj` for CIDER would be:
4546

4647
[source,clojure]
4748
----
48-
{:repl {:plugins [[cider/cider-nrepl "0.28.4"]
49+
{:repl {:plugins [[cider/cider-nrepl "0.28.5"]
4950
[mx.cider/enrich-classpath "1.9.0"]]}}
5051
----
5152

@@ -95,7 +96,24 @@ run `cider-connect` or `cider-connect-cljs`.
9596

9697
=== Using Gradle
9798

98-
NOTE: This section is currently a stub. Contributions welcome!
99+
NOTE: Make sure you're using https://github.com/clojurephant/clojurephant[Clojurephant] 0.4.0 or newer.
100+
101+
.build.gradle
102+
[source, groovy]
103+
----
104+
dependencies {
105+
devImplementation 'nrepl:nrepl:0.9.0'
106+
devImplementation 'cider:cider-nrepl:0.28.5'
107+
}
108+
109+
tasks.named('clojureRepl') {
110+
middleware = ['cider.nrepl/cider-middleware']
111+
}
112+
----
113+
114+
You can then launch the nREPL server from the command line via: `./gradlew clojureRepl`.
115+
116+
For more information, see the https://clojurephant.dev[Clojurephant docs].
99117

100118
=== Using Maven
101119

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

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,8 @@ Here's how you can modify the injected dependencies for `cider-jack-in-clj`:
8888
"foo/bar" "1.0")
8989
----
9090

91+
IMPORTANT: Always use the fully qualified `group/artifact` (e.g. `re-frame/re-frame`) in these dependencies, since only Leiningen supports the bare `re-frame` syntax.
92+
9193
CIDER will also inject the most recent version of nREPL that it supports. This is a simple
9294
trick to override the version of nREPL bundled with your build tool (e.g. Leiningen), so you can gain
9395
access to the newest nREPL features. Generally that's one aspect of CIDER's inner workings
@@ -104,10 +106,6 @@ for example, if your project defaults to an older version of Clojure than that
104106
supported by the CIDER middleware. Set `cider-jack-in-auto-inject-clojure`
105107
appropriately to enable this.
106108

107-
NOTE: CIDER does not currently support
108-
dependency auto-injection for Gradle projects. Unfortunately there's no
109-
way to pass extra dependencies to Gradle via its command-line interface.
110-
111109
=== Jacking-in without a Project
112110

113111
If you try to run `cider-jack-in` outside a project

doc/modules/ROOT/pages/cljs/configuration.adoc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ variable. Here's an example:
1717
(cider-add-to-alist 'cider-jack-in-cljs-dependencies "weasel/weasel" "0.7.1")
1818
----
1919

20+
IMPORTANT: Always use the fully qualified `group/artifact` (e.g. `re-frame/re-frame`) in these dependencies, since only Leiningen supports the bare `re-frame` syntax.
21+
2022
Typically, modifying this variable is not needed, as ClojureScript dependencies are declared
2123
explicitly in your project configuration most of the time.
2224

doc/modules/ROOT/pages/cljs/up_and_running.adoc

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,21 @@ or in `deps.edn`:
5555
"[cider.nrepl/cider-middleware,cider.piggieback/wrap-cljs-repl]"]}}}
5656
----
5757

58+
or in `build.gradle`:
59+
60+
[source, groovy]
61+
----
62+
dependencies {
63+
devImplementation 'nrepl:nrepl:0.9.0'
64+
devImplementation 'cider:cider-nrepl:0.28.5'
65+
devImplementation 'cider:cider-piggieback:0.5.3'
66+
}
67+
68+
tasks.named('clojureRepl') {
69+
middleware = ['cider.nrepl/cider-middleware', 'cider.piggieback/wrap-cljs-repl']
70+
}
71+
----
72+
5873
== Starting a ClojureScript REPL
5974

6075
Open a ClojureScript file in your project and type kbd:[M-x]

0 commit comments

Comments
 (0)