-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.clj
More file actions
143 lines (119 loc) · 4.09 KB
/
build.clj
File metadata and controls
143 lines (119 loc) · 4.09 KB
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
(ns build
(:refer-clojure :exclude [test])
(:require
[clojure.tools.build.api :as b]
[deps-deploy.deps-deploy :as dd]))
(def lib 'com.vadelabs/uid)
(defn- get-git-tag-version
"Get version from current git tag if it exists"
[]
(try
(let [proc (.exec (Runtime/getRuntime) (into-array String ["git" "describe" "--exact-match" "--tags"]))
exit-code (.waitFor proc)]
(when (zero? exit-code)
(let [output (clojure.string/trim (slurp (.getInputStream proc)))]
(when (and (not (empty? output))
(clojure.string/starts-with? output "v"))
(subs output 1))))) ; Strip leading 'v'
(catch Exception _
nil)))
(defn- get-last-release-tag
"Get the last release tag (tags starting with 'v')"
[]
(try
(let [proc (.exec (Runtime/getRuntime) (into-array String ["git" "describe" "--tags" "--abbrev=0" "--match" "v*"]))
exit-code (.waitFor proc)]
(when (zero? exit-code)
(let [output (clojure.string/trim (slurp (.getInputStream proc)))]
(when-not (empty? output)
output))))
(catch Exception _
nil)))
(defn- count-commits-since-tag
"Count commits since the given tag"
[tag]
(try
(let [proc (.exec (Runtime/getRuntime) (into-array String ["git" "rev-list" (str tag "..HEAD") "--count"]))
_ (.waitFor proc)
output (clojure.string/trim (slurp (.getInputStream proc)))]
(if (empty? output)
0
(Integer/parseInt output)))
(catch Exception _
0)))
(defn- count-all-commits
"Count all commits in repository history"
[]
(try
(let [proc (.exec (Runtime/getRuntime) (into-array String ["git" "rev-list" "--count" "HEAD"]))
_ (.waitFor proc)
output (clojure.string/trim (slurp (.getInputStream proc)))]
(if (empty? output)
0
(Integer/parseInt output)))
(catch Exception _
0)))
(defn- date-commit-count-version
"Generate version as YYYY.MM.DD-N where N is commits since last release (or all commits if no release)"
[]
(let [date (.format (java.time.LocalDate/now)
(java.time.format.DateTimeFormatter/ofPattern "yyyy.MM.dd"))
last-tag (get-last-release-tag)
commit-count (if last-tag
(count-commits-since-tag last-tag)
(count-all-commits))]
(format "%s-%d" date commit-count)))
(def version (or (get-git-tag-version) (date-commit-count-version)))
(def class-dir "target/classes")
(defn- pom-template
[version]
[[:description "Unified interface for unique identifier generation - UUID (RFC9562) and Flake implementations"]
[:url "https://github.com/vadelabs/uid"]
[:licenses
[:license
[:name "MIT License"]
[:url "https://opensource.org/licenses/MIT"]]]
[:developers
[:developer
[:name "Pragyan"]
[:email "pragyan@vadelabs.com"]]]
[:scm
[:url "https://github.com/vadelabs/uid"]
[:connection "scm:git:https://github.com/vadelabs/uid.git"]
[:developerConnection "scm:git:ssh:git@github.com:vadelabs/uid.git"]
[:tag (str "v" version)]]])
(defn- jar-opts
[opts]
(assoc opts
:lib lib :version version
:jar-file (format "target/%s-%s.jar" lib version)
:basis (b/create-basis {})
:class-dir class-dir
:target "target"
:src-dirs ["src"]
:pom-data (pom-template version)))
(defn jar
"Build the JAR."
[opts]
(b/delete {:path "target"})
(let [opts (jar-opts opts)]
(println "\nWriting pom.xml...")
(b/write-pom opts)
(println "\nCopying source...")
(b/copy-dir {:src-dirs ["resources" "src"] :target-dir class-dir})
(println "\nBuilding JAR..." (:jar-file opts))
(b/jar opts))
opts)
(defn install
"Install the JAR locally."
[opts]
(let [opts (jar-opts opts)]
(b/install opts))
opts)
(defn deploy
"Deploy the JAR to Clojars."
[opts]
(let [{:keys [jar-file] :as opts} (jar-opts opts)]
(dd/deploy {:installer :remote :artifact (b/resolve-path jar-file)
:pom-file (b/pom-path (select-keys opts [:lib :class-dir]))}))
opts)