Skip to content

Instantly share code, notes, and snippets.

Last active September 14, 2021 08:06
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]
[ :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))))]
(fn [acc path]
(if-let [m (get-in acc path)]
(assoc-in acc path (merge-defaults m defaults))
(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]
(fn [m]
(if (map? m)
(into (sorted-map-by comparator*) 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")
(gen-deps "feeds")
(gen-deps "lvg")
(gen-deps "cuisine")
(gen-deps "integration")
(gen-deps "ontolog")
(gen-deps "triplestore_import")
Copy link

  • Each sub-project has a deps.template.edn which we then use to generate a corresponding deps.edn.
  • Comments, etc you store in the template, the generate deps.edn are a bunch of sorted maps, to be more friendly with git etc.
  • We execute this script in the :init phase of bb.edn so any time we execute a task, the deps.edns are synced. You could also hook it up to a git hook.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment