Skip to content

Instantly share code, notes, and snippets.

@borkdude

borkdude/gen-deps.clj

Last active Feb 16, 2021
Embed
What would you like to do?
Gen deps.edns in mono-repo using babashka script. Merge default dep versions.
#!/usr/bin/env bb
(ns update-deps
(:require [clojure.edn :as edn]
[clojure.java.io :as io]
[clojure.pprint :as pp :refer [pprint]]
[clojure.walk :as walk]))
(defn- merge-defaults [deps defaults]
(let [ff (some-fn deps (complement (partial contains? deps)))
overriden-deps (remove ff (keys defaults))
defaults (select-keys defaults overriden-deps)]
(merge deps defaults)))
(defn merge-default-deps [deps-map defaults]
(let [paths (into [[:deps]]
(map (fn [alias]
[:aliases alias :extra-deps])
(keys (:aliases deps-map))))]
(reduce
(fn [acc path]
(if-let [m (get-in acc path)]
(assoc-in acc path (merge-defaults m defaults))
acc))
deps-map
paths)))
(def base-deps (edn/read-string (slurp "base/deps.edn")))
(def default-deps (-> base-deps :aliases :base :default-deps))
(def repos (-> base-deps :mvn/repos))
(def key-order
{:paths 0
:deps 1
:aliases 2})
(defn comparator* [x y]
(if-let [o1 (key-order x)]
(if-let [o2 (key-order y)]
(compare o1 o2)
-1) ;; defined order goes first
(if (key-order y)
1 ;; defined order goes first
;; compare keywords by string, regardless of namespace
(compare (str x) (str y)))))
(defn sort-maps [m]
(walk/postwalk
(fn [m]
(if (map? m)
(into (sorted-map-by comparator*) m)
m))
m))
(defn gen-deps [dir]
(let [deps-template (edn/read-string (slurp (io/file dir "deps.template.edn")))
deps (merge-default-deps deps-template default-deps)
deps (update deps :mvn/repos merge repos)
deps (sort-maps deps)
writer (io/writer (io/file dir "deps.edn"))]
(binding [*out* writer
*print-namespace-maps* false]
(println ";; WARNING: Generated from deps.template.edn. Do not edit here. Use script/gen-deps.clj from the root dir to regenerate.")
(binding [pp/*print-right-margin* 100]
(pprint deps)))))
(gen-deps "common")
(gen-deps "genseng")
(gen-deps "app")
(gen-deps "annotator")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment