Skip to content

Instantly share code, notes, and snippets.

@kwrooijen
Created May 4, 2021 19:39
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kwrooijen/e932169d5fed2b84f464ef51777621cb to your computer and use it in GitHub Desktop.
Save kwrooijen/e932169d5fed2b84f464ef51777621cb to your computer and use it in GitHub Desktop.
Duct GraalVM module loader
(ns duct.loader
"Not used yet. Meant for Possible GraalVM future. This namespace is
meant to require any namespace with ig/init-keys. GraalVM cannot
require them dynamically (which is what integrant does). So instead
we use a macro at compile time to fetch all init-keys."
(:require
[clojure.java.io :as io]
[duct.core :as duct]
[integrant.core :as ig]
[weavejester.dependency :as dep]
[clojure.set :as set]))
(defn- find-keys [config keys f]
(let [graph (ig/dependency-graph config {:include-refsets? false})
keyset (set (mapcat #(map key (ig/find-derived config %)) keys))]
(->> (f graph keyset)
(set/union keyset)
(sort (ig/key-comparator (ig/dependency-graph config))))))
(defn dependent-keys [config keys]
(find-keys config keys dep/transitive-dependencies-set))
(defn- keyword->namespaces [kw]
(when-let [ns (namespace kw)]
[(symbol ns)
(symbol (str ns "." (name kw)))]))
(defn- key->namespaces [k]
(if (vector? k)
(mapcat keyword->namespaces k)
(keyword->namespaces k)))
(defn all-namespaces [config]
(->> (dependent-keys config (keys config))
(mapcat #(conj (ancestors %) %))
(mapcat key->namespaces)
(distinct)))
(defmacro try-require [r]
`(try
(require '[~r])
'[~r]
(catch java.io.FileNotFoundException e#)))
(defmacro require-keys-from-config [path]
`(do
~@(for [i# (all-namespaces (duct/build-config (duct/read-config (io/resource path))))]
`(try-require ~i#))))
(defmacro require-modules-from-config [path]
`(do
~@(for [i# (all-namespaces (duct/read-config (io/resource path)))]
`(try-require ~i#))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment