forked from abogoyavlensky/automigrate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.clj
153 lines (114 loc) · 3.86 KB
/
build.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
(ns build
"Tools for building and deploying lib artefacts to Clojars."
(:require [clojure.tools.build.api :as tools-build]
[org.corfield.build :as build-clj]
[clojure.spec.alpha :as s]
[clojure.string :as str]))
; Enable asserts for spec
(s/check-asserts true)
(def ^:private lib 'net.clojars.abogoyavlensky/automigrate)
(def ^:private SNAPSHOT-SUFFIX "-SNAPSHOT")
(defn- latest-git-tag-name
"Return latest git tag name as a string."
[]
(tools-build/git-process {:git-args ["describe" "--tags" "--abbrev=0"]}))
(defn- latest-git-tag-name-across-all-branches
"Return latest git tag name as a string across all branches."
[]
(let [latest-tag-rev (tools-build/git-process
{:git-args ["rev-list" "--tags" "--max-count=1"]})]
(tools-build/git-process {:git-args ["describe" "--tags" latest-tag-rev]})))
(s/def ::version
(s/coll-of integer? :min-count 3 :max-count 3 :kind vector?))
(s/def ::snapshot? boolean?)
(s/def ::release? boolean?)
(s/def ::bump #{:major :minor :patch})
(s/def ::version-args
(s/keys
:opt-un [::snapshot?
::release?
::bump]))
(defn- split-git-tag
[git-tag]
(mapv #(Integer. %) (str/split git-tag #"\.")))
(defn- join-git-tag
[git-tag-splitted]
(str/join "." (mapv str git-tag-splitted)))
(defn- bump-version
"Bump anyu part of semver version string."
[git-tag-name bump]
(let [splitted-tag (split-git-tag git-tag-name)
next-git-tag (condp = bump
:patch (update splitted-tag 2 inc)
:minor (-> splitted-tag
(update 1 inc)
(assoc 2 0))
:major (-> splitted-tag
(update 0 inc)
(assoc 1 0)
(assoc 2 0)))]
(join-git-tag next-git-tag)))
(defn- add-snapshot
"Add SNAPSHOT suffix to the string version."
[git-tag-name]
(str git-tag-name SNAPSHOT-SUFFIX))
(defn- version
"Return version by latest tag.
Optionally you could bump any part of version or add snapshot suffix."
[{:keys [snapshot? bump release?] :as version-args}]
{:pre [(s/valid? ::version-args version-args)]}
(let [latest-version (if (true? release?)
(latest-git-tag-name)
(latest-git-tag-name-across-all-branches))
_ (s/assert ::version (split-git-tag latest-version))
_ (println (format "Latest version: %s" latest-version))
new-version (cond-> latest-version
(some? bump) (bump-version bump)
(true? snapshot?) (add-snapshot))]
(println (format "New version: %s" new-version))
new-version))
(defn- create-git-tag
[{version-name :version
:as opts}]
(tools-build/git-process
{:git-args ["tag" "-a" version-name "-m" (format "'Release version %s'" version-name)]})
opts)
(defn- push-git-tag
[{version-name :version
:as opts}]
(tools-build/git-process {:git-args ["push" "origin" version-name]})
opts)
; Public
(defn build
"Build a jar-file for the lib."
[opts]
(let [new-version (version (select-keys opts [:snapshot? :release? :bump]))]
(-> opts
(assoc
:lib lib
:version new-version
:tag new-version
:src-pom "templates/pom.xml")
(build-clj/clean)
(build-clj/jar))))
(defn install
"Build and install jar-file to the local repo."
[opts]
(-> opts
(build)
(build-clj/install)))
(defn deploy
"Build and deploy the jar-file to Clojars."
[opts]
(-> opts
(build)
(build-clj/deploy)))
(defn release
"Bump the latest git tag version, create and push new git tag with next version."
[opts]
(-> opts
(assoc
:lib lib
:version (version (select-keys opts [:bump])))
(create-git-tag)
(push-git-tag)))