-
Notifications
You must be signed in to change notification settings - Fork 30
/
vab.v
238 lines (210 loc) · 5.67 KB
/
vab.v
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
// Copyright(C) 2019-2022 Lars Pontoppidan. All rights reserved.
// Use of this source code is governed by an MIT license file distributed with this software package
module main
import os
import flag
import vab.cli
import vab.android
import vab.android.sdk
import vab.android.ndk
import vab.android.env
fn main() {
// Collect user flags in an extended manner.
// Start with defaults -> overwrite by VAB_FLAGS -> overwrite by commandline flags -> extend by .vab file entries.
mut opt := cli.Options{}
mut fp := &flag.FlagParser(unsafe { nil })
opt = cli.options_from_env(opt) or {
eprintln('Error while parsing `VAB_FLAGS`: ${err}')
eprintln('Use `${cli.exe_short_name} -h` to see all flags')
exit(1)
}
opt, fp = cli.args_to_options(os.args, opt) or {
eprintln('Error while parsing `os.args`: ${err}')
eprintln('Use `${cli.exe_short_name} -h` to see all flags')
exit(1)
}
$if vab_debug_options ? {
eprintln(opt)
eprintln(vab_flags)
eprintln(os.args)
}
if opt.dump_usage {
println(fp.usage())
exit(0)
}
if opt.list_ndks {
if !ndk.found() {
eprintln('No NDK could be found. Please use `${cli.exe_short_name} doctor` to get more information.')
exit(1)
}
for ndk_v in ndk.versions_available() {
println(ndk_v)
}
exit(0)
}
if opt.list_apis {
if !sdk.found() {
eprintln('No SDK could be found. Please use `${cli.exe_short_name} doctor` to get more information.')
exit(1)
}
for api in sdk.apis_available() {
println(api)
}
exit(0)
}
if opt.list_build_tools {
if !sdk.found() {
eprintln('No SDK could be found. Please use `${cli.exe_short_name} doctor` to get more information.')
exit(1)
}
for btv in sdk.build_tools_available() {
println(btv)
}
exit(0)
}
if opt.list_devices {
devices := android.adb_get_device_list(opt.verbosity) or {
eprintln('Error getting device list: ${err}')
exit(1)
}
println('Device IDs:\n')
println(devices.join('\n'))
exit(0)
}
// All flags after this requires an input argument, except
// doing one-off screenshots on a device
if fp.args.len == 0 {
if opt.screenshot != '' {
android.simple_screenshot(
verbosity: opt.verbosity
device_id: opt.device_id
path: opt.screenshot
delay: opt.screenshot_delay
) or {
eprintln('Failed to take screenshot:\n${err}')
exit(1)
}
exit(0)
} else {
eprintln('No arguments given')
eprintln('Use `vab -h` to see all flags')
exit(1)
}
}
if opt.additional_args.len > 1 {
if opt.additional_args[0] == 'install' {
install_arg := opt.additional_args[1]
res := env.install(install_arg, opt.verbosity)
if res == 0 && opt.verbosity > 0 {
if install_arg != 'auto' {
println('Installed ${install_arg} successfully.')
} else {
println('Installed all dependencies successfully.')
}
}
exit(res)
}
}
// Call the doctor at this point
if opt.additional_args.len > 0 {
if opt.additional_args[0] == 'doctor' {
// Validate environment
cli.check_essentials(false)
opt.resolve(false)
cli.doctor(opt)
exit(0)
}
}
// Validate environment
cli.check_essentials(true)
opt.resolve(true)
input := fp.args.last()
cli.validate_input(input) or {
eprintln('${cli.exe_short_name}: ${err}')
exit(1)
}
opt.input = input
opt.resolve_output()
opt.extend_from_dot_vab()
// Validate environment after options and input has been resolved
opt.validate_env()
opt.ensure_launch_fields()
// Keystore file
keystore := opt.resolve_keystore() or {
eprintln('${cli.exe_short_name}: could not resolve keystore: ${err}')
exit(1)
}
ado := opt.as_android_deploy_options() or {
eprintln('Could not create deploy options.\n${err}')
exit(1)
}
deploy_opt := android.DeployOptions{
...ado
keystore: keystore
}
if opt.verbosity > 1 {
println('Output will be signed with keystore at "${deploy_opt.keystore.path}"')
}
screenshot_opt := opt.as_android_screenshot_options(deploy_opt)
input_ext := os.file_ext(opt.input)
// Early deployment of existing packages.
if input_ext in ['.apk', '.aab'] {
if deploy_opt.device_id != '' {
deploy(deploy_opt)
android.screenshot(screenshot_opt) or {
eprintln('${cli.exe_short_name} screenshot did not succeed.\n${err}')
exit(1)
}
exit(0)
}
}
aco := opt.as_android_compile_options()
comp_opt := android.CompileOptions{
...aco
cache_key: if os.is_dir(input) || input_ext == '.v' { opt.input } else { '' }
}
android.compile(comp_opt) or {
eprintln('${cli.exe_short_name} compiling didn\'t succeed.\n${err}')
exit(1)
}
apo := opt.as_android_package_options()
pck_opt := android.PackageOptions{
...apo
keystore: keystore
}
android.package(pck_opt) or {
eprintln("Packaging didn't succeed.\n${err}")
exit(1)
}
if deploy_opt.device_id != '' {
deploy(deploy_opt)
android.screenshot(screenshot_opt) or {
eprintln('${cli.exe_short_name} screenshot did not succeed.\n${err}')
exit(1)
}
} else {
if opt.verbosity > 0 {
println('Generated ${os.real_path(opt.output)}')
println('Use `${cli.exe_short_name} --device <id> ${os.real_path(opt.output)}` to deploy package')
println('Use `${cli.exe_short_name} --device <id> run ${os.real_path(opt.output)}` to both deploy and run the package')
if deploy_opt.run != '' {
println('Use `adb -s "<DEVICE ID>" shell am start -n "${deploy_opt.run}"` to run the app on the device, via adb')
}
}
}
}
fn deploy(deploy_opt android.DeployOptions) {
android.deploy(deploy_opt) or {
eprintln('${cli.exe_short_name} deployment didn\'t succeed.\n${err}')
if deploy_opt.kill_adb {
cli.kill_adb()
}
exit(1)
}
if deploy_opt.verbosity > 0 {
println('Deployed to ${deploy_opt.device_id} successfully')
}
if deploy_opt.kill_adb {
cli.kill_adb()
}
}