Skip to content

Commit

Permalink
Refactored tool classpath calculation and align on create-basis
Browse files Browse the repository at this point in the history
  • Loading branch information
puredanger committed Dec 31, 2024
1 parent d1a84a9 commit 1540d7f
Show file tree
Hide file tree
Showing 5 changed files with 141 additions and 174 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ Changelog
*Also see [Tools and installer changelog](https://github.com/clojure/brew-install/blob/1.12.0/CHANGELOG.md)*

* next
* Refactored tool classpath calculation and align on create-basis
* Update to tools.gitlibs 2.6.206
* Update other dep versions to latest
* 0.21.1460 on Nov 21, 2024
Expand Down
28 changes: 14 additions & 14 deletions src/main/clojure/clojure/tools/deps.clj
Original file line number Diff line number Diff line change
Expand Up @@ -630,8 +630,9 @@
(-> (make-classpath-map {:paths paths} lib-map classpath-args) :classpath-roots join-classpath))

(defn tool
"Transform project edn for tool by applying tool args (keys = :paths, :deps) and
returning an updated project edn."
"Replaces project-edn :deps and :paths with tool :deps and :paths
and returns a new project-edn map.
(Also supports :replace-deps and :replace-paths as aliases.)"
[project-edn tool-args]
(let [{:keys [replace-deps replace-paths deps paths]} tool-args]
(cond-> project-edn
Expand Down Expand Up @@ -704,8 +705,7 @@
(dir/with-dir root-dir
(let [basis (create-basis
{:project :standard ;; deps.edn at root
:extra {:aliases {:deps/TOOL {:replace-deps {} :replace-paths ["."]}}}
:aliases [:deps/TOOL alias]})
:args {:replace-deps {} :replace-paths ["."]}})
cp (join-classpath (:classpath-roots basis))
qual-f (qualify-fn f (get-in basis [:aliases alias]))
exit (exec-prep! root-dir cp qual-f exec-args)]
Expand Down Expand Up @@ -795,8 +795,9 @@
Each dep source value can be :standard, a string path, a deps edn map, or nil.
Sources are merged in the order - :root, :user, :project, :extra.
Aliases refer to argmaps in the merged deps that will be supplied to the basis
subprocesses (tool, resolve-deps, make-classpath-map).
When resolving deps and forming the classpath, different steps need various
args. These are pulled from the 'argmap' which is a merged set of args
selected by the aliases and optionally a map of args.
Options:
:dir - directory root path, defaults to current directory
Expand All @@ -805,6 +806,7 @@
:project - dep source, default = :standard (\"deps.edn\")
:extra - dep source, default = nil
:aliases - coll of aliases, default = nil
:args - extra map of argmap args, merged after aliases
The following subprocess argmap args can be provided:
Key Subproc Description
Expand All @@ -822,13 +824,9 @@
:libs - lib map, per resolve-deps
:classpath - classpath map per make-classpath-map
:classpath-roots - vector of paths in classpath order"
[{:keys [dir root user project extra aliases] :as params}]
[{:keys [dir root user project extra aliases args] :as params}]
(dir/with-dir (jio/file (or dir "."))
(let [basis-config (cond-> nil
(contains? params :root) (assoc :root root)
(contains? params :project) (assoc :project project)
(contains? params :user) (assoc :user user)
(contains? params :extra) (assoc :extra extra)
(let [basis-config (cond-> (select-keys params [:root :project :user :extra :args])
(seq aliases) (assoc :aliases (vec aliases)))

{root-edn :root user-edn :user project-edn :project extra-edn :extra} (create-edn-maps params)
Expand All @@ -841,11 +839,13 @@
argmap-data (->> aliases
(remove nil?)
(map #(get alias-data %)))
argmap (apply merge-alias-maps argmap-data)
argmap (apply merge-alias-maps (conj argmap-data args))

project-tooled-edn (tool project-edn argmap)
merged-edn (merge-edns [root-edn user-edn project-tooled-edn extra-edn])
basis (calc-basis merged-edn {:resolve-args argmap, :classpath-args argmap})]
basis (if (:skip-cp argmap) ;; UNSUPPORTED, USE AT YOUR OWN RISK
(assoc merged-edn :argmap argmap)
(calc-basis merged-edn {:resolve-args argmap, :classpath-args argmap}))]
(cond-> (assoc basis :basis-config basis-config)
(pos? (count argmap)) (assoc :argmap argmap)))))

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
clojure.tools.deps.script.generate-manifest2
(:require [clojure.java.io :as jio]
[clojure.tools.cli :as cli]
[clojure.tools.deps :as deps]
[clojure.tools.deps.gen.pom :as pom]
[clojure.tools.deps.script.parse :as parse]
[clojure.tools.deps.script.make-classpath2 :as makecp]
Expand All @@ -36,20 +35,15 @@
--config-data={...} - deps.edn as data
--gen TYPE - manifest type to generate (currently only pom)
Options:
-Raliases - concated resolve-deps alias names, applied to the :deps
-Aaliases - concatenated generic alias names"
Options (see make-classpath2)"
[& args]
(let [{:keys [options errors]} (cli/parse-opts args opts)]
(when (seq errors)
(run! println errors)
(System/exit 1))
(let [{:keys [gen config-user config-project]} options]
(try
(let [basis-map (makecp/run-core (merge options
{:install-deps (deps/root-deps)
:user-deps (makecp/read-deps config-user)
:project-deps (makecp/read-deps config-project)}))
(let [basis-map (makecp/run-core options)
mod-map (:basis basis-map)
;; treat all transitive deps as top-level deps
updated-deps (reduce-kv (fn [m lib {:keys [dependents] :as coord}]
Expand Down
88 changes: 33 additions & 55 deletions src/main/clojure/clojure/tools/deps/script/make_classpath2.clj
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
(:require
[clojure.java.io :as jio]
[clojure.pprint :as pprint]
[clojure.string :as str]
[clojure.tools.cli :as cli]
[clojure.tools.deps :as deps]
[clojure.tools.deps.extensions :as ext]
Expand All @@ -22,9 +21,12 @@
(:import
[clojure.lang IExceptionInfo]))

(defn blank-to-nil [s]
(if (zero? (count s)) nil s))

(def ^:private opts
[;; deps.edn inputs
[nil "--config-user PATH" "User deps.edn location"]
[nil "--config-user PATH" "User deps.edn location" :parse-fn blank-to-nil]
[nil "--config-project PATH" "Project deps.edn location"]
[nil "--config-data EDN" "Final deps.edn data to treat as the last deps.edn file" :parse-fn parse/parse-config]
;; tool args to resolve
Expand Down Expand Up @@ -61,61 +63,45 @@
(defn resolve-tool-args
"Resolves the tool by name to the coord + usage data.
Returns the proper alias args as if the tool was specified as an alias."
[tool-name config]
[tool-name]
(if-let [{:keys [lib coord]} (tool/resolve-tool tool-name)]
(let [manifest-type (ext/manifest-type lib coord config)
(let [config nil ;; for now, tools are only git or local which have none
manifest-type (ext/manifest-type lib coord config)
coord' (merge coord manifest-type)
{:keys [ns-default ns-aliases]} (ext/coord-usage lib coord' (:deps/manifest coord') config)]
{:replace-deps {lib coord'}
:replace-paths ["."]
:ns-default ns-default
:ns-aliases ns-aliases})
(throw (ex-info (str "Unknown tool: " tool-name) {:tool tool-name}))))

(defn run-core
"Run make-classpath script from/to data (no file stuff). Returns:
{;; Outputs:
"Run make-classpath script from/to data. Returns map w/keys:
:basis ;; the basis, including classpath roots
:trace ;; if requested, trace.edn file
:manifests ;; manifest files used in making classpath
}"
[{:keys [install-deps user-deps project-deps config-data ;; all deps.edn maps
tool-mode tool-name tool-resolver ;; -T options
:manifests ;; manifest files used in making classpath"
[{:keys [config-user config-project config-data tool-data
main-aliases exec-aliases repl-aliases tool-aliases
skip-cp threads trace tree] :as _opts}]
(when (and main-aliases exec-aliases)
(throw (ex-info "-M and -X cannot be used at the same time" {})))
(let [pretool-edn (deps/merge-edns [install-deps user-deps project-deps config-data])
;; tool use - :deps/:paths/:replace-deps/:replace-paths in project if needed
tool-args (cond
tool-name (tool-resolver tool-name pretool-edn)
tool-mode {:replace-deps {} :replace-paths ["."]})
tool-edn (when tool-args {:aliases {:deps/TOOL tool-args}})
;; :deps/TOOL is a synthetic deps.edn combining the tool definition and usage
;; it is injected at the end of the deps chain and added as a pseudo alias
;; the effects are seen in the basis but this pseduo alias should not escape
combined-tool-args (deps/combine-aliases
(deps/merge-edns [pretool-edn tool-edn])
(concat main-aliases exec-aliases repl-aliases tool-aliases (when tool-edn [:deps/TOOL])))
project-deps (deps/tool project-deps combined-tool-args)

;; calc basis
merge-edn (deps/merge-edns [install-deps user-deps project-deps config-data (when tool-edn tool-edn)]) ;; recalc to get updated project-deps
combined-exec-aliases (concat main-aliases exec-aliases repl-aliases tool-aliases (when tool-edn [:deps/TOOL]))
_ (check-aliases merge-edn combined-exec-aliases)
argmap (deps/combine-aliases merge-edn combined-exec-aliases)
resolve-args (cond-> argmap
threads (assoc :threads (Long/parseLong threads))
trace (assoc :trace trace)
tree (assoc :trace true))
basis (cond-> nil
(not skip-cp) (merge
(deps/calc-basis merge-edn {:resolve-args resolve-args, :classpath-args argmap})
{:basis-config (cond-> {} ;; :root and :project are always :standard
(nil? user-deps) (assoc :user nil) ;; -Srepro => :user nil
config-data (assoc :extra config-data) ;; -Sdeps => :extra ...
(seq combined-exec-aliases) (assoc :aliases (vec combined-exec-aliases)))})
(pos? (count argmap)) (assoc :argmap argmap))
(let [combined-exec-aliases (concat main-aliases exec-aliases repl-aliases tool-aliases)
args (cond-> nil
threads (assoc :threads (Long/parseLong threads))
trace (assoc :trace trace)
tree (assoc :trace true)
skip-cp (assoc :skip-cp true)
(or tool-data tool-aliases) (assoc :replace-paths ["."] :replace-deps {})
tool-data (merge tool-data))
basis (deps/create-basis
{:user config-user
:project config-project
:extra config-data
:aliases combined-exec-aliases
:args args})

;; check for unused aliases and warn
_ (check-aliases basis combined-exec-aliases)

libs (:libs basis)
trace (-> libs meta :trace)

Expand All @@ -129,19 +115,12 @@
(ext/manifest-file lib coord (:deps/manifest mf) basis)))
(remove nil?)
seq)]
(when (and (-> argmap :main-opts seq) repl-aliases)
(when (and (-> (:argmap basis) :main-opts seq) repl-aliases)
(io/printerrln "WARNING: Use of :main-opts with -A is deprecated. Use -M instead."))
(cond-> {:basis basis}
trace (assoc :trace trace)
manifests (assoc :manifests manifests))))

(defn read-deps
[name]
(when (not (str/blank? name))
(let [f (jio/file name)]
(when (.exists f)
(deps/slurp-deps f)))))

(defn write-lines
[lines file]
(if lines
Expand All @@ -152,11 +131,10 @@

(defn run
"Run make-classpath script. See -main for details."
[{:keys [config-user config-project cp-file jvm-file main-file basis-file manifest-file skip-cp trace tree] :as opts}]
(let [opts' (merge opts {:install-deps (deps/root-deps)
:user-deps (read-deps config-user)
:project-deps (read-deps config-project)
:tool-resolver resolve-tool-args})
[{:keys [cp-file jvm-file main-file basis-file manifest-file skip-cp tool-mode tool-name tool-aliases trace tree] :as opts}]
(let [opts' (cond-> opts
(and tool-mode (not tool-aliases))
(assoc :tool-data (when tool-name (resolve-tool-args tool-name))))
{:keys [basis manifests], trace-log :trace} (run-core opts')
{:keys [argmap libs classpath-roots]} basis
{:keys [jvm-opts main-opts]} argmap]
Expand Down
Loading

0 comments on commit 1540d7f

Please sign in to comment.