Permalink
Switch branches/tags
Nothing to show
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
62 lines (54 sloc) 2.32 KB
(ns nixtract
(:require [clojure.java.io :as io]
[clojure.data.json :as json]
[clojure.string :as str]
[clojure.tools.deps.alpha :as deps])
(:import [java.io BufferedInputStream]
[java.nio.file Paths]
))
;; CLJ_CONFIG=. CLJ_CACHE=. nix-shell -p clojure jre --run "clojure -R:build -C:build -Srepro -Sforce -m nixtract generated/nix-deps.json"
(defn sha256-stream [stream]
(let [buffer (byte-array 8192)
digest (java.security.MessageDigest/getInstance "SHA-256")
bstream (BufferedInputStream. stream)]
(loop []
(let [count (.read bstream buffer)]
(when (> count 0)
(.update digest buffer 0 count)
(recur))))
(apply str (map #(format "%02x" %)
(.digest digest)))))
(def sha256-file (comp sha256-stream io/input-stream))
(defn ensure-trailing-slash [s]
(if (str/ends-with? s "/") s (str s "/")))
(defn relativize [pathname basename]
(let [empty-array (into-array String []) ;; need this for weird java varargs thing
path (Paths/get pathname empty-array)
base (Paths/get basename empty-array)]
(.toFile (.relativize base path))))
(defn nix-libs []
(let [deps (read-string (slurp "deps.edn"))
_ (println (:build (:aliases deps)))
libs (deps/resolve-deps deps (:build (:aliases deps)))
maven-local-repo (or (:mvn/local-repo deps)
clojure.tools.deps.alpha.util.maven/default-local-repo)
artifacts
(reduce-kv (fn [a k v]
(let [p (first (:paths v))]
(conj a
{:coordinates [k (:mvn/version v)]
:relativePathname (.toString (relativize p maven-local-repo))
:sha256 (sha256-file p)})))
[]
libs)]
{:0NOTICE "Automatically generated by nixtract.clj, do not edit"
:artifacts artifacts
:repositories (reduce-kv (fn [a k {:keys [url]}]
(conj a (ensure-trailing-slash url)))
[]
(:mvn/repos deps)
)}))
(defn -main [json-out & args]
(let [file-out (io/writer json-out)]
(json/write (nix-libs) file-out)
(.close file-out)))