Last active
January 8, 2022 18:18
-
-
Save just-sultanov/2c57297e2152a1e800c785873449d225 to your computer and use it in GitHub Desktop.
Typical project tasks using babashka
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{:min-bb-version "0.6.1" | |
:tasks | |
{:requires ([babashka.fs :as fs] | |
[babashka.process :as proc] | |
[clojure.string :as str] | |
[clojure.pprint :as pprint]) | |
;; Helpers | |
:init (do | |
(defn get-env [s] | |
(System/getenv s)) | |
(defn pretty-print [x] | |
(binding [pprint/*print-right-margin* 130] | |
(pprint/pprint x))) | |
(defn as-params [params] | |
(->> params | |
(seq) | |
(flatten) | |
(map | |
(fn [x] | |
(str/replace (pr-str x) (java.util.regex.Pattern/compile "(\".+\")") "'$1'"))) | |
(str/join \space))) | |
(defn execute [command] | |
(-> (proc/process command) :out slurp str/trim-newline)) | |
(defn release? [branch] | |
(= "master" branch)) | |
(defn snapshot? [branch] | |
(not= "master" branch)) | |
(defn deployable? [branch] | |
(contains? #{"master" "develop"} branch)) | |
(def -zone-id (java.time.ZoneId/of "UTC")) | |
(def -datetime-formatter java.time.format.DateTimeFormatter/ISO_OFFSET_DATE_TIME) | |
(def -current-timestamp (java.time.ZonedDateTime/now -zone-id)) | |
(def -build-timestamp (str (.format -current-timestamp -datetime-formatter))) | |
(def -build-number (execute ["git" "rev-list" "HEAD" "--count"])) | |
(def -git-url (execute ["git" "config" "--get" "remote.origin.url"])) | |
(def -git-branch (execute ["git" "rev-parse" "--abbrev-ref" "HEAD"])) | |
(def -git-sha (execute ["git" "rev-parse" "--short" "HEAD"])) | |
(def -version-template (execute ["cat" "version.tmpl"])) | |
(def -version (cond-> (str/replace -version-template "{{build-number}}" -build-number) | |
(snapshot? -git-branch) (str "-SNAPSHOT"))) | |
(def -config | |
{:version -version | |
:build-number -build-number | |
:build-timestamp -build-timestamp | |
:git-url -git-url | |
:git-branch -git-branch | |
:git-sha -git-sha})) | |
:enter (let [{:keys [doc print-doc?] | |
:or {print-doc? true}} (current-task)] | |
(when (and print-doc? doc) | |
(println (str "▸ " doc)))) | |
;; Tasks | |
config {:doc "[project] Show config" | |
:print-doc? false | |
:task (pretty-print -config)} | |
version {:doc "[project] Show version" | |
:print-doc? false | |
:task (print -version)} | |
build {:doc "[project] Run build" | |
:task (clojure (str "-T:run build " (as-params -config)))}} | |
;; run this command in the specified directory: | |
;; (shell {:dir "modules/backend"} (str "clojure -T:run build " (as-params -config))) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
0.1.{{build-number}} |
Example output:
$ bb version
v0.1.45-SNAPSHOT
$ bb config
{:version "v0.1.45-SNAPSHOT",
:build-number "45",
:build-timestamp "2021-10-07T13:36:26.688484Z",
:git-url "git@github.com:sultanov-team/secret-keeper.git",
:git-branch "example",
:git-sha "f62e982"}
$ bb build
▸ [project] Run build
Received build params:
{:version "v0.1.45-SNAPSHOT",
:build-number "45",
:build-timestamp "2021-10-07T13:36:26.688484Z",
:git-url "git@github.com:sultanov-team/secret-keeper.git",
:git-branch "example",
:git-sha "f62e982"}
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
All tasks specified in the
src/dev/clojure/run.clj
file.