From bcb04cedf828e4883cdeb79a4f99ca21f3049122 Mon Sep 17 00:00:00 2001 From: Gert Goet Date: Fri, 20 Oct 2023 11:03:12 +0200 Subject: [PATCH] Release --- .github/workflows/ci.yml | 2 +- .github/workflows/release.yml | 33 ++++++++++ build.clj | 114 ++++++++++++++++++++++++++++++++++ deps.edn | 20 +++--- 4 files changed, 160 insertions(+), 9 deletions(-) create mode 100644 .github/workflows/release.yml create mode 100644 build.clj diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 313778a..ede2a6e 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -12,7 +12,7 @@ jobs: with: fetch-depth: 0 - name: Install clojure tools - uses: DeLaGuardo/setup-clojure@9.4 + uses: DeLaGuardo/setup-clojure@10.0 with: cli: latest - name: Run tests diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml new file mode 100644 index 0000000..239aaeb --- /dev/null +++ b/.github/workflows/release.yml @@ -0,0 +1,33 @@ +--- +name: Release + +on: + push: + branches: + - main + tags: + - '*' + +jobs: + Release: + runs-on: ubuntu-latest + steps: + - name: Checkout + uses: actions/checkout@v3 + with: + fetch-depth: 0 + + - name: Install clojure tools + uses: DeLaGuardo/setup-clojure@10.0 + with: + cli: latest + + - name: Build jar + run: clojure -T:build ci :git-version $(printf '"%s"' $(git describe --tags)) + + - name: Release to clojars + continue-on-error: true + env: + CLOJARS_USERNAME: ${{ secrets.CLOJARS_USERNAME }} + CLOJARS_PASSWORD: ${{ secrets.CLOJARS_PASSWORD }} + run: clojure -T:build deploy diff --git a/build.clj b/build.clj new file mode 100644 index 0000000..6a6175d --- /dev/null +++ b/build.clj @@ -0,0 +1,114 @@ +(ns build + (:refer-clojure :exclude [test]) + (:require [clojure.data.xml :as xml] + [clojure.tools.build.api :as b] + [clojure.tools.build.tasks.write-pom :as write-pom] + [deps-deploy.deps-deploy :as dd] + [clojure.java.io :as io])) + +;; Monkey patch to have provided dependencies in the POM +;; SOURCE https://clojurians.zulipchat.com/#narrow/stream/180378-slack-archive/topic/tools-deps/near/326868214 +(xml/alias-uri 'pom "http://maven.apache.org/POM/4.0.0") + +(alter-var-root + #'write-pom/to-dep + (fn [old] + (fn [[_ {:keys [scope]} :as pair]] + (cond-> (old pair) + scope + (conj [::pom/scope scope]))))) + +(def lib 'dk.thinkcreate/malli-select) + +(def class-dir "target/classes") + + +(defn- pom-template [version version-type] + [[:description "spec2-inspired selection of Malli schemas"] + [:url "https://github.com/dk.thinkcreate/mycoollib"] + [:licenses + [:license + [:name "MIT"] + [:url "https://github.com/eval/malli-select/blob/main/LICENSE"]]] + [:developers + [:developer + [:name "Gert Goet (@eval)"]]] + (cond-> [:scm + [:url "https://github.com/eval/malli-select"] + [:connection "scm:git:https://github.com/eval/malli-select.git"] + [:developerConnection "scm:git:ssh:git@github.com:eval/malli-select.git"]] + (= :exact version-type) (conj [:tag (str "v" version)]))]) + + +(defn- jar-opts [{:keys [version version-type] :as opts}] + (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 version-type))) + +(defn- git-version->version&type + "`git-version` typically output of `git describe --tags`, + e.g. `v1.2.3`, `v1.2.3-pre.1` or `v1.2.3-1-g`. + Yields map with `version` and `type`." + [git-version] + (let [type (condp re-find git-version + #"^v\d+\.\d+\.\d+$" :exact + #"^v\d+\.\d+\.\d+-pre\.\d+" :pre ;; pre-tag and any commit after + :build) + exact-version (second (re-find #"v(\d+\.\d+\.\d+)" git-version)) + version (case type + :exact exact-version + :pre (str exact-version "-SNAPSHOT") + :build (subs git-version 1))] + {:version version :version-type type})) + +(comment + (git-version->version&type "v1.2.3-123") + + #_:end) + +(defn build + "Build the JAR. + Usage: + $ clojure -T:build build :git-version $(printf '\" %s \"' $(git describe --tags))" + [{:keys [git-version] :as opts}] + {:pre [(let [git-version-re #"^v\d+\.\d+\.\d+"] + (or (and git-version (re-find git-version-re git-version)) + (throw (ex-info (str "requires :git-version with value matching " (pr-str git-version-re) ", e.g. :git-version '\"v1.2.3\"\"'") {}))))]} + (let [opts (merge opts (git-version->version&type git-version))] + (b/delete {:path "target"}) + (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..." (:jar-file opts)) + (b/jar opts))) + opts) + +(defn- pom-path->version [pom-path] + (->> (io/reader pom-path) + xml/parse + :content + (filter map?) + (filter (comp #(= "version" %) name :tag)) + first + :content + first)) + +(defn deploy "Deploy the JAR to Clojars." [opts] + (let [{:keys [jar-file] :as opts} (jar-opts opts) + pom-file (b/pom-path (select-keys opts [:lib :class-dir])) + version (pom-path->version pom-file) + deployable-version-re #"^\d+\.\d+\.\d+(-SNAPSHOT)?$"] + (when-not (re-find deployable-version-re version) + (throw (ex-info (str "Can't deploy build-version" + " (version: " + (pr-str version) ")") {}))) + (dd/deploy {:installer :remote :artifact (b/resolve-path jar-file) + :pom-file pom-file})) + opts) diff --git a/deps.edn b/deps.edn index 688e188..959fc6a 100644 --- a/deps.edn +++ b/deps.edn @@ -23,11 +23,15 @@ :extra-deps {metosin/malli {:mvn/version "0.8.9"} io.github.cognitect-labs/test-runner {:git/tag "v0.5.1" :git/sha "dfb30dd"}} :exec-fn cognitect.test-runner.api/test} - :perf {#_#_:extra-paths ["perf"] - :extra-deps {criterium/criterium {:mvn/version "0.4.6"} - org.clojure/clojure {:mvn/version "1.11.1"} - com.clojure-goes-fast/clj-async-profiler {:mvn/version "1.0.5"}} - :jvm-opts ["-server" - "-Xmx4096m" - "-Dclojure.compiler.direct-linking=true" - "-Djdk.attach.allowAttachSelf"]}}} + :perf {#_#_:extra-paths ["perf"] + :extra-deps {criterium/criterium {:mvn/version "0.4.6"} + org.clojure/clojure {:mvn/version "1.11.1"} + com.clojure-goes-fast/clj-async-profiler {:mvn/version "1.0.5"}} + :jvm-opts ["-server" + "-Xmx4096m" + "-Dclojure.compiler.direct-linking=true" + "-Djdk.attach.allowAttachSelf"]} + :build {:deps {io.github.clojure/tools.build + {:mvn/version "0.9.6"} + slipset/deps-deploy {:mvn/version "0.2.1"}} + :ns-default build}}}