-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbuild.clj
339 lines (277 loc) · 10.1 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
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
(ns build
(:require [clojure.tools.build.api :as b]
[clojure.string :as str]
[clojure.java.io :as io]
[clojure.java.shell]
[clojure.data.xml :as xml]
[me.raynes.fs :as fs]
[clojure.edn :as edn]
[digest]
)
(:import [java.nio.file Path Paths]
[java.io FileOutputStream]
[com.dropbox.core DbxRequestConfig]
[com.dropbox.core.v2 DbxClientV2]
[com.dropbox.core.v2.files WriteMode])
)
(def project 'gd-edit)
(def lib 'gd-edit/core)
(def version (format "0.2.%s" (b/git-count-revs nil)))
(def class-dir "target/classes")
(def basis (b/create-basis {:project "deps.edn"}))
(def uber-file (format "target/%s-standalone.jar" (name project) version))
(defn shell
[cmd-str]
(apply clojure.java.shell/sh (str/split cmd-str #" ")))
(defn shell-stream
[cmd-str]
(.waitFor (-> (ProcessBuilder. (str/split cmd-str #" ")) .inheritIO .start)))
(defn git-sha
[]
(str/trim ((shell "git rev-parse --short HEAD") :out)))
(defn git-tag
[]
(str/trim ((shell "git describe --abbrev=0 --tags HEAD") :out))
)
(defn git-branch
[]
(str/trim ((shell "git rev-parse --abbrev-ref HEAD") :out)))
(defn git-has-uncommitted-changes
[]
(if (= (:exit (shell "git diff-index --quiet HEAD --")) 1)
true
false))
(defn make-build-info
([]
(make-build-info {}))
([kv]
(merge
{:app-name project
:version version
:sha (git-sha)
:tag (git-tag)
:branch (git-branch)
:timestamp (clojure.instant/read-instant-timestamp ((shell "git log -1 --pretty=format:%cd --date=iso-strict") :out))}
kv)))
(defn- to-four-places
[version]
(let [places (str/split version #"\.")]
(try
(doall (map #(Integer/parseInt %) places))
(if (> (count places) 4) (throw (NumberFormatException.)))
(catch NumberFormatException e
(throw (Exception. "Version string must consist of up to four numbers, separated by periods."))))
(str/join "." (concat places (repeat (- 4 (count places)) "0")))))
(defn- filename-without-path
[file]
(let [path (Paths/get (.getPath file) (into-array [""]))]
(str (.getFileName path))))
(defn launch4j-config
[{:keys [out-file jar-file main-class
project-name description version copyright
jvm-opts jre-path]}]
(xml/sexp-as-element
[:launch4jConfig
[:headerType "console"]
[:outfile (.getAbsolutePath out-file)]
[:jar (.getAbsolutePath jar-file)]
[:classPath
[:mainClass main-class]]
(into [:jre
(if jre-path
[:path jre-path]
[:path])
[:minVersion "1.7.0"]
[:maxHeapSize "2048"]
[:jdkPreference "preferJdk"]]
(for [jvm-opt jvm-opts]
[:opt jvm-opt]))
[:versionInfo
[:fileVersion (to-four-places version)]
[:txtFileVersion version]
[:fileDescription description]
[:copyright copyright]
[:productVersion (to-four-places version)]
[:txtProductVersion version]
[:productName project-name]
[:internalName project-name]
[:originalFilename (filename-without-path out-file)]]]))
(defn write-launch4j-config
[opts file-writer]
(xml/emit (launch4j-config opts) file-writer))
(defn build-exe
[{:keys [uber-file project-name main description version copyright jvm-opt jre-path]}]
(let [jar (b/resolve-path uber-file)
tmp (fs/temp-dir "gd-edit-build-exe")
fname (->> (.getName jar)
(re-matches #"(.+)\.jar")
second)
xml-fname (str fname ".xml")
exe-fname (str fname ".exe")
xml-file (io/file tmp xml-fname)
out-file (io/file (.getParent (io/file uber-file)) exe-fname)
]
(with-open [xml (io/writer xml-file :encoding "UTF-8")]
(write-launch4j-config {:jar-file jar
:out-file out-file
:main-class main
:project-name project-name
:description description
:version version
:copyright copyright
:jvm-opts (or jvm-opt #{})
:jre-path jre-path}
xml))
(shell-stream (format "launch4j %s" (.getPath xml-file)))
out-file))
(defn- bin-header
[jvm-opts]
(format "#!/bin/sh\n\nexec java %s -jar $0 \"$@\"\n\n\n"
(str/join \space jvm-opts)))
(defn build-bin
[{:keys [uber-file project-name main description version copyright jvm-opt jre-path header]}]
(let [jar (b/resolve-path uber-file)
tmp (fs/temp-dir "gd-edit-build-bin")
header (if header
(slurp header)
(bin-header (or jvm-opt #{})))
bin-fname (->> (.getName jar)
(re-matches #"(.+)\.jar")
second)
tgt-file (io/file (.getParent (io/file uber-file)) bin-fname)
]
(println (format "Creating %s binary..." bin-fname))
(with-open [bin (FileOutputStream. tgt-file)]
(io/copy header bin)
(io/copy jar bin))
(.setExecutable tgt-file true false)
tgt-file))
(comment
(build-exe {:uber-file uber-file
:main 'gd_edit.core
:jvm-opt #{"-Xms128m" "-Djna.nosys=true"}
:project-name project
:description "GrimDawn save game editor"
:copyright "2022"
:version version
}
)
(build-bin {:uber-file uber-file
:main 'gd_edit.core
:jvm-opt #{"-Xms128m" "-Djna.nosys=true"}
:project-name project
:description "GrimDawn save game editor"
:copyright "2022"
:version version
}
)
)
(def build-stage-atom (atom {:count 0}))
(defn print-build-stage
[text]
(swap! build-stage-atom update :count inc)
(println (format "[%d] %s" (:count @build-stage-atom) text)))
(defn clean
[_]
(print-build-stage "Cleaning old build files...")
(b/delete {:path "target"}))
(defn uber
[_]
(clean nil)
;; Pack build info as "build.edn" with the jar
(print-build-stage "Creating build.edn...")
(b/write-file {:path (format "%s/build.edn" class-dir)
:content (make-build-info)})
(print-build-stage "Copying sources...")
(b/copy-dir {:src-dirs ["src" "resources"]
:target-dir class-dir})
(print-build-stage "Compilng sources...")
(b/compile-clj {:basis basis
:src-dirs ["src"]
:class-dir class-dir})
(print-build-stage "Building uberjar...")
(b/uber {:class-dir class-dir
:uber-file uber-file
:basis basis
:main 'gd-edit.core})
)
(defn build
[_]
(uber {})
(let [settings {:uber-file uber-file
:main 'gd_edit.core
:jvm-opt #{"-Xms128m" "-Djna.nosys=true"}
:project-name project
:description "GrimDawn save game editor"
:copyright "2022"
:version version}
_ (print-build-stage "Building nix binary...")
bin-file (build-bin settings)
_ (print-build-stage "Building exe...")
exe-file (build-exe settings)]
{:bin-file bin-file
:exe-file exe-file}))
(defn make-dropbox-client
[]
(let [config (DbxRequestConfig. "GDUploader/0.1" "en_US")
dropbox-setting-file (edn/read-string (slurp (io/file ".publish.edn")))]
(DbxClientV2. config (:token dropbox-setting-file))))
(defn replace-path-extension
[path new-ext]
(as-> (io/file path) $
(fs/base-name $ true)
(io/file (fs/parent path) $)
(str $ new-ext)))
(defn replace-base-name-extension
[basename new-ext]
(-> (io/file basename)
(fs/base-name true)
(str new-ext)))
(defn upload-bin-edn-pair-to-dropbox
[dropbox-client bin-file upload-filename]
(let [edn-base-name (replace-base-name-extension upload-filename ".edn")]
;; Write the gd-editor.exe file
(println (format "Uploading %s build..." upload-filename))
(with-open [exe-stream (io/input-stream bin-file)]
(-> dropbox-client
(.files)
(.uploadBuilder (str (io/file "/Public/GrimDawn/editor/" upload-filename)))
(.withMode WriteMode/OVERWRITE)
(.uploadAndFinish exe-stream)))
;; Write the gd-editor.edn file to describe the latest version
(println (format "Uploading %s edn file..." upload-filename))
(-> dropbox-client
(.files)
(.uploadBuilder (str (io/file "/Public/GrimDawn/editor/" edn-base-name)))
(.withMode WriteMode/OVERWRITE)
(.uploadAndFinish (-> (make-build-info {:filesize (.length bin-file)
:file-sha1 (digest/sha1 bin-file)})
(pr-str)
(.getBytes)
(io/input-stream))))))
(defn publish
"Publish the built windows exe to dropbox"
[_]
(when (git-has-uncommitted-changes)
(println "Please don't publish using uncommitted changes.\nThis makes the build info useless for determining what the user is running.")
(throw (Throwable. "Should not publish uncommitted changes")))
(println "Publishing exe to dropbox...")
(let [{:keys [bin-file exe-file]} (build {})]
(if-not (and (.exists bin-file)
(.exists exe-file))
(println "Not all the files were properly built... aboring")
(let [client (make-dropbox-client)]
;; Upload the windows exe
(upload-bin-edn-pair-to-dropbox client
exe-file
"gd-edit.exe")
;; Upload wrapped binary suitable for platforms
(upload-bin-edn-pair-to-dropbox client
;; (io/file (replace-path-extension output-file ""))
bin-file
"gd-edit.nix.bin")))))
(comment
(uber {})
(build {})
(publish {})
)