-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
core.clj
337 lines (306 loc) · 11.6 KB
/
core.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
(ns build-edn.core
(:require
[build-edn.pom :as be.pom]
[build-edn.repository :as be.repo]
[build-edn.schema :as be.schema]
[build-edn.util.string :as be.u.str]
[build-edn.variable :as be.var]
[build-edn.version :as be.ver]
[clojure.string :as str]
[clojure.tools.build.api :as b]
[deps-deploy.deps-deploy :as deploy]
[malli.core :as m]
[malli.error :as me]
[malli.util :as mu]
[pogonos.core :as pg]
[rewrite-clj.zip :as z])
(:import
(java.util.regex
Pattern)))
(def ^:private default-configs
{:class-dir "target/classes"
:jar-file "target/{{lib}}.jar"
:uber-file "target/{{lib}}-standalone.jar"
:deploy-repository {:id "clojars"}
:pom {:no-clojure-itself? false}
:skip-compiling-dirs #{"resources"}
:github-actions? false})
(defn- getenv
[k]
(System/getenv k))
(defn- validate-config!
([config]
(validate-config! be.schema/?build-config config))
([?schema config]
(when-let [e (m/explain ?schema config)]
(let [m (me/humanize e)]
(throw (ex-info (str "Invalid config: " m) m))))))
(defn- generate-render-data
([]
(generate-render-data {}))
([{:keys [lib version]}]
(merge (be.var/variable-map)
(and lib {:lib (name lib)})
(and version {:version version})
(and version (be.ver/parse-semantic-version version)))))
(defn- gen-config
[arg]
(or (:config arg)
(let [render-data (generate-render-data)
config (cond-> arg
(and (:version arg)
(str/includes? (:version arg) "{{"))
(update :version #(pg/render-string % render-data)))
scm (or (get-in config [:pom :scm])
(be.pom/generate-scm-from-git-dir))
config (if (and scm (map? scm))
(assoc config :scm scm)
config)
config (cond-> config
(contains? config :scm)
(assoc-in [:scm :tag] (:version config)))]
(merge default-configs config))))
(defn- get-basis
[arg]
(or (:basis arg)
(-> (select-keys arg [:aliases])
(b/create-basis))))
(defn- get-src-dirs
[config basis]
(or (:source-dirs config)
(:paths basis)))
(defn- set-gha-output
[config k v]
(when (:github-actions? config)
(when-let [github-output (getenv "GITHUB_OUTPUT")]
(spit github-output (str k "=" v "\n") :append true))))
(defn pom
[arg]
(let [{:as config :keys [description licenses pom]} (gen-config arg)
?schema (mu/merge be.schema/?build-config
be.schema/?pom-build-config)
_ (validate-config! ?schema config)
basis (cond-> (get-basis arg)
(or (:no-clojure-itself? pom) false)
(update :libs dissoc 'org.clojure/clojure))
pom-path (b/pom-path config)]
(-> config
(select-keys [:lib :version :class-dir :scm])
(assoc :basis basis
:src-dirs (get-src-dirs config basis)
:pom-data (cond-> []
description
(conj [:description description])
licenses
(conj (vec (cons :licenses
(map (fn [{:keys [name url]}]
[:license
[:name name]
[:url url]])
licenses))))))
(b/write-pom))
(set-gha-output config "pom" pom-path)
pom-path))
(defn jar
[arg]
(let [{:as config :keys [class-dir jar-file]} (gen-config arg)
_ (validate-config! config)
basis (get-basis arg)
arg (assoc arg :config config :basis basis)
jar-file (->> (generate-render-data config)
(pg/render-string jar-file))]
(pom arg)
(b/copy-dir {:src-dirs (get-src-dirs config basis)
:target-dir class-dir})
(b/jar {:class-dir class-dir
:jar-file jar-file})
(set-gha-output config "jar" jar-file)
jar-file))
(defn java-compile
[arg]
(let [{:as config :keys [class-dir java-paths javac-opts]} (gen-config arg)
?schema (mu/merge be.schema/?build-config
be.schema/?java-compile-config)
_ (validate-config! ?schema config)
basis (get-basis arg)]
(b/javac {:src-dirs java-paths
:class-dir class-dir
:basis basis
:javac-opts javac-opts})))
(defn uberjar
[arg]
(let [{:as config :keys [class-dir uber-file main skip-compiling-dirs]} (gen-config arg)
?schema (mu/merge be.schema/?build-config
be.schema/?uber-build-config)
_ (validate-config! ?schema config)
basis (get-basis arg)
src-dirs (get-src-dirs config basis)
arg (assoc arg :config config :basis basis)
uber-file (->> (generate-render-data config)
(pg/render-string uber-file))
skip-compiling-dir-set (set skip-compiling-dirs)]
(pom arg)
(b/copy-dir {:src-dirs src-dirs
:target-dir class-dir})
(b/compile-clj {:basis basis
:src-dirs (remove skip-compiling-dir-set src-dirs)
:class-dir class-dir})
(b/uber {:class-dir class-dir
:uber-file uber-file
:basis basis
:main main})
(set-gha-output config "uberjar" uber-file)
uber-file))
(defn install
[arg]
(let [{:as config :keys [lib version class-dir]} (gen-config arg)
_ (validate-config! config)
arg (assoc arg :config config)
jar-file (jar arg)]
(deploy/deploy {:artifact jar-file
:installer :local
:pom-file (b/pom-path {:lib lib :class-dir class-dir})})
(set-gha-output config "version" version)
version))
(defn deploy
[arg]
(let [{:as config :keys [lib version class-dir deploy-repository]} (gen-config arg)
?schema (mu/merge be.schema/?build-config
be.schema/?deploy-repository-build-config)
_ (validate-config! ?schema config)
{:keys [id username password url]} deploy-repository
arg (assoc arg :config config)
jar-file (jar arg)
repo (some-> id
(be.repo/repository-by-id)
(cond->
username (assoc-in [id :username] username)
password (assoc-in [id :password] password)
url (assoc-in [id :url] url)))]
(deploy/deploy (cond-> {:artifact jar-file
:installer :remote
:pom-file (b/pom-path {:lib lib :class-dir class-dir})}
repo (assoc :repository repo)))
(set-gha-output config "version" version)
version))
(defn- update-line
[f s]
(let [tail-blanks (if (re-seq #"^\s*$" s)
""
(first (re-seq #"\s*$" s)))
result (->> (str/split-lines s)
(mapcat #(f [%]))
(str/join "\n"))]
(str result tail-blanks)))
(defn update-documents
[arg]
(let [{:as config :keys [version documents]} (gen-config arg)
?schema (mu/merge be.schema/?build-config
be.schema/?documents-build-config)
_ (validate-config! ?schema config)
render-data (generate-render-data config)]
(doseq [{:keys [file match match-exactly action text keep-indent?]} documents
:let [match (if (string? match-exactly)
(Pattern/quote match-exactly)
match)
regexp (when (string? match)
(re-pattern match))
text (pg/render-string text render-data)]]
(if (= :create action)
(spit file text)
(->> (slurp file)
(update-line (fn [[line]]
(if (re-find regexp line)
(let [text (if keep-indent?
(be.u.str/add-indent line text)
text)]
(case action
:append-before [text line]
:append-after [line text]
[text]))
[line])))
(spit file))))
(set-gha-output config "version" version)
version))
(defn- print-error
([err]
(print-error 0 err))
([indent-level err]
(cond
(map? err)
(doseq [[k v] err]
(print-error indent-level k)
(print-error (inc indent-level) v))
(sequential? err)
(doseq [v err]
(print-error indent-level v))
:else
(println (format "%s* %s"
(str (apply str (repeat (* 2 indent-level) " ")))
err)))))
(defn lint
[arg]
(let [config (gen-config arg)
?schema (cond-> be.schema/?build-config
(contains? config :documents)
(mu/merge be.schema/?documents-build-config)
(contains? config :main)
(mu/merge be.schema/?uber-build-config)
(contains? config :deploy-repository)
(mu/merge be.schema/?deploy-repository-build-config)
(contains? config :pom)
(mu/merge be.schema/?pom-build-config)
(contains? config :java-paths)
(mu/merge be.schema/?java-compile-config))]
(if-let [e (m/explain ?schema config)]
(do (print-error (me/humanize e))
false)
(do (println "OK")
true))))
(defn- replace-version!
[config-file version]
(-> (z/of-file config-file)
(z/find-value z/next :version)
(z/right)
(z/replace version)
(z/root-string)
(->> (spit config-file))))
(defn- parse-and-validate-semantic-version
[{:as arg :keys [version]}]
(let [config (gen-config arg)
_ (validate-config! config)
parsed (be.ver/parse-semantic-version version)]
(if-not (and parsed (:config-file config))
(print-error (format "'%s' is not semantic versioning" version))
[config parsed])))
(defn bump-version
[{:as arg :keys [config-file]} version-type]
(when-let [[config parsed] (parse-and-validate-semantic-version arg)]
(let [new-version (some-> parsed
(be.ver/bump-version version-type)
(be.ver/to-semantic-version))]
(if-not new-version
(print-error (format "Could not bump %s version: %s"
(name version-type)
(be.ver/to-semantic-version parsed)))
(do (replace-version! config-file new-version)
(set-gha-output config "version" new-version)
new-version)))))
(defn add-snapshot
[{:as arg :keys [config-file]}]
(when-let [[config parsed] (parse-and-validate-semantic-version arg)]
(let [new-version (-> parsed
(be.ver/add-snapshot)
(be.ver/to-semantic-version))]
(replace-version! config-file new-version)
(set-gha-output config "version" new-version)
new-version)))
(defn remove-snapshot
[{:as arg :keys [config-file]}]
(when-let [[config parsed] (parse-and-validate-semantic-version arg)]
(let [new-version (-> parsed
(be.ver/remove-snapshot)
(be.ver/to-semantic-version))]
(replace-version! config-file new-version)
(set-gha-output config "version" new-version)
new-version)))