Skip to content

Commit

Permalink
Optionally allow specifying time with timestamp in MS via env var INF…
Browse files Browse the repository at this point in the history
…ERV_TIMESTAMP_MS
  • Loading branch information
devth committed Oct 28, 2020
1 parent 74d2ced commit b461fd0
Showing 1 changed file with 32 additions and 7 deletions.
39 changes: 32 additions & 7 deletions src/lein_inferv/plugin.clj
Original file line number Diff line number Diff line change
@@ -1,11 +1,20 @@
(ns lein-inferv.plugin
(:require
[leiningen.core.main]
[clojure.string :refer [trim-newline]]
[clojure.string :refer [blank? trim-newline]]
[clojure.java.shell :refer [sh]]))

;; must be computed once in order to ensure a stable timestamp
(def now (java.time.Instant/now))
(def now-utc
(.atZone
(if (not (blank? (System/getenv "INFERV_TIMESTAMP_MS")))
;; obtain timestamp from env
(-> (System/getenv "INFERV_TIMESTAMP_MS")
read-string
java.time.Instant/ofEpochMilli)
;; or grab current
(java.time.Instant/now))
java.time.ZoneOffset/UTC))

(defn has-git? [] (zero? (:exit (sh "git" "version"))))

Expand All @@ -22,6 +31,13 @@
{:release-tasks [["vcs" "assert-committed"]
["deploy"]]})

(defn format-java-instant
[instant]
(.format
(java.time.format.DateTimeFormatter/ofPattern
"yyyyMMdd.HHmmss")
instant))

(defn middleware
"Leiningen middleware that:
Expand All @@ -43,11 +59,20 @@
"Skipping version inferrence: lein-inferv requires there to be at least 1 commit but this repo has none")
project)

:else (let [instant (.atZone now java.time.ZoneOffset/UTC)
datetime (.format
(java.time.format.DateTimeFormatter/ofPattern
"yyyyMMdd.HHmmss")
instant)]
:else (let [datetime (format-java-instant now-utc)]
(merge project
release-tasks
{:version (format "%s.%s" datetime (short-ref))}))))


(comment


;; here's an example of taking a milliseconds string, parsing into a
;; java.time.Instant and formatting
(format-java-instant
(->
"1603926430488"
read-string
java.time.Instant/ofEpochMilli
(.atZone java.time.ZoneOffset/UTC))))

0 comments on commit b461fd0

Please sign in to comment.