Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

build-clj #8

Merged
merged 1 commit into from
Feb 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/clojure.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,4 @@ jobs:
- name: Prepare Deps
run: clj -P
- name: Test, Write POM, Build
run: clj -T:build ci
run: clj -T:build jar
122 changes: 70 additions & 52 deletions build.clj
Original file line number Diff line number Diff line change
@@ -1,86 +1,104 @@
(ns build
"build script.

Test
"Clean:
clj -T:build clean

Test:
clj -T:build test

Test, Write POM, Build
clj -T:build ci
Test, Write POM, Build JAR:
clj -T:build jar

Deploy to Clojars
CLOJARS_USERNAME=username CLOJARS_PASSWORD=clojars-token clj -T:build deploy"
Deploy to Clojars:
CLOJARS_USERNAME=username
CLOJARS_PASSWORD=clojars-token
clj -T:build deploy"
(:refer-clojure :exclude [test])
(:require [clojure.tools.build.api :as b]
[deps-deploy.deps-deploy :as dd]))

;; Project Metadata
(def lib 'com.github.wdhowe/clj-contrib)
(def version "1.0.0")
(def project
"Project Metadata"
{:description "A library of functions to enhance clojure.core."
:lib 'com.github.wdhowe/clj-contrib
:version "1.0.0"
:license "Eclipse Public License"
:license-url "http://www.eclipse.org/legal/epl-v20.html"
:author "Bill Howe"
:url "https://github.com/wdhowe/clj-contrib"
:git-repo "wdhowe/clj-contrib"
:class-dir "target/classes"})

(defn- pom-template
[version]
(let [git-repo (:git-repo project)
url (:url project)]
[[:description (:description project)]
[:url url]
[:licenses
[:license
[:name (:license project)]
[:url (:license-url project)]]]
[:developers
[:developer
[:name (:author project)]]]
[:scm
[:url url]
[:connection (format "scm:git:%s.git" url)]
[:developerConnection (format "scm:git:ssh:git@github.com:%s.git" git-repo)]
[:tag (str "v" version)]]]))

(defn- jar-opts
[opts]
(let [version (:version project)
lib (:lib project)]
(assoc opts
:basis (b/create-basis {})
:class-dir (:class-dir project)
:jar-file (format "target/%s-%s.jar" lib version)
:lib lib
:pom-data (pom-template version)
:src-dirs ["src"]
:target "target"
:version version)))

(defn- the-version [patch] (format "1.0.%s" patch))
;(def version (the-version (b/git-count-revs nil)))
(def snapshot (the-version "999-SNAPSHOT"))
(def class-dir "target/classes")
(defn clean
"Remove build directory."
[_]
(b/delete {:path "target"}))

(defn test
"Run all the tests."
[opts]
(let [basis (b/create-basis {:aliases [:test]})
cmds (b/java-command
{:basis basis
:main 'clojure.main
:main-args ["-m" "cognitect.test-runner"]})
{:basis basis
:main 'clojure.main
:main-args ["-m" "cognitect.test-runner"]})
{:keys [exit]} (b/process cmds)]
(when-not (zero? exit) (throw (ex-info "Tests failed" {}))))
opts)

(defn- pom-template
[version]
[[:description "A library of functions to enhance clojure.core."]
[:url "https://github.com/wdhowe/clj-contrib"]
[:licenses
[:license
[:name "Eclipse Public License"]
[:url "http://www.eclipse.org/legal/epl-v20.html"]]]
[:developers
[:developer
[:name "Bill Howe"]]]
[:scm
[:url "https://github.com/wdhowe/clj-contrib"]
[:connection "scm:git:https://github.com/wdhowe/clj-contrib.git"]
[:developerConnection "scm:git:ssh:git@github.com:wdhowe/clj-contrib.git"]
[:tag (str "v" version)]]])

(defn- jar-opts
[opts]
(let [version (if (:snapshot opts) snapshot version)]
(assoc opts
:lib lib :version version
:jar-file (format "target/%s-%s.jar" lib version)
:basis (b/create-basis {})
:class-dir class-dir
:target "target"
:src-dirs ["src"]
:pom-data (pom-template version))))

(defn ci
(defn jar
"Run the CI pipeline of: tests, write pom, and build the JAR."
[opts]
(test opts)
(b/delete {:path "target"})
(clean nil)
(let [opts (jar-opts opts)]
(println "\nWriting pom.xml...")
(b/write-pom opts)
(println "\nCopying source...")
(b/copy-dir {:src-dirs ["resources" "src"] :target-dir class-dir})
(println "\nBuilding" (:jar-file opts) "...")
(b/copy-dir {:src-dirs ["resources" "src"]
:target-dir (:class-dir project)})
(println "\nBuilding JAR" (:jar-file opts) "...")
(b/jar opts))
opts)

(defn deploy
"Deploy the JAR to Clojars."
[opts]
(let [{:keys [jar-file] :as opts} (jar-opts opts)]
(dd/deploy {:installer :remote :artifact (b/resolve-path jar-file)
(let [{:keys [jar-file] :as opts}
(jar-opts opts)]
(dd/deploy {:installer :remote
:artifact (b/resolve-path jar-file)
:pom-file (b/pom-path (select-keys opts [:lib :class-dir]))}))
opts)