Skip to content

Instantly share code, notes, and snippets.

@emidln
Created September 23, 2015 18:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save emidln/3b4096aeec6a4f6992ab to your computer and use it in GitHub Desktop.
Save emidln/3b4096aeec6a4f6992ab to your computer and use it in GitHub Desktop.
(ns my.build-info
(:require [clojure.java.shell :as shell]
[clojure.string :as str]
[cheshire.core :as json]
[clojure.java.io :as io]
[clj-time.core :as t]
[clj-time.format :as tf]))
;; shell utility library
(def exec-str
"Executes a string in a subshell."
(comp (partial apply shell/sh) #(str/split % #" ")))
(defmacro list+
"Returns a list with symbols in scope resolved and symbols
not in scope as strings."
[& forms]
(->> forms
(map (fn [x] (if (and (symbol? x)
(not (contains? &env x))
(not (resolve x)))
(name x)
x)))
(cons `list)))
(defmacro sh
"Now witness the firepower of this fully ARMED and OPERATIONAL battle station!"
[& args]
`(some->> (list+ ~@args)
(str/join " ")
exec-str
:out))
;; git library
(defmacro git
"Executes git commands. The full power of git in your repl"
[& args]
`(-> (sh "git" ~@args)
str/trimr))
(defn now-iso-str
[]
(tf/unparse (tf/formatters :date-time) (t/now)))
(defn build-info []
{:sha (git rev-parse --short HEAD)
:branch (git rev-parse --abbrev-ref HEAD)
:timestamp (now-iso-str)})
(defmacro write-build-info
[path]
`(with-open [out# (io/writer ~path)]
(json/generate-stream ~(build-info) out#)))
(def BUILD_INFO_PATH "src/build_info.json")
(def BUILD_INFO_RESOURCE_PATH "build_info.json")
(def with-lein? #(contains? (System/getenv) "LEIN_VERSION"))
(defn read-build-info-from-json
[path]
(with-open [in (-> path io/resource io/reader)]
(json/parse-stream in)))
(if (with-lein?)
(do
(write-build-info BUILD_INFO_PATH)
(def read-build-info build-info))
(def read-build-info (memoize (partial read-build-info-from-json BUILD_INFO_RESOURCE_PATH))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment