From 020d42a94fab2a2f80abd15fa05b19b355b1b545 Mon Sep 17 00:00:00 2001 From: Michiel Borkent Date: Sat, 24 Sep 2022 13:25:06 +0200 Subject: [PATCH] Fix #808: -Sdeps --- CHANGELOG.md | 1 + src/babashka/main.clj | 21 +++++++++++++++------ test/babashka/bb_edn_test.clj | 6 ++++++ 3 files changed, 22 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c2408f75..0396b8d0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,7 @@ A preview of the next release can be installed from ## Unreleased +- [#808](https://github.com/babashka/babashka/issues/808): support `-Sdeps` option to support passing extra deps map which will be merged last - [#1336](https://github.com/babashka/babashka/issues/1336): tasks subcommand doesn't work with global --force option ([@bobisageek](https://github.com/bobisageek)) - [#1340](https://github.com/babashka/babashka/issues/1340): `defprotocol` are methods missing `:doc` metadata ([@bobisageek](https://github.com/bobisageek)) - [#1368](https://github.com/babashka/babashka/issues/1368): `-x`: do not pick up on aliases in `user` ns diff --git a/src/babashka/main.clj b/src/babashka/main.clj index 784c5473..e5bd9b4b 100644 --- a/src/babashka/main.clj +++ b/src/babashka/main.clj @@ -145,10 +145,11 @@ Global opts: -cp, --classpath Classpath to use. Overrides bb.edn classpath. --debug Print debug information and internal stacktrace in case of exception. - --force Passes -Sforce to deps.clj, forcing recalculation of the classpath. --init Load file after any preloads and prior to evaluation/subcommands. --config Replacing bb.edn with file. Relative paths are resolved relative to file. --deps-root Treat dir as root of relative paths in config. + -Sforce Force recalculation of the classpath (don't use the cache) + -Sdeps Deps data to use as the last deps file to be merged Help: @@ -696,9 +697,12 @@ Use bb run --help to show this help output. ("--init") (recur (nnext options) (assoc opts-map :init (second options))) - ("--force") + ("--force" "-Sforce") (recur (next options) (assoc opts-map :force? true)) + ("-Sdeps") + (recur (nnext options) (assoc opts-map :merge-deps (second options))) + ("--config") (recur (nnext options) (assoc opts-map :config (second options))) @@ -1052,15 +1056,19 @@ Use bb run --help to show this help output. {:keys [:jar] :as file-opt} (when (some-> args first io/file .isFile) (parse-file-opt args global-opts)) config (:config global-opts) + merge-deps (:merge-deps global-opts) abs-path #(-> % io/file .getAbsolutePath) bb-edn-file (cond config (when (fs/exists? config) (abs-path config)) jar (some-> jar cp/loader (cp/resource "META-INF/bb.edn") .toString) :else (when (fs/exists? "bb.edn") (abs-path "bb.edn"))) - bb-edn (when bb-edn-file - (System/setProperty "babashka.config" bb-edn-file) - (let [raw-string (slurp bb-edn-file) - edn (load-bb-edn raw-string) + bb-edn (when (or bb-edn-file merge-deps) + (when bb-edn-file (System/setProperty "babashka.config" bb-edn-file)) + (let [raw-string (when bb-edn-file (slurp bb-edn-file)) + edn (when bb-edn-file (load-bb-edn raw-string)) + edn (if merge-deps + (deps/merge-deps [edn (load-bb-edn merge-deps)]) + edn) edn (assoc edn :raw raw-string :file bb-edn-file) @@ -1069,6 +1077,7 @@ Use bb run --help to show this help output. (assoc edn :deps-root deps-root) edn)] (vreset! common/bb-edn edn))) + ;; _ (.println System/err (str bb-edn)) min-bb-version (:min-bb-version bb-edn)] (when min-bb-version (when-not (satisfies-min-version? min-bb-version) diff --git a/test/babashka/bb_edn_test.clj b/test/babashka/bb_edn_test.clj index 589d72b7..0422fe2b 100644 --- a/test/babashka/bb_edn_test.clj +++ b/test/babashka/bb_edn_test.clj @@ -451,3 +451,9 @@ even more stuff here\" "{:deps {} :aliases {:foo {:env-vars {:dude #env \"DUDE\"}}}}" (is (= 6 (bb "-e" "(+ 1 2 3)"))))) + +(deftest merge-deps-test + (test-utils/with-config + "{:deps {}}" + (is (= {1 {:a 1}} + (bb "-Sdeps" "{:deps {medley/medley {:mvn/version \"1.4.0\"}}}" "-e" "(require 'medley.core) (medley.core/index-by :a [{:a 1}])")))))