Skip to content

Instantly share code, notes, and snippets.

@just-sultanov
Last active January 8, 2022 18:18
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save just-sultanov/2c57297e2152a1e800c785873449d225 to your computer and use it in GitHub Desktop.
Save just-sultanov/2c57297e2152a1e800c785873449d225 to your computer and use it in GitHub Desktop.
Typical project tasks using babashka
{: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)))
}
0.1.{{build-number}}
@just-sultanov
Copy link
Author

All tasks specified in the src/dev/clojure/run.clj file.

;; deps.edn
{ ...
  :aliases {:run  {:extra-paths ["src/dev/clojure" "src/dev/resources"]
                   :extra-deps {io.github.clojure/tools.build    {:git/tag "v0.5.1" :git/sha "21da7d4"}
                                io.github.seancorfield/build-clj {:git/tag "v0.5.0" :git/sha "2ceb95a"}}
                   :ns-default run}}}

@just-sultanov
Copy link
Author

just-sultanov commented Oct 7, 2021

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