Skip to content

Commit

Permalink
Merge pull request #5 from kepler16/jv/change-ypwwlysorwvu
Browse files Browse the repository at this point in the history
Add native-image compat to ns loader
  • Loading branch information
julienvincent authored Apr 5, 2024
2 parents 9bdfcfd + 1534926 commit 0a1953d
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 11 deletions.
12 changes: 8 additions & 4 deletions packages/mallard/src/k16/mallard/api.clj
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,20 @@
(undo! props)
(run-next! props))

#_{:clj-kondo/ignore [:clojure-lsp/unused-public-var]}
(defn run
"A run function to be used in a Deps.edn project to execute operations using the file loader.
:init-store! - Should be given a symbol that resolves to a datastore init function.
:load-dir - should be a resource path to a directory containing operation files that will
be loaded using the file loader.
:action - should be given an action to perform. One of #{:up :down :next :undo :redo}"
:load-dir - should be a resource path to a directory containing operation files that will
be loaded using the file loader.
:operations - Should be a symbol resolving to a set of operations
:action - should be given an action to perform. One of #{:up :down :next :undo :redo}"
[{create-ctx-fn :create-ctx!
create-store-fn :create-store!
shutdown-fn :shutdown!
load-dir :load-dir
operations :operations
action :action}]

(let [create-store! (requiring-resolve create-store-fn)
Expand All @@ -52,7 +55,8 @@
(create-ctx))
store (create-store! context)
props {:context context
:operations (loaders.fs/load! load-dir)
:operations (or operations
(loaders.fs/load! load-dir))
:store store}]

(case action
Expand Down
2 changes: 2 additions & 0 deletions packages/mallard/src/k16/mallard/dev.clj
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
(ns k16.mallard.dev
"For use in development, this is a component that can be included in a gx configuration to automatically
run any migrations on system start."
(:refer-clojure :exclude [run!])
(:require
[k16.mallard.api :as api]
[k16.mallard.datastore :as datastore.api]
Expand All @@ -20,6 +21,7 @@
[:or executor/?Operations
[:=> [:cat] executor/?Operations]]]]]])

#_{:clj-kondo/ignore [:clojure-lsp/unused-public-var]}
(def run!
{:gx/start
{:gx/processor (fn [{:keys [props]}]
Expand Down
17 changes: 10 additions & 7 deletions packages/mallard/src/k16/mallard/loaders/ns.clj
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,15 @@

(set! *warn-on-reflection* true)

(defn load!
(defmacro load!
"Dynamically require all given namespaces as operation files."
[namespaces]
(->> namespaces
(map (fn [ns']
(require ns')
{:id (-> (name ns') (str/split #"\.") last)
:run-up! (ns-resolve ns' 'run-up!)
:run-down! (ns-resolve ns' 'run-down!)}))))
`(do
(doseq [namespace# ~namespaces]
(require namespace#))

(->> ~namespaces
(map (fn [namespace#]
{:id (-> namespace# str (str/split #"\.") last)
:run-up! (resolve (symbol (str namespace# "/run-up!")))
:run-down! (resolve (symbol (str namespace# "/run-down!")))})))))
17 changes: 17 additions & 0 deletions packages/mallard/test/k16/mallard/loaders/ns_test.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
(ns k16.mallard.loaders.ns-test
(:require
[clojure.test :refer [deftest is testing]]
[k16.mallard.loaders.ns :as loaders.ns]
[matcher-combinators.test]))

(deftest ns-loader-test
(testing "It should load migrations from a given collection of namespaces"
(let [migrations (loaders.ns/load! '(fixtures.migrations.1-migration
fixtures.migrations.2-migration))]
(is (match? [{:id "1-migration"
:run-up! (requiring-resolve 'fixtures.migrations.1-migration/run-up!)
:run-down! (requiring-resolve 'fixtures.migrations.1-migration/run-down!)}
{:id "2-migration"
:run-up! (requiring-resolve 'fixtures.migrations.2-migration/run-up!)
:run-down! (requiring-resolve 'fixtures.migrations.2-migration/run-down!)}]
migrations)))))

0 comments on commit 0a1953d

Please sign in to comment.