This repository has been archived by the owner on Nov 1, 2018. It is now read-only.
forked from cyverse-archive/DE
-
Notifications
You must be signed in to change notification settings - Fork 2
/
build-all.clj
423 lines (324 loc) · 11.6 KB
/
build-all.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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
(use '[leiningen.exec :only (deps)]
'[leiningen.core.project :only (defproject)])
(deps '[[cheshire "5.3.1"]
[me.raynes/fs "1.4.6"]
[org.clojure/tools.cli "0.3.1"]
[org.clojure/clojure "1.5.1"]
[medley "0.1.5"]
[org.clojure/tools.cli "0.2.1"]])
(require '[clojure.java.shell :as sh]
'[cheshire.core :as json]
'[clojure.java.io :as io]
'[clojure.pprint]
'[me.raynes.fs :as fs]
'[clojure.string :as string]
'[clojure.pprint :as pprint]
'[clojure.tools.cli :as cli]
'[clojure.edn :as edn])
(use '[medley.core])
(import '[java.nio.file Files])
(def projects (edn/read-string (slurp "project-defs.edn")))
; Categorized by type
(def lein-plugins (filter-vals #(= (:type %) :lein-plugin) projects))
(def tools (filter-vals #(= (:type %) :tool) projects))
(def services (filter-vals #(= (:type %) :service) projects))
(def libs (filter-vals #(= (:type %) :library) projects))
(def dbs (filter-vals #(= (:type %) :database) projects))
; Categorized by programming language
(def clojure-projects (filter-vals #(= (:language %) :clojure) projects))
(def java-projects (filter-vals #(= (:lanugage %) :java) projects))
; libraries are the only thing that have to be built in a certain order.
(def libs-build-order (sort-by #(:type-num (get libs %)) (keys libs)))
; cmdtar projects are an exceptional case
(def cmdtar (filter-vals :tarball? projects))
(def uberjar-projects (filter-vals :uberjar? projects))
(defn path-join
"Joins paths together and returns the resulting path as a string. nil and empty strings are
silently discarded.
Parameters:
paths - a parameter list of names to join together to form a path.
Returns:
It returns the path as a string."
[& paths]
(let [paths' (remove empty? paths)]
(if (empty? paths')
""
(str (apply io/file paths')))))
(defn print-shell-result
[key result]
(locking *out*
(let [keyprefix (str key ": ")]
(when-not (zero? (:exit result))
(println keyprefix "exit:" (:exit result)))
(when-not (string/blank? (:out result))
(println keyprefix "stdout:")
(println (:out result)))
(when-not (string/blank? (:err result))
(println keyprefix "error:")
(println (:err result)))
(when-not (zero? (:exit result))
(println keyprefix "ERROR ENCOUNTERED, EXITING!!!")
(System/exit 1)))))
;;; Multi-methods for installations
;;; The dispatch function extracts the :install field from the
;;; project map.
(defmulti install
(fn [project-map]
(:install project-map)))
(defmethod install :lein
[project-map]
(let [path-to-project (:path project-map)]
(sh/with-sh-dir path-to-project
(locking *out* (println ">> Installing" path-to-project))
(print-shell-result path-to-project (sh/sh "lein" "clean"))
(print-shell-result path-to-project (sh/sh "lein" "install")))))
(defmethod install :mvn
[project-map]
(let [path-to-project (:path project-map)]
(sh/with-sh-dir path-to-project
(locking *out* (println ">> Installing" path-to-project))
(print-shell-result path-to-project (sh/sh "mvn" "clean"))
(print-shell-result path-to-project (sh/sh "mvn" "install")))))
(defmethod install :default
[project-map]
(locking *out* (println ">> Don't know how to install" (:path project-map))))
;;; Multi-methods for builds
;;; The dispatch function extracts the :build field from the
;;; project map.
(defmulti build
(fn [project-map]
(:build project-map)))
(defmethod build :lein
[project-map]
(let [path-to-project (:path project-map)]
(sh/with-sh-dir path-to-project
(locking *out* (println ">> Building" path-to-project))
(print-shell-result path-to-project (sh/sh "lein" "clean"))
(print-shell-result path-to-project (sh/sh "lein" "uberjar")))))
(defmethod build :kifshare
[project-map]
(let [path-to-project (:path project-map)]
(sh/with-sh-dir
path-to-project
(locking *out* (println ">> Building" path-to-project))
(print-shell-result path-to-project (sh/sh "grunt" "--version"))
(print-shell-result path-to-project (sh/sh "npm" "--version"))
(print-shell-result path-to-project (sh/sh "npm" "install"))
(print-shell-result path-to-project (sh/sh "grunt" "clean-all"))
(print-shell-result path-to-project (sh/sh "lein" "uberjar")))))
(defmethod build :cmdtar
[project-map]
(let [path-to-project (:path project-map)]
(sh/with-sh-dir path-to-project
(locking *out* (println ">> Building iplant-cmdtar project" path-to-project))
(print-shell-result path-to-project (sh/sh "lein" "clean"))
(print-shell-result path-to-project (sh/sh "lein" "uberjar"))
(print-shell-result path-to-project (sh/sh "lein" "iplant-cmdtar")))))
(defmethod build :mvn
[project-map]
(let [path-to-project (:path project-map)]
(sh/with-sh-dir path-to-project
(locking *out* (println ">> Building Java project" path-to-project))
(print-shell-result path-to-project (sh/sh "mvn" "clean"))
(print-shell-result path-to-project (sh/sh "mvn" "build")))))
(defmethod build :shell
[project-map]
(let [path-to-project (:path project-map)]
(sh/with-sh-dir path-to-project
(locking *out* (println ">> Building shell project" path-to-project))
(print-shell-result path-to-project (sh/sh "./build.sh")))))
(defmethod build :default
[project-map]
(println ">> Don't know how to build" (:path project-map)))
(defn install-libs
"Installs the libraries in the correct order."
[]
(println "> Installing libs")
(doseq [prj-name libs-build-order]
(install (get projects prj-name))
(println "")))
(defn install-lein-plugins
[]
(println "> Installing the lein-plugins")
(doseq [[prj-name prj-map] (seq lein-plugins)]
(install prj-map)))
(defn- build-projects
([projects]
(build-projects projects false))
([projects parallel?]
(if parallel?
(doall (pmap
#(build (get projects %))
(keys projects)))
(doseq [k (keys projects)]
(build (get projects k))))
(println "")))
(defn build-databases
[opts]
(println "> Building the databases" (when (:parallel opts) " in parallel"))
(build-projects dbs (:parallel opts)))
(defn build-services
[opts]
(println "> Building the services" (when (:parallel opts) " in parallel"))
(build-projects services (:parallel opts)))
(defn build-tools
[opts]
(println "> Building tools" (when (:parallel opts) " in parallel"))
(build-projects tools (:parallel opts)))
(defn build-libs
[]
(println "> Building libraries")
(doseq [prj (mapv #(get libs %) libs-build-order)]
(build-projects prj)))
(defn bash-cmd
"A utility function for execing a script with bash."
[str-to-run]
(sh/sh "bash" "-c" str-to-run))
(defn move-uberjar-build
"Moves all .jar files from the target/ directory of the given
project. Yeah, it'll blow up if it's not there, but that's
intentional. We don't want any false positive builds."
[project-map]
(let [target-path (path-join (:path project-map) "target")]
(println ">> Copying builds from" target-path "to builds directory.")
(print-shell-result (str "copy " target-path) (bash-cmd (str "mv " target-path "/*.jar " "builds")))))
(defn move-cmdtar-build
"Moves all .tar.gz files found in the project's target directory."
[project-map]
(let [path-to-project (:path project-map)
target-path (path-join path-to-project "target")]
(println ">> Copying any cmdtars from" path-to-project "to builds directory.")
(print-shell-result path-to-project (bash-cmd (str "mv " target-path "/*.tar.gz " "builds")))))
(defn move-database-build
"Moves all .tar.gz files found in the project's root directory."
[project-map]
(let [path-to-project (:path project-map)]
(println ">> Copying builds from" path-to-project "to builds directory.")
(print-shell-result path-to-project (sh/sh "bash" "-c" (str "mv " path-to-project "/*.tar.gz " "builds")))))
(defn- archive-project
[opts project-map]
(when (:archive? project-map)
(when (:tarball? project-map)
(move-cmdtar-build project-map))
(when (:uberjar? project-map)
(move-uberjar-build project-map))
(when (= (:type project-map) :database)
(move-database-build project-map))))
(defn- archive-all
"Iterates over all of the projects defined in the given map
and passes the project map to the (archive-project) function."
[opts projects-map]
(doseq [[project-name project-map] (seq projects-map)]
(archive-project opts project-map)))
(defn archive-services
[opts]
(println "> Moving services builds")
(archive-all opts services)
(println ""))
(defn archive-lein-plugins
[opts]
(println "> Moving lein-plugins builds")
(archive-all opts lein-plugins)
(println ""))
(defn archive-libs
[opts]
(println "> Moving library builds")
(archive-all opts libs)
(println ""))
(defn archive-databases
[opts]
(println "> Moving database builds")
(archive-all opts dbs)
(println ""))
(defn archive-tools
[opts]
(println "> Moving tool builds")
(archive-all opts tools)
(println ""))
(defn prep-builds-dir
"Deletes and recreates the top-level builds/ directory, as
necessary."
[]
(when (fs/exists? "builds")
(println ">> Cleaning out builds directory")
(print-shell-result "cleaning builds" (sh/sh "rm" "-r" "builds")))
(println ">> Creating builds directory")
(print-shell-result "creating builds" (sh/sh "mkdir" "builds")))
(defn do-libs
[opts]
(install-libs)
(if (:archive opts)
(archive-libs opts)))
(defn do-lein-plugins
[opts]
(install-lein-plugins)
(if (:archive opts)
(archive-lein-plugins opts)))
(defn do-services
[opts]
(build-services opts)
(if (:archive opts)
(archive-services opts)))
(defn do-tools
[opts]
(build-tools opts)
(if (:archive opts)
(archive-tools opts)))
(defn do-databases
[opts]
(build-databases opts)
(if (:archive opts)
(archive-databases opts)))
(defn do-everything
[opts]
(do-lein-plugins opts)
(do-libs opts)
(do-services opts)
(do-tools opts)
(do-databases opts))
(defn parse-args
[args]
(cli/cli
args
["-h" "--help" "Show help." :default false :flag true]
["-b" "--build-number" "Assigns a build number" :default nil]
["-p" "--parallel" "Build in parallel?" :default false :flag true]
["-a" "--archive" "Archive builds?" :default false :flag true]))
(def valid-cmds
#{"help"
"lein-plugins"
"libs"
"services"
"tools"
"databases"})
(defn validate-cmds
[cmds]
(let [invalid-cmds (filter #(not (contains? valid-cmds %)) cmds)]
(when (pos? (count invalid-cmds))
(println "The following are invalid commands:")
(doseq [cmd invalid-cmds]
(println "*" cmd))
(System/exit 1))))
(defn main-func
[]
(let [args *command-line-args*
[opts cmds help-str] (parse-args args)
cmds (drop 1 cmds)
_ (validate-cmds cmds)]
(when (:archive opts)
(prep-builds-dir))
(when (pos? (count cmds))
(doseq [cmd cmds]
(case cmd
"help" (do (println help-str)
(println "Commands are:" (string/join " " (seq valid-cmds)))
(System/exit 0))
"lein-plugins" (do-lein-plugins opts)
"libs" (do-libs opts)
"services" (do-services opts)
"tools" (do-tools opts)
"databases" (do-databases opts)
(println "Not a valid command:" cmd))))
(when-not (pos? (count cmds))
(do-everything opts))))
(main-func)