Last active
February 23, 2021 23:43
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(require '[clojure.tools.namespace.find :as find] | |
'[clojure.java.io :as io]) | |
(defn infer-deps | |
"Given an m2 (a maven repo) and a directory of clojure source code, | |
make up some dependencies." | |
[m2 src] | |
(let [repo (io/file m2 "repository")] | |
{:deps | |
(into | |
{} | |
(map (fn [dep] | |
[(:id dep) (dissoc dep :id)])) | |
(vals | |
(apply | |
merge-with | |
(fn [a b] | |
(let [i (compare (:mvn/version a) (:mvn/version b))] | |
(if (pos? i) a b))) | |
{} | |
(for [x (find/find-ns-decls [(io/file src)]) | |
i x | |
:when (seq? i) | |
:when (or (= :require (first i))fv | |
(= :import (first i))) | |
clause (rest i) | |
resource-name (concat | |
(when (= (first i) :require) | |
(let [a-ns-name (first clause)] | |
[(str (.replaceAll (munge (name a-ns-name)) "\\." "/") | |
".clj") | |
(str (.replaceAll (munge (name a-ns-name)) "\\." "/") | |
".cljc")])) | |
(when (= (first i) :import) | |
(for [a-name (rest clause) | |
:let [package (first clause)]] | |
(str (.replaceAll (str (name package) "." (name a-name)) "\\." "/") | |
".class")))) | |
f (file-seq repo) | |
:when (.endsWith (.getName f) ".jar") | |
:when (not (.isDirectory f)) | |
:let [exists (try | |
(with-open [jf (java.util.jar.JarFile. f)] | |
(.getJarEntry jf resource-name)) | |
(catch Throwable _))] | |
:when exists | |
:let [p (.getAbsolutePath f) | |
[_ group-id artifact version _] (re-find #"repository/(.*)/(.*)/(.*)/.*jar" p)]] | |
{resource-name {:id (symbol (.replaceAll group-id "/" ".") artifact) | |
:mvn/version version}}))))})) | |
;; (infer-deps "/home/kevin/.m2/" "/home/kevin/src/reitit/modules/reitit-malli/src") | |
;; (infer-deps "/home/kevin/.m2/" "/home/kevin/src/partyline/src") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment