Skip to content

Commit

Permalink
Release
Browse files Browse the repository at this point in the history
  • Loading branch information
eval committed Oct 20, 2023
1 parent 3526826 commit bcb04ce
Show file tree
Hide file tree
Showing 4 changed files with 160 additions and 9 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
33 changes: 33 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -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
114 changes: 114 additions & 0 deletions build.clj
Original file line number Diff line number Diff line change
@@ -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<sha>`.
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)
20 changes: 12 additions & 8 deletions deps.edn
Original file line number Diff line number Diff line change
Expand Up @@ -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}}}

0 comments on commit bcb04ce

Please sign in to comment.