-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
git_inject.clj
48 lines (44 loc) · 1.79 KB
/
git_inject.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
(ns leiningen.git-inject
(:require
[clojure.walk :as walk]
[cuddlefish.core :as git])
(:import
(java.time LocalDateTime)
(java.time.format DateTimeFormatter)))
(def default-config
"The default configuration values."
{:git "git"
:describe-pattern git/git-describe-pattern})
(defn git-status-to-version
[config]
(let [{:keys [tag ahead ahead? dirty?]} (git/status config)]
(if-not (string? tag)
;; If git status is nil (e.g. IntelliJ reading project.clj) then return...
"git-tag-unavailable"
(if (and (not ahead?) (not dirty?))
(let [[_ version] (re-find #"v?(.*)" tag)]
version)
(let [[_ major minor patch suffix] (re-find #"v?(\d+)\.(\d+)\.(\d+)(-.+)?" tag)]
(if (nil? major)
;; If tag is poorly formatted then return...
"git-tag-invalid"
(let [patch' (try (Long/parseLong patch) (catch Throwable _ 0))
patch+ (inc patch')]
(str major "." minor "." patch+ suffix "-" ahead "-SNAPSHOT"))))))))
(def x->f
{:lein-git-inject/build-iso-date-time (fn [_] (.format (LocalDateTime/now) DateTimeFormatter/ISO_DATE_TIME))
:lein-git-inject/build-iso-week-date (fn [_] (.format (LocalDateTime/now) DateTimeFormatter/ISO_WEEK_DATE))
:lein-git-inject/version git-status-to-version
:lein-git-inject/username (fn [_] (System/getProperty "user.name"))})
(defn middleware
[{:keys [git-inject]
:as project}]
(let [config (merge default-config git-inject)
project' (walk/prewalk
(fn [x]
(let [k (if (string? x) (keyword x) x)]
(if-let [f (get x->f k)]
(f config)
x)))
project)]
project'))