deps: aliases, user config merge, tools.deps conflict semantics
deps.edn :aliases now work the tools.deps way, scoped to what jolt supports (git/:local, no maven): :extra-paths and :extra-deps accumulate across selected aliases, :main-opts is last-wins. The CLI grows -A:dev:test (selects aliases for path/run/repl/-e) and -M:alias (runs the alias :main-opts through jolt). A user-level deps.edn ($JOLT_CONFIG, else $XDG_CONFIG_HOME/jolt, else ~/.jolt) merges under the project file: :deps and :aliases merge per key with the project winning, everything else replaces. Resolution is now breadth-first so a top-level coordinate always beats a transitive one for the same lib (it was DFS first-wins — a dep's pin could shadow the project's own). Conflicting coordinates for one lib warn on stderr with both coords and which won. Also fixes dedup keying: it hashed the symbol struct's string repr, which carries reader position metadata, so the same lib from two files never deduped. resolve-deps-cached keys on project edn + user edn + aliases. Tests: deps-aliases-test and deps-conflicts-test, local deps only.
This commit is contained in:
parent
f50fdad0ee
commit
add80c3018
4 changed files with 358 additions and 44 deletions
|
|
@ -42,50 +42,153 @@
|
|||
[dir edn]
|
||||
(map |(string dir "/" $) (or (and edn (get edn :paths)) ["src"])))
|
||||
|
||||
# --- user config + aliases (tools.deps-shaped, scoped to git/:local) -----------
|
||||
|
||||
(defn config-dir
|
||||
"User-level config dir: $JOLT_CONFIG, else $XDG_CONFIG_HOME/jolt, else
|
||||
~/.jolt — the same fallback chain the Clojure CLI uses for ~/.clojure."
|
||||
[]
|
||||
(or (os/getenv "JOLT_CONFIG")
|
||||
(when-let [x (os/getenv "XDG_CONFIG_HOME")]
|
||||
(when (> (length x) 0) (string x "/jolt")))
|
||||
(string (os/getenv "HOME") "/.jolt")))
|
||||
|
||||
(defn- merge-per-key [a b] # dictionary union, b's entries win
|
||||
(def out @{})
|
||||
(each m [a b] (when (dictionary? m) (eachp [k v] m (put out k v))))
|
||||
out)
|
||||
|
||||
(defn load-config
|
||||
"The project deps.edn merged over the user-level one (config-dir)/deps.edn.
|
||||
tools.deps merge semantics: scalar keys and :paths replace (project wins),
|
||||
:deps and :aliases merge per key with the project winning. Relative
|
||||
:local/root in the USER file is resolved against the cwd — prefer absolute
|
||||
paths there."
|
||||
[deps-edn-path]
|
||||
(def proj (read-edn deps-edn-path))
|
||||
(def user (read-edn (string (config-dir) "/deps.edn")))
|
||||
(cond
|
||||
(nil? user) proj
|
||||
(nil? proj) user
|
||||
(let [out (merge-per-key user proj)]
|
||||
(put out :deps (merge-per-key (get user :deps) (get proj :deps)))
|
||||
(put out :aliases (merge-per-key (get user :aliases) (get proj :aliases)))
|
||||
out)))
|
||||
|
||||
(defn combine-aliases
|
||||
"Combine the selected alias keywords against `edn`'s :aliases:
|
||||
:extra-paths and :extra-deps accumulate in order, :main-opts is last-wins
|
||||
(the tools.deps CLI rules). Unknown alias -> error."
|
||||
[edn aliases]
|
||||
(def als (or (and (dictionary? edn) (get edn :aliases)) {}))
|
||||
(def extra-paths @[])
|
||||
(def extra-deps @{})
|
||||
(var main-opts nil)
|
||||
(each a (or aliases [])
|
||||
(def spec (get als a))
|
||||
(when (nil? spec) (error (string "unknown alias: " a)))
|
||||
(each p (or (get spec :extra-paths) []) (array/push extra-paths p))
|
||||
(when (dictionary? (get spec :extra-deps))
|
||||
(eachp [lib coord] (get spec :extra-deps) (put extra-deps lib coord)))
|
||||
(when-let [mo (get spec :main-opts)]
|
||||
(set main-opts (tuple ;(map string mo)))))
|
||||
{:extra-paths extra-paths :extra-deps extra-deps :main-opts main-opts})
|
||||
|
||||
(defn alias-main-opts
|
||||
"The :main-opts the selected aliases produce (last alias with the key wins),
|
||||
or nil. Reads the merged user+project config."
|
||||
[deps-edn-path aliases]
|
||||
(get (combine-aliases (load-config deps-edn-path) aliases) :main-opts))
|
||||
|
||||
(defn resolve-deps
|
||||
"Resolve the git/:local deps of `deps-edn-path` into an ordered, de-duplicated
|
||||
array of source dirs (the project's own :paths first, then each dependency's,
|
||||
transitively). `tree` is where jpm's clone cache lives (default ./jpm_tree)."
|
||||
[deps-edn-path &opt tree]
|
||||
transitively). `tree` is where jpm's clone cache lives (default ./jpm_tree).
|
||||
`aliases` (keywords) pull :extra-paths/:extra-deps from the merged config's
|
||||
:aliases. The user-level deps.edn (see load-config) merges under the project."
|
||||
[deps-edn-path &opt tree aliases]
|
||||
(default tree (string (os/cwd) "/jpm_tree"))
|
||||
(os/mkdir tree)
|
||||
(ensure-jpm-config tree)
|
||||
(def edn (load-config deps-edn-path))
|
||||
(def extra (combine-aliases edn aliases))
|
||||
(def roots @[])
|
||||
(def seen @{})
|
||||
(def seen @{}) # lib name -> chosen coordinate (for conflict reporting)
|
||||
(defn add-root [r] (unless (index-of r roots) (array/push roots r)))
|
||||
(defn process [edn base-dir own-paths?]
|
||||
(when (dictionary? edn)
|
||||
(when own-paths? (each r (src-roots base-dir edn) (add-root r)))
|
||||
(eachp [lib spec] (or (get edn :deps) {})
|
||||
(def k (string lib))
|
||||
(unless (get seen k)
|
||||
(put seen k true)
|
||||
(def dir
|
||||
(cond
|
||||
(and (dictionary? spec) (get spec :git/url)) (clone-git spec)
|
||||
(and (dictionary? spec) (get spec :local/root))
|
||||
(let [lr (get spec :local/root)]
|
||||
(if (string/has-prefix? "/" lr) lr (string base-dir "/" lr)))
|
||||
nil)) # :mvn/* and anything else: skip
|
||||
(when dir
|
||||
(def dep-edn (read-edn (string dir "/deps.edn")))
|
||||
(each r (src-roots dir dep-edn) (add-root r))
|
||||
(process dep-edn dir false))))))
|
||||
(process (read-edn deps-edn-path) (os/cwd) true)
|
||||
# Reader symbols carry position metadata, so dedup/conflict keys must use the
|
||||
# NAME, never (string lib) — two my/b symbols from different files differ.
|
||||
(defn lib-name [lib]
|
||||
(if (and (dictionary? lib) (get lib :name))
|
||||
(if-let [ns (get lib :ns)]
|
||||
(string ns "/" (get lib :name))
|
||||
(get lib :name))
|
||||
(string lib)))
|
||||
(defn coord-str [spec]
|
||||
(cond
|
||||
(and (dictionary? spec) (get spec :local/root))
|
||||
(string ":local/root " (get spec :local/root))
|
||||
(and (dictionary? spec) (get spec :git/url))
|
||||
(string (get spec :git/url) " @ " (or (get spec :git/sha) (get spec :git/tag)))
|
||||
(string/format "%j" spec)))
|
||||
(defn coord= [a b]
|
||||
(and (deep= (get a :local/root) (get b :local/root))
|
||||
(deep= (get a :git/url) (get b :git/url))
|
||||
(deep= (get a :git/sha) (get b :git/sha))
|
||||
(deep= (get a :git/tag) (get b :git/tag))))
|
||||
(def queue @[])
|
||||
(defn discover [lib spec base-dir]
|
||||
(def k (lib-name lib))
|
||||
(if-let [prev (get seen k)]
|
||||
(unless (coord= prev spec)
|
||||
(eprintf "WARNING: %s: conflicting coordinates — using %s, ignoring %s"
|
||||
k (coord-str prev) (coord-str spec)))
|
||||
(do
|
||||
(put seen k spec)
|
||||
(def dir
|
||||
(cond
|
||||
(and (dictionary? spec) (get spec :git/url)) (clone-git spec)
|
||||
(and (dictionary? spec) (get spec :local/root))
|
||||
(let [lr (get spec :local/root)]
|
||||
(if (string/has-prefix? "/" lr) lr (string base-dir "/" lr)))
|
||||
nil)) # :mvn/* and anything else: skip
|
||||
(when dir
|
||||
(def dep-edn (read-edn (string dir "/deps.edn")))
|
||||
(each r (src-roots dir dep-edn) (add-root r))
|
||||
(array/push queue [dep-edn dir])))))
|
||||
# the project's own paths (+ alias extra paths) lead the roots
|
||||
(each r (src-roots (os/cwd) edn) (add-root r))
|
||||
(each pp (extra :extra-paths) (add-root (string (os/cwd) "/" pp)))
|
||||
# breadth-first: every top-level dep (incl. alias :extra-deps) registers
|
||||
# before any transitive dep — so a top-level coordinate always wins,
|
||||
# matching tools.deps
|
||||
(eachp [lib spec] (or (and (dictionary? edn) (get edn :deps)) {})
|
||||
(discover lib spec (os/cwd)))
|
||||
(eachp [lib spec] (extra :extra-deps)
|
||||
(discover lib spec (os/cwd)))
|
||||
(while (> (length queue) 0)
|
||||
(def [dep-edn dir] (get queue 0))
|
||||
(array/remove queue 0)
|
||||
(when (dictionary? dep-edn)
|
||||
(eachp [lib spec] (or (get dep-edn :deps) {})
|
||||
(discover lib spec dir))))
|
||||
roots)
|
||||
|
||||
(defn resolve-deps-cached
|
||||
"Like resolve-deps, but caches the resolved roots in the tree keyed on a hash
|
||||
of the deps.edn, so an unchanged deps.edn resolves without re-fetching."
|
||||
[deps-edn-path &opt tree]
|
||||
of the project deps.edn + the user deps.edn + the selected aliases, so an
|
||||
unchanged config resolves without re-fetching."
|
||||
[deps-edn-path &opt tree aliases]
|
||||
(default tree (string (os/cwd) "/jpm_tree"))
|
||||
(when (os/stat deps-edn-path)
|
||||
(os/mkdir tree)
|
||||
(def cache-file (string tree "/.jolt-deps-roots.jdn"))
|
||||
(def h (hash (slurp deps-edn-path)))
|
||||
(def user-path (string (config-dir) "/deps.edn"))
|
||||
(def h (hash [(slurp deps-edn-path)
|
||||
(when (os/stat user-path) (slurp user-path))
|
||||
(string/format "%j" (map string (or aliases [])))]))
|
||||
(def cached (when (os/stat cache-file) (try (parse (slurp cache-file)) ([_] nil))))
|
||||
(if (and cached (= h (get cached :hash)))
|
||||
(get cached :roots)
|
||||
(let [roots (resolve-deps deps-edn-path tree)]
|
||||
(let [roots (resolve-deps deps-edn-path tree aliases)]
|
||||
(spit cache-file (string/format "%j" {:hash h :roots roots}))
|
||||
roots))))
|
||||
|
|
|
|||
|
|
@ -5,40 +5,63 @@
|
|||
# api/init). This tool does the resolution (git + :local deps, via jpm's fetch
|
||||
# cache) and either prints the roots or launches jolt with JOLT_PATH set.
|
||||
#
|
||||
# jolt-deps path print the resolved roots (':'-joined), e.g. for
|
||||
# JOLT_PATH=$(jolt-deps path) jolt file.clj
|
||||
# jolt-deps run FILE [args] resolve, then run `jolt FILE args` with JOLT_PATH set
|
||||
# jolt-deps repl resolve, then start a jolt REPL with JOLT_PATH set
|
||||
# jolt-deps -e EXPR [args] resolve, then `jolt -e EXPR ...` with JOLT_PATH set
|
||||
# jolt-deps uberscript OUT -m NS
|
||||
# resolve, then bundle NS + its deps into one .clj
|
||||
# jolt-deps [-A:a:b] path print the resolved roots (':'-joined), e.g.
|
||||
# JOLT_PATH=$(jolt-deps path) jolt file.clj
|
||||
# jolt-deps [-A:a:b] run FILE [args]
|
||||
# resolve, then `jolt FILE args` with JOLT_PATH set
|
||||
# jolt-deps [-A:a:b] repl resolve, then a jolt REPL with JOLT_PATH set
|
||||
# jolt-deps [-A:a:b] -e EXPR resolve, then `jolt -e EXPR ...`
|
||||
# jolt-deps -M:a[:b] [args] resolve with the aliases, then run jolt with
|
||||
# the last alias's :main-opts ++ args
|
||||
# jolt-deps uberscript OUT -m NS resolve, then bundle NS + deps into one .clj
|
||||
#
|
||||
# -A:dev:test selects aliases (tools.deps style): their :extra-paths and
|
||||
# :extra-deps join the resolution. A user-level deps.edn ($JOLT_CONFIG, else
|
||||
# $XDG_CONFIG_HOME/jolt, else ~/.jolt) merges under the project's.
|
||||
# The jolt binary is found via $JOLT_BIN, else `jolt` on PATH.
|
||||
|
||||
(import ./deps :as deps)
|
||||
|
||||
(defn- roots []
|
||||
(if (os/stat "deps.edn") (deps/resolve-deps-cached "deps.edn") @[]))
|
||||
(defn- parse-alias-flag
|
||||
"\"-A:dev:test\" -> [:dev :test] (also accepts -M:...)."
|
||||
[arg]
|
||||
(map keyword (filter |(not= "" $) (string/split ":" (string/slice arg 2)))))
|
||||
|
||||
(defn- exec-jolt [extra-args]
|
||||
(defn- roots [aliases]
|
||||
(if (os/stat "deps.edn") (deps/resolve-deps-cached "deps.edn" nil aliases) @[]))
|
||||
|
||||
(defn- exec-jolt [aliases extra-args]
|
||||
# Set JOLT_PATH in our own env and let the child inherit it (os/execute's env
|
||||
# arg isn't honored here; inheriting is reliable).
|
||||
(def rs (string/join (roots) ":"))
|
||||
(def rs (string/join (roots aliases) ":"))
|
||||
(def existing (os/getenv "JOLT_PATH"))
|
||||
(os/setenv "JOLT_PATH" (if (and existing (> (length existing) 0)) (string rs ":" existing) rs))
|
||||
(os/execute [(os/getenv "JOLT_BIN" "jolt") ;extra-args] :p))
|
||||
|
||||
(defn- usage []
|
||||
(print "usage: jolt-deps [path | run FILE [args] | repl | -e EXPR [args]]"))
|
||||
(print "usage: jolt-deps [-A:alias[:alias]] [path | run FILE [args] | repl | -e EXPR [args]]")
|
||||
(print " jolt-deps -M:alias[:alias] [args] (runs the alias :main-opts)")
|
||||
(print " jolt-deps uberscript OUT -m NS"))
|
||||
|
||||
(defn main [&]
|
||||
(def argv (tuple/slice (or (dyn :args) @[]) 1))
|
||||
(var argv (tuple/slice (or (dyn :args) @[]) 1))
|
||||
(var aliases nil)
|
||||
# leading -A:... selects aliases for whatever command follows
|
||||
(while (string/has-prefix? "-A" (or (get argv 0) ""))
|
||||
(set aliases (array/concat (or aliases @[]) (parse-alias-flag (get argv 0))))
|
||||
(set argv (tuple/slice argv 1)))
|
||||
(def cmd (get argv 0))
|
||||
(cond
|
||||
(or (nil? cmd) (= cmd "help") (= cmd "-h") (= cmd "--help")) (usage)
|
||||
(= cmd "path") (print (string/join (roots) ":"))
|
||||
(= cmd "run") (os/exit (exec-jolt (tuple/slice argv 1)))
|
||||
(= cmd "repl") (os/exit (exec-jolt []))
|
||||
(= cmd "-e") (os/exit (exec-jolt argv))
|
||||
(= cmd "uberscript") (os/exit (exec-jolt argv))
|
||||
(string/has-prefix? "-M" cmd)
|
||||
(let [als (array/concat (or aliases @[]) (parse-alias-flag cmd))
|
||||
mo (or (deps/alias-main-opts "deps.edn" als)
|
||||
(do (eprint "jolt-deps: no :main-opts in aliases " (string/format "%j" (map string als)))
|
||||
(os/exit 1)))]
|
||||
(os/exit (exec-jolt als [;mo ;(tuple/slice argv 1)])))
|
||||
(= cmd "path") (print (string/join (roots aliases) ":"))
|
||||
(= cmd "run") (os/exit (exec-jolt aliases (tuple/slice argv 1)))
|
||||
(= cmd "repl") (os/exit (exec-jolt aliases []))
|
||||
(= cmd "-e") (os/exit (exec-jolt aliases argv))
|
||||
(= cmd "uberscript") (os/exit (exec-jolt aliases argv))
|
||||
(do (eprint "jolt-deps: unknown command " cmd) (usage) (os/exit 1))))
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue