This repository has been archived by the owner on Apr 25, 2024. It is now read-only.
/
utils.clj
380 lines (339 loc) · 14.6 KB
/
utils.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
;; Provides utilities for the plugin.
;;
(ns leiningen.droid.utils
(:require [leiningen.core.project :as pr]
[robert.hooke :refer [with-hooks-disabled]])
(:use [clojure.java.io :only (file reader)]
[leiningen.core.main :only (info debug abort *debug*)]
[leiningen.core.classpath :only [resolve-dependencies]]
[clojure.string :only (join)])
(:import [java.io File StringWriter]))
;; #### Convenient functions to run SDK binaries
(defmacro ensure-paths
"Checks if the given directories or files exist. Aborts Leiningen
execution in case either of them doesn't or the value equals nil.
We assume paths to be strings or lists/vectors. The latter case is
used exclusively for Windows batch-files which are represented like
`cmd.exe /C batch-file`, so we test third element of the list for
the existence."
[& paths]
`(do
~@(for [p paths]
`(cond (nil? ~p)
(abort "The value of" (str '~p) "is nil. Abort execution.")
(or
(and (sequential? ~p) (not (.exists (file (nth ~p 2)))))
(and (string? ~p) (not (.exists (file ~p)))))
(abort "The path" ~p "doesn't exist. Abort execution.")))))
(defn windows?
"Returns true if we are running on Microsoft Windows"
[]
(= java.io.File/separator "\\"))
(defn sdk-binary-paths
"Returns a map of relative paths to different SDK binaries for both
Unix and Windows platforms."
[sdk-path build-tools-version]
(ensure-paths sdk-path)
(let [bt-root-dir (file sdk-path "build-tools")
;; build-tools directory contains a subdir which name we don't
;; know that has all the tools. Let's grab the last directory
;; inside build-tools/ and hope it is the one we need.
bt-dir (or build-tools-version
(->> (.list bt-root-dir)
(filter #(.isDirectory (file bt-root-dir %)))
sort last)
(abort "Build tools not found."
"Download them using the Android SDK Manager."))]
{:dx {:unix ["build-tools" bt-dir "dx"]
:win ["build-tools" bt-dir "dx.bat"]}
:adb {:unix ["platform-tools" "adb"]
:win ["platform-tools" "adb.exe"]}
:aapt {:unix ["build-tools" bt-dir "aapt"]
:win ["build-tools" bt-dir "aapt.exe"]}
:zipalign {:unix ["build-tools" bt-dir "zipalign"]
:win ["build-tools" bt-dir "zipalign.exe"]}
:proguard {:unix ["tools" "proguard" "bin" "proguard.sh"]
:win ["tools" "proguard" "bin" "proguard.bat"]}}))
(defn sdk-binary
"Given the project map and the binary keyword, returns either a full
path to the binary as a string, or a vector with call to cmd.exe for
batch-files."
[{{:keys [sdk-path build-tools-version]} :android} binary-kw]
(let [binary (get-in (sdk-binary-paths sdk-path build-tools-version)
[binary-kw (if (windows?) :win :unix)])
binary-str (str (apply file sdk-path binary))]
(ensure-paths binary-str)
(if (.endsWith (last binary) ".bat")
["cmd.exe" "/C" binary-str]
binary-str)))
;; ### Middleware section
(defn absolutize
"Taken from Leiningen source code.
Absolutizes the `path` given `root` if it is relative. Leaves the
path as is if it is absolute."
[root path]
(str (if (.isAbsolute (file path))
path
(file root path))))
(defn absolutize-android-paths
"Taken from Leiningen source code.
Absolutizes all values with keys ending with `path` or `paths` in
the `:android` map of the project."
[{:keys [root android] :as project}]
(assoc project :android
(into {} (for [[key val] android]
[key (cond (re-find #"-path$" (name key))
(absolutize root val)
(re-find #"-paths$" (name key))
(map (partial absolutize root) val)
:else val)]))))
(defn get-default-android-params
"Returns a map of the default android-specific parameters."
[{root :root, name :name, target-path :target-path
java-paths :java-source-paths}]
(let [manifest-template "AndroidManifest.template.xml"
manifest-template-file (file (absolutize root manifest-template))
gen-path (str (file target-path "gen"))
has-template (.exists manifest-template-file)]
{:out-dex-path target-path
:proguard-execute false
:proguard-conf-path "proguard.conf"
:proguard-output-jar-path (str (file target-path "mininified-classes.jar"))
:manifest-path (if has-template
(str (file target-path "AndroidManifest.xml"))
"AndroidManifest.xml")
:manifest-template-path manifest-template
:manifest-options {:app-name "@string/app_name"}
:res-path "res"
:gen-path gen-path
:out-res-path (str (file target-path "res"))
:assets-paths ["assets"]
:assets-gen-path (str (file target-path "assets-gen"))
:out-res-pkg-path (str (file target-path (str name ".ap_")))
:out-apk-path (str (file target-path (str name ".apk")))
:keystore-path (str (file (System/getProperty "user.home")
".android" "debug.keystore"))
:key-alias "androiddebugkey"
:repl-device-port 9999
:repl-local-port 9999
:target-version 10}))
(declare android-parameters)
(defn read-project
"Reads and initializes a Leiningen project and applies Android
middleware to it."
[project-file]
(android-parameters (pr/init-project (pr/read (str project-file)))))
(defn get-project-file
"Returns the path to project.clj file in the specified project
directory (either absolute or relative)."
[root project-directory-path]
(let [project-directory (file project-directory-path)]
(if (.isAbsolute project-directory)
(file project-directory-path "project.clj")
(file root project-directory-path "project.clj"))))
(defn process-project-dependencies
"Parses `project.clj` files from the project dependencies to extract
the paths to external resources and class files."
[{{:keys [project-dependencies]} :android, root :root :as project}]
(reduce (fn [project dependency-path]
(let [project-file (get-project-file root dependency-path)]
(if-not (.exists ^File project-file)
(do
(info "WARNING:" (str project-file) "doesn't exist.")
project)
(let [dep (read-project project-file)
{:keys [compile-path dependencies]} dep
{:keys [res-path out-res-path]} (:android dep)]
(-> project
(update-in [:dependencies]
concat dependencies)
(update-in [:android :external-classes-paths]
conj compile-path)
(update-in [:android :external-res-paths]
conj res-path out-res-path))))))
project project-dependencies))
;; This is the middleware function to be plugged into project.clj.
(defn android-parameters
"Merges project's `:android` map with the default parameters map,
processes project dependencies and absolutizes paths in the
`:android` map."
[{:keys [android] :as project}]
(let [android-params (merge (get-default-android-params project)
android)]
(-> project
(assoc :android android-params)
process-project-dependencies
absolutize-android-paths)))
;; ### General utilities
(defn proj [] (read-project "sample/project.clj"))
(defn sdk-version-number
"If version keyword is passed (for example, `:ics` or `:jelly-bean`), resolves
it to the version number. Otherwise just returns the input."
[kw-or-number]
(if (keyword? kw-or-number)
(case kw-or-number
:ics 15
:jelly-bean 18
:kitkat 19
:lollipop 21
(abort "Unknown Android SDK version: " kw-or-number))
kw-or-number))
(defn get-sdk-platform-path
"Returns a version-specific path to the Android platform tools."
[sdk-root version]
(str (file sdk-root "platforms" (str "android-"
(sdk-version-number version)))))
(defn get-sdk-android-jar
"Returns a version-specific path to the `android.jar` SDK file."
([{{:keys [sdk-path target-version]} :android :as project}]
(get-sdk-android-jar sdk-path target-version))
([sdk-root version]
(str (file (get-sdk-platform-path sdk-root version) "android.jar"))))
(defn get-sdk-google-api-path
"Returns a version-specific path to the Google SDK directory."
[sdk-root version]
(str (file sdk-root "add-ons" (str "addon-google_apis-google-"
(sdk-version-number version)))))
(defn get-sdk-google-api-jars
"Returns a version-specific paths to all Google SDK jars."
[sdk-root version]
(map #(.getAbsolutePath ^File %)
(rest ;; The first file is the directory itself, no need in it.
(file-seq
(file (str (get-sdk-google-api-path sdk-root version) "/libs"))))))
(defn- get-sdk-support-jar
"Returns a path to the Android Support library."
[sdk-root version]
(.getAbsolutePath
(apply file sdk-root "extras" "android" "support"
(case version
"v4" ["v4" "android-support-v4.jar"]
"v7-appcompat" ["v7" "appcompat" "libs"
"android-support-v7-appcompat.jar"]
"v7-gridlayout" ["v7" "gridlayout" "libs"
"android-support-v7-gridlayout.jar"]
"v7-mediarouter" ["v7" "mediarouter" "libs"
"android-support-v7-mediarouter.jar"]
"v13" ["v13" "android-support-v13.jar"]
(abort "Unknown support library version in :support-libraries : "
version)))))
(defn get-sdk-support-jars
"Takes a list of support library versions and returns a list of JAR
files."
[sdk-root version-list & [warn?]]
(let [message "WARNING: Support library V4 is redundant if you use V13."
versions (set version-list)
versions (if (every? versions ["v4" "v13"])
(do (when warn? (info message))
(disj versions "v4"))
versions)]
(map #(get-sdk-support-jar sdk-root %) (seq versions))))
(defn get-resource-jars
"Get the list of dependency libraries that has `:use-resources true`
in their definition."
[{:keys [dependencies] :as project}]
(let [res-deps (for [[lib _ & options :as dep] (:dependencies project)
:when (or (:use-resources (apply hash-map options))
;; Should be removed in final release
(= lib 'org.clojure-android/clojure))]
dep)
mod-proj (assoc project :dependencies res-deps)]
(with-hooks-disabled resolve-dependencies
(resolve-dependencies :dependencies mod-proj))))
(def ^:dynamic *sh-print-output*
"If true, print the output of the shell command regardless of *debug*."
false)
(defmacro with-process
"Executes the subprocess specified in the binding list and applies
`body` do it while it is running. The binding list consists of a var
name for the process and the list of strings that represents shell
command.
After body is executed waits for a subprocess to finish, then checks
the exit code. If code is not zero then prints the subprocess'
output. If in DEBUG mode print both the command and it's output even
for the successful run."
[[process-name command] & body]
`(do
(apply debug ~command)
(let [builder# (ProcessBuilder. ~command)
_# (.redirectErrorStream builder# true)
~process-name (.start builder#)
output# (line-seq (reader (.getInputStream ~process-name)))
out-stream# (StringWriter.)
print-output?# (or *debug* *sh-print-output*)]
~@body
(doseq [line# output#]
(if print-output?#
(info line#)
(binding [*out* out-stream#]
(println line#))))
(.waitFor ~process-name)
(when-not (and (= (.exitValue ~process-name) 0)
(not print-output?#))
(info (str out-stream#)))
(when-not (= (.exitValue ~process-name) 0)
(abort "Abort execution."))
output#)))
(defn sh
"Executes the command given by `args` in a subprocess. Flattens the
given list."
[& args]
(with-process [process (flatten args)]))
(defn dev-build?
"Checks the build type of the current project, assuming dev build if
not a release build"
[project]
(not= (get-in project [:android :build-type]) :release))
(defn wrong-usage
"Returns a string with the information about the proper function usage."
([task-name function-var]
(wrong-usage task-name function-var 0))
([task-name function-var arglist-number]
(let [arglist (-> function-var
meta :arglists (nth arglist-number))
argcount (count arglist)
parametrify #(str "<" % ">")
;; Replace the destructuring construction after & with
;; [optional-args].
arglist (if (= (nth arglist (- argcount 2)) '&)
(concat (map parametrify
(take (- argcount 2) arglist))
["[optional-args]"])
(map parametrify arglist))]
(format "Wrong number of argumets. USAGE: %s %s"
task-name (join (interpose " " arglist))))))
(defn prompt-user
"Reads a string from the console until the newline character."
[prompt]
(print prompt)
(flush)
(read-line))
(defn read-password
"Reads the password from the console without echoing the
characters."
[prompt]
(if-let [console (System/console)]
(join (.readPassword console prompt nil))
(prompt-user prompt)))
(defn append-suffix
"Appends a suffix to a filename, e.g. transforming `sample.apk` into
`sample-signed.apk`"
[filename suffix]
(let [[_ without-ext ext] (re-find #"(.+)(\.\w+)" filename)]
(str without-ext "-" suffix ext)))
(defn create-debug-keystore
"Creates a keystore for signing debug APK files."
[keystore-path]
(sh "keytool" "-genkey" "-v"
"-keystore" keystore-path
"-alias" "androiddebugkey"
"-sigalg" "SHA1withRSA"
"-keyalg" "RSA"
"-keysize" "1024"
"-validity" "365"
"-keypass" "android"
"-storepass" "android"
"-dname" "CN=Android Debug,O=Android,C=US"))
(defn relativize-path [^File dir ^File to-relativize]
(.getPath (.relativize (.toURI dir)
(.toURI to-relativize))))