restructure build.clj functions to not need to accept options and return them, so that they may return useful data relevant to their task

This commit is contained in:
Kristin Rutenkolk 2024-06-12 15:52:44 -07:00
parent a04fe7253f
commit c740745d49

View file

@ -36,9 +36,8 @@
(defn clean (defn clean
"Deletes the `target/` directory." "Deletes the `target/` directory."
[opts] []
(b/delete {:path target-dir}) (b/delete {:path target-dir}))
opts)
(defn- exists? (defn- exists?
"Checks if a file composed of the given path segments exists." "Checks if a file composed of the given path segments exists."
@ -47,18 +46,17 @@
(defn compile-java (defn compile-java
"Compiles java classes required for interop." "Compiles java classes required for interop."
[opts] []
(.mkdirs (io/file class-dir)) (.mkdirs (io/file class-dir))
(b/process {:command-args ["javac" "--enable-preview" (b/process {:command-args ["javac" "--enable-preview"
"src/java/coffi/ffi/Loader.java" "src/java/coffi/ffi/Loader.java"
"-d" class-dir "-d" class-dir
"-target" "22" "-target" "22"
"-source" "22"]}) "-source" "22"]}))
opts)
(defn- write-pom (defn- write-pom
"Writes a pom file if one does not already exist." "Writes a pom file if one does not already exist."
[opts] []
(when-not (exists? (b/pom-path {:lib lib-coord (when-not (exists? (b/pom-path {:lib lib-coord
:class-dir class-dir})) :class-dir class-dir}))
(b/write-pom {:basis basis (b/write-pom {:basis basis
@ -72,44 +70,40 @@
:src-dirs source-dirs}) :src-dirs source-dirs})
(b/copy-file {:src (b/pom-path {:lib lib-coord (b/copy-file {:src (b/pom-path {:lib lib-coord
:class-dir class-dir}) :class-dir class-dir})
:target (str target-dir "pom.xml")})) :target (str target-dir "pom.xml")})))
opts)
(defn pom (defn pom
"Generates a `pom.xml` file in the `target/classes/META-INF` directory. "Generates a `pom.xml` file in the `target/classes/META-INF` directory.
If `:pom/output-path` is specified, copies the resulting pom file to it." If `:pom/output-path` is specified, copies the resulting pom file to it."
[opts] [opts]
(write-pom opts) (write-pom)
(when-some [path (:output-path opts)] (when-some [path (:output-path opts)]
(b/copy-file {:src (b/pom-path {:lib lib-coord (b/copy-file {:src (b/pom-path {:lib lib-coord
:class-dir class-dir}) :class-dir class-dir})
:target path})) :target path})))
opts)
(defn- copy-resources (defn- copy-resources
"Copies the resources from the [[resource-dirs]] to the [[class-dir]]." "Copies the resources from the [[resource-dirs]] to the [[class-dir]]."
[opts] []
(b/copy-dir {:target-dir class-dir (b/copy-dir {:target-dir class-dir
:src-dirs resource-dirs}) :src-dirs resource-dirs}))
opts)
(defn jar (defn jar
"Generates a `coffi.jar` file in the `target/` directory. "Generates a `coffi.jar` file in the `target/` directory.
This is a thin jar including only the sources." This is a thin jar including only the sources."
[opts] [opts]
(write-pom opts) (write-pom opts)
(compile-java opts) (compile-java)
(copy-resources opts) (copy-resources)
(when-not (exists? target-dir jar-file) (when-not (exists? target-dir jar-file)
(b/copy-dir {:target-dir class-dir (b/copy-dir {:target-dir class-dir
:src-dirs source-dirs}) :src-dirs source-dirs})
(b/jar {:class-dir class-dir (b/jar {:class-dir class-dir
:jar-file jar-file})) :jar-file jar-file})))
opts)
(defn compile-test-library (defn compile-test-library
"Compiles the C test code for running the tests." "Compiles the C test code for running the tests."
[opts] []
(let [c-files (->> c-test-dirs (let [c-files (->> c-test-dirs
(map io/file) (map io/file)
(mapcat file-seq) (mapcat file-seq)
@ -118,8 +112,20 @@
(.mkdirs (io/file target-dir)) (.mkdirs (io/file target-dir))
(b/process {:command-args (concat ["clang" "-fpic" "-shared"] (b/process {:command-args (concat ["clang" "-fpic" "-shared"]
c-files c-files
["-o" test-c-library])})) ["-o" test-c-library])})))
opts)
(defn- arities [fn-var] (:arglists (meta fn-var)))
(defn- niladic-only? [fn-var]
(let [ari (arities fn-var)
one-arity? (= 1 (count ari))
niladic? (= 0 (count (first ari)))]
(and one-arity? niladic?)))
(defn- call-optionally [fn-sym arg]
(let [fn-var (resolve fn-sym)]
(if (niladic-only? fn-var)
(fn-var)
(fn-var arg))))
(defn- call-optionally-with [arg] #(call-optionally % arg))
(defn run-tasks (defn run-tasks
"Runs a series of tasks with a set of options. "Runs a series of tasks with a set of options.
@ -127,8 +133,4 @@
the option keys are passed unmodified." the option keys are passed unmodified."
[opts] [opts]
(binding [*ns* (find-ns 'build)] (binding [*ns* (find-ns 'build)]
(reduce (run! (call-optionally-with opts) (:tasks opts))))
(fn [opts task]
((resolve task) opts))
opts
(:tasks opts))))