Skip to content

Instantly share code, notes, and snippets.

@mikeananev
Last active May 1, 2021 14:35
Show Gist options
  • Save mikeananev/c87c1596aed50b7bb095a816f6921737 to your computer and use it in GitHub Desktop.
Save mikeananev/c87c1596aed50b7bb095a816f6921737 to your computer and use it in GitHub Desktop.
Babashka script example
{:deps {cprop/cprop {:mvn/version "0.1.17"}}
:tasks {:requires ([babashka.fs :as fs]
[cprop.core :refer [load-config]]
[cprop.source :refer [from-env]])
;; helpers and constants
:init (do
(def ansi-green "\u001B[32m") (def ansi-reset "\u001B[0m") (def ansi-yellow "\u001B[33m")
(def target-folder "target")
(defn current-date [] (let [date (java.time.LocalDateTime/now)
formatter (java.time.format.DateTimeFormatter/ofPattern "yyyy-MM-dd HH:mm:ss")]
(.format date formatter)))
(def config (load-config :file "project-config.edn"))
(def secrets (load-config :file "project-secrets.edn"))
(def env (from-env))
(def version-file "project-version")
(def version-id (clojure.string/trim (slurp version-file)))
(def group-id (:group-id config))
(def artifact-id (:artifact-id config))
(def project-name (:project-name config))
(def jar-filename (format "%s/%s-%s.jar" target-folder artifact-id version-id))
)
:enter (let [{:keys [name]} (current-task)] (println (current-date) ansi-yellow "[ ]" ansi-reset name))
:leave (let [{:keys [name]} (current-task)] (println (current-date) ansi-green "[✔]︎" ansi-reset name))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; commands
clean {:doc "Clean target folder"
:task (do (fs/delete-tree target-folder)
(fs/create-dir target-folder))}
build {:doc "Build deployable jar file for this project"
:task (let [params (format "-X:jar :jar %s :group-id %s :artifact-id %s :version '\"%s\"'"
jar-filename
group-id
artifact-id
version-id)]
(fs/create-dirs target-folder)
(clojure params))}
install {:doc "Install deployable jar locally (requires the pom.xml file)"
:task (let [params (format "-X:install :installer :local :artifact '\"%s\"'" jar-filename)]
(clojure params))}
deploy {:doc "Deploy this library to Clojars"
:task (let [cmd (format "clojure -X:install :installer :remote :artifact '\"%s\"'" jar-filename)]
(shell (format "env CLOJARS_USERNAME=%s CLOJARS_PASSWORD=%s %s"
(:clojars/username -secrets)
(:clojars/password -secrets)
cmd)))}
repl {:doc "Run Clojure repl"
:task (clojure "-M:repl")}
outdated {:doc "Check for outdated dependencies"
:task (clojure (str "-M:outdated " (apply str (interpose " " *command-line-args*))))}
bump {:doc "Bump version artifact in `project-version` file, level may be one of: major, minor, patch, alpha, beta, rc, release."
:task (let [param (first *command-line-args*)
level (or (#{"major" "minor" "patch" "alpha" "beta" "rc" "release"} param) "patch")]
(shell {:out version-file} (format "bb -f scripts/bump-semver.clj %s %s" version-id level))
(println version-id "->" (slurp version-file)))}
test {:doc "Run tests"
:task (clojure (str "-M:test " (apply str (interpose " " *command-line-args*))))}
format {:doc "Format source code"
:task (shell "cljstyle fix")}
lint {:doc "Lint source code"
:task (do (shell "clj-kondo --parallel --lint src:test:dev/src")
(shell "cljstyle check"))}
requirements {:doc "Install project requirements"
:task (let [os-name (clojure.string/lower-case (System/getProperty "os.name"))]
(case os-name
"mac os x" (do
(shell "brew install git")
(shell "brew install coreutils")
(shell "brew install --cask cljstyle")
(shell "brew install borkdude/brew/clj-kondo"))
(println "Please, install manually the following tools: git, cljstyle, clj-kondo")))}}}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment