Skip to content

Commit

Permalink
Improve deps handling
Browse files Browse the repository at this point in the history
  • Loading branch information
plexus committed Jan 19, 2024
1 parent bc39c2f commit 07ecd90
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 57 deletions.
7 changes: 6 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,15 @@

## Added

- Inject shadow-cljs deps when needed and not already present

## Fixed

## Changed

- Check deps before injecting extra deps (cider, nrepl, shadow, etc). Declared
deps versions always get precedence.

# 0.22.110-alpha (2024-01-17 / 95c22dc)

## Added
Expand Down Expand Up @@ -169,4 +174,4 @@ Initial release
- lambdaisland.classpath integration
- Support for cider-nrepl, refactor-nrepl
- Basic support for shadow-cljs cljs nREPL-base REPL
- Auto-connect for Emacs
- Auto-connect for Emacs
3 changes: 2 additions & 1 deletion bb.edn
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{:deps
{:paths ["src" "resources"]
:deps
{lambdaisland/open-source {:git/url "https://github.com/lambdaisland/open-source"
:git/sha "7ce125cbd14888590742da7ab3b6be9bba46fc7a"}
com.lambdaisland/launchpad {:local/root "."}}}
21 changes: 13 additions & 8 deletions deps.edn
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,22 @@
com.lambdaisland/dotenv {:mvn/version "0.2.5"}}

:aliases
{:clojure { :extra-deps {babashka/babashka {:mvn/version "1.3.187" :scope "provided"}
org.clojure/tools.deps.alpha {:mvn/version "0.15.1254"}
com.nextjournal/beholder {:mvn/version "1.0.2"}
thheller/shadow-cljs {:mvn/version "2.26.3"}
;; prevent tools.deps.alpha from dragging in an old guava
com.google.guava/guava {:mvn/version "33.0.0-jre"}
com.lambdaisland/classpath {:mvn/version "0.5.48"}}}
{:clojure
{:extra-deps {babashka/babashka {:mvn/version "1.3.188" :scope "provided"}
org.clojure/tools.deps.alpha {:mvn/version "0.15.1254"}
com.nextjournal/beholder {:mvn/version "1.0.2"}
thheller/shadow-cljs {:mvn/version "2.26.5"}
;; prevent tools.deps.alpha from dragging in an old guava
com.google.guava/guava {:mvn/version "33.0.0-jre"}
com.lambdaisland/classpath {:mvn/version "0.5.48"}}}
:dev
{:extra-paths ["dev"]
:extra-deps {djblue/portal {:mvn/version "RELEASE"}}}

:test
{:extra-paths ["test"]
:extra-deps {lambdaisland/kaocha {:mvn/version "1.87.1366"}}}}}
:extra-deps {lambdaisland/kaocha {:mvn/version "1.87.1366"}}}

:self-deps
{:extra-deps {com.lambdaisland/launchpad-deps {:local/root "resources/launchpad"}}}
}}
12 changes: 12 additions & 0 deletions resources/launchpad/deps.edn
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
;; This file is just a lookup file for Launchpad to find the version of various
;; things to use. We keep it in deps.edn format so antq can automatically
;; upgrade versions.
{:deps
{com.lambdaisland/launchpad {:mvn/version "0.22.110-alpha"}
com.lambdaisland/classpath {:mvn/version "0.5.48"}
com.github.jnr/jnr-posix {:mvn/version "3.1.18"}
thheller/shadow-cljs {:mvn/version "2.26.5"}
com.google.guava/guava {:mvn/version "33.0.0-jre"}
cider/cider-nrepl {:mvn/version "0.45.0"}
nrepl/nrepl {:mvn/version "1.1.0"}
refactor-nrepl/refactor-nrepl {:mvn/version "3.9.1"}}}
104 changes: 57 additions & 47 deletions src/lambdaisland/launchpad.clj
Original file line number Diff line number Diff line change
Expand Up @@ -28,22 +28,14 @@
[nil "--emacs" "Shorthand for --cider-nrepl --refactor-nrepl --cider-connect"]
[nil "--go" "Call (user/go) on boot"]])

(def default-nrepl-version "1.0.0")
(def library-versions
(:deps (edn/read-string (slurp (io/resource "launchpad/deps.edn")))))

;; Unless we have a mechanism of automatically updating these I would use
;; `RELEASE` here, so non-emacs user always default to the latest version. This
;; is a good candidate for making this configurable, for explicitness.
(def default-cider-version "RELEASE")
(def default-refactor-nrepl-version "RELEASE")

(def classpath-coords {:mvn/version "0.5.48"})
(def jnr-posix-coords {:mvn/version "3.1.18"})

(def default-launchpad-coords
"Version coordinates for Launchpad, which we use to inject ourselves into the
project dependencies for runtime support. Only used when we are unable to find
the current version in `bb.edn`"
{:mvn/version "RELEASE"})
;; (def default-launchpad-coords
;; "Version coordinates for Launchpad, which we use to inject ourselves into the
;; project dependencies for runtime support. Only used when we are unable to find
;; the current version in `bb.edn`"
;; {:mvn/version "RELEASE"})

(def verbose? (some #{"-v" "--verbose"} *command-line-args*))

Expand Down Expand Up @@ -107,22 +99,24 @@
;; even though they are not running Emacs.
)))

(defn emacs-cider-version
(defn emacs-cider-coords
"Find the CIDER version that is currently in use by the running Emacs instance."
[]
(when (emacs-require 'cider)
(read-string
(eval-emacs '(if (boundp 'cider-required-middleware-version)
cider-required-middleware-version
(upcase cider-version))))))
{:mvn/version
(read-string
(eval-emacs '(if (boundp 'cider-required-middleware-version)
cider-required-middleware-version
(upcase cider-version))))}))

(defn emacs-refactor-nrepl-version
(defn emacs-refactor-nrepl-coords
"Find the refactor-nrepl version that is required by the `clj-refactor` version
installed in Emacs."
[]
(when (emacs-require 'clj-refactor)
(read-string
(eval-emacs 'cljr-injected-middleware-version))))
{:mvn/version
(read-string
(eval-emacs 'cljr-injected-middleware-version))}))

(defn add-nrepl-middleware [& mws]
(fn [ctx]
Expand All @@ -137,15 +131,31 @@
(:refactor-nrepl options)
((add-nrepl-middleware 'refactor-nrepl.middleware/wrap-refactor))))

(defn library-in-deps? [ctx libname]
(or (contains? (get-in ctx [:deps-edn :deps]) libname)
(some
(fn [extra-deps]
(contains? extra-deps libname))
(map (comp :extra-deps val)
(select-keys (get-in ctx [:deps-edn :aliases])
(:aliases ctx))))))

(defn assoc-extra-dep [ctx libname & [version]]
(if (library-in-deps? ctx libname)
ctx
(update ctx :extra-deps
assoc libname
(or version
(get library-versions libname)))))

(defn compute-extra-deps [{:keys [options] :as ctx}]
(let [assoc-dep #(update %1 :extra-deps assoc %2 %3)]
(cond-> ctx
true
(assoc-dep 'nrepl/nrepl {:mvn/version default-nrepl-version})
(:cider-nrepl options)
(assoc-dep 'cider/cider-nrepl {:mvn/version (or (emacs-cider-version) default-cider-version)})
(:refactor-nrepl options)
(assoc-dep 'refactor-nrepl/refactor-nrepl {:mvn/version (or (emacs-refactor-nrepl-version) default-refactor-nrepl-version)}))))
(cond-> ctx
true
(assoc-extra-dep 'nrepl/nrepl)
(:cider-nrepl options)
(assoc-extra-dep 'cider/cider-nrepl (emacs-cider-coords))
(:refactor-nrepl options)
(assoc-extra-dep 'refactor-nrepl/refactor-nrepl (emacs-refactor-nrepl-coords))))

(defn get-nrepl-port [ctx]
(assoc ctx :nrepl-port (or (get-in ctx [:options :nrepl-port])
Expand Down Expand Up @@ -290,7 +300,7 @@

(defn include-hot-reload-deps [{:keys [extra-deps aliases] :as ctx}]
(as-> ctx <>
(update <> :extra-deps assoc 'com.lambdaisland/classpath classpath-coords)
(assoc-extra-dep <> 'com.lambdaisland/classpath)
(update <> :requires conj 'lambdaisland.launchpad.deps)
(update <> :eval-forms (fnil conj [])
`(lambdaisland.launchpad.deps/write-cpcache-file))
Expand All @@ -310,7 +320,7 @@

(defn watch-dotenv [ctx]
(-> ctx
(update :extra-deps assoc 'com.github.jnr/jnr-posix jnr-posix-coords)
(assoc-extra-dep 'com.github.jnr/jnr-posix)
(update :java-args conj
"--add-opens=java.base/java.lang=ALL-UNNAMED"
"--add-opens=java.base/java.util=ALL-UNNAMED")
Expand Down Expand Up @@ -338,6 +348,9 @@
(debug "Starting shadow-cljs builds" build-ids))
(if (seq build-ids)
(-> ctx
(assoc-extra-dep 'thheller/shadow-cljs)
;; tools.deps pulls in an old Guava, which causes issues with shadow-cljs
(assoc-extra-dep 'com.google.guava/guava)
(update :middleware (fnil conj []) 'shadow.cljs.devtools.server.nrepl/middleware)
(assoc :shadow-cljs/build-ids build-ids)
(assoc :shadow-cljs/connect-ids connect-ids)
Expand All @@ -350,13 +363,11 @@
ctx)))

(defn find-launchpad-coords []
(or
(when (.exists (io/file "bb.edn"))
(get-in (edn/read-string (slurp "bb.edn")) [:deps 'com.lambdaisland/launchpad]))
default-launchpad-coords))
(when (.exists (io/file "bb.edn"))
(get-in (edn/read-string (slurp "bb.edn")) [:deps 'com.lambdaisland/launchpad])))

(defn include-launchpad-deps [{:keys [extra-deps] :as ctx}]
(update ctx :extra-deps assoc 'com.lambdaisland/launchpad (find-launchpad-coords)))
(defn include-launchpad-deps [ctx]
(assoc-extra-dep ctx 'com.lambdaisland/launchpad (find-launchpad-coords)))

(defn maybe-connect-emacs [{:keys [options nrepl-port project-root] :as ctx}]
(when (:cider-connect options)
Expand Down Expand Up @@ -511,11 +522,10 @@
(defn process-steps [ctx steps]
(reduce #(%2 %1) ctx steps))

(defn main
([{:keys [steps] :or {steps default-steps} :as opts}]
(let [ctx (process-steps (initial-context opts) steps)
processes (:processes ctx)]
(.addShutdownHook (Runtime/getRuntime)
(Thread. (fn [] (run! #(.destroy %) processes))))
(System/exit (apply min (for [p processes]
(.waitFor p)))))))
(defn main [{:keys [steps] :or {steps default-steps} :as opts}]
(let [ctx (process-steps (initial-context opts) steps)
processes (:processes ctx)]
(.addShutdownHook (Runtime/getRuntime)
(Thread. (fn [] (run! #(.destroy %) processes))))
(System/exit (apply min (for [p processes]
(.waitFor p))))))

0 comments on commit 07ecd90

Please sign in to comment.