-
Notifications
You must be signed in to change notification settings - Fork 1
/
vab.v
281 lines (247 loc) · 7.11 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
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
// 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.util
import vab.android
import vab.android.sdk
import vab.android.ndk
import vab.android.env
fn main() {
mut args := arguments()
// NOTE: `run_vab_sub_command` executes first matching sub-command, if found; then calls `exit(...)`
cli.run_vab_sub_command(args)
// Get input to `vab`, and strip it from `args` if found.
mut input := ''
input, args = cli.input_from_args(args)
// Collect user flags precedented going from most implicit to most explicit.
// Start with defaults -> overwrite by .vab file entries -> overwrite by VAB_FLAGS -> overwrite by commandline flags.
mut opt := cli.Options{}
opt = cli.options_from_dot_vab(input, opt) or {
util.vab_error('Could not parse `.vab`', details: '${err}')
exit(1)
}
opt = cli.options_from_env(opt) or {
util.vab_error('Could not parse `VAB_FLAGS`', details: '${err}')
util.vab_notice('Use `${cli.exe_short_name} -h` to see all flags')
exit(1)
}
mut unmatched_args := []string{}
opt, unmatched_args = cli.options_from_arguments(args, opt) or {
util.vab_error('Could not parse `os.args`', details: '${err}')
util.vab_notice('Use `${cli.exe_short_name} -h` to see all flags')
exit(1)
}
if unmatched_args.len > 0 {
util.vab_error('Could not parse arguments', details: 'No matches for ${unmatched_args}')
util.vab_notice('Use `${cli.exe_short_name} -h` to see all flags')
exit(1)
}
$if vab_debug_options ? {
eprintln('--- ${@FN} ---')
dump(os.args)
dump(opt)
}
if opt.dump_version {
println('${cli.exe_short_name} ${cli.version_full()}')
exit(0)
}
if opt.dump_usage {
documentation := flag.to_doc[cli.Options](cli.vab_documentation_config) or {
util.vab_error('Could not generate usage documentation via `flag.to_doc[cli.Options](...)` this should not happen',
details: '${err}'
)
exit(1)
}
println(documentation)
exit(0)
}
if opt.list_ndks {
if !ndk.found() {
util.vab_error('No NDK could be found',
details: '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() {
util.vab_error('No SDK could be found',
details: '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() {
util.vab_error('No SDK could be found',
details: '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 {
util.vab_error('Could not get device list', details: '${err}')
exit(1)
}
println('Device IDs:\n')
println(devices.join('\n'))
exit(0)
}
// Call the doctor at this point
if opt.run_builtin_cmd == 'doctor' {
// Validate environment
cli.check_essentials(false)
opt.resolve(false)
cli.doctor(opt)
exit(0)
}
// NOTE: All flags after this requires an input argument,
// *except* doing one-off screenshots on a device:
if opt.screenshot != '' {
android.simple_screenshot(
verbosity: opt.verbosity
device_id: opt.device_id
path: opt.screenshot
delay: opt.screenshot_delay
) or {
util.vab_error('Failed to take screenshot', details: '${err}')
exit(1)
}
exit(0)
}
match opt.run_builtin_cmd {
'install' {
install_args := os.args[os.args.index(opt.run_builtin_cmd)..]
env.install_components(install_args, opt.verbosity) or {
util.vab_error('Failed to install components', details: '${err}')
exit(1)
}
exit(0)
}
'remove' {
remove_args := os.args[os.args.index(opt.run_builtin_cmd)..]
env.remove_components(remove_args, opt.verbosity) or {
util.vab_error('Failed to remove components', details: '${err}')
exit(1)
}
exit(0)
}
else {
if opt.run_builtin_cmd != '' && opt.run_builtin_cmd !in cli.subcmds_builtin {
util.vab_error('Unknown sub-command "${opt.run_builtin_cmd}"',
details: 'Accepted sub-commands are: ${cli.subcmds_builtin}'
)
exit(1)
}
}
}
// Validate environment
cli.check_essentials(true)
opt.resolve(true)
cli.validate_input(input) or {
suggestions := cli.input_suggestions(input)
if suggestions.len > 0 {
util.vab_error('${cli.exe_short_name}: ${err}',
details: 'Did you mean `${suggestions.join('` ,`')}`?'
)
} else {
util.vab_error('${cli.exe_short_name}: ${err}')
}
exit(1)
}
opt.input = input
opt.resolve_output()
// Validate environment after options and input has been resolved
opt.validate_env()
opt.ensure_launch_fields()
// Keystore file
keystore := opt.resolve_keystore() or {
util.vab_error('Could not resolve keystore', details: '${err}')
exit(1)
}
ado := opt.as_android_deploy_options() or {
util.vab_error('Could not create deploy options', details: '${err}')
exit(1)
}
deploy_opt := android.DeployOptions{
...ado
keystore: keystore
}
opt.verbose(2, '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 {
util.vab_error('Screenshot did not succeed', details: '${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 {
util.vab_error('Compiling did not succeed', details: '${err}')
exit(1)
}
apo := opt.as_android_package_options()
pck_opt := android.PackageOptions{
...apo
keystore: keystore
}
android.package(pck_opt) or {
util.vab_error('Packaging did not succeed', details: '${err}')
exit(1)
}
if deploy_opt.device_id != '' {
deploy(deploy_opt)
android.screenshot(screenshot_opt) or {
util.vab_error('Screenshot did not succeed', details: '${err}')
exit(1)
}
} else {
if opt.verbosity > 0 {
opt.verbose(1, 'Generated ${os.real_path(opt.output)}')
util.vab_notice('Use `${cli.exe_short_name} --device <id> ${os.real_path(opt.output)}` to deploy package')
util.vab_notice('Use `${cli.exe_short_name} --device <id> run ${os.real_path(opt.output)}` to both deploy and run the package')
if deploy_opt.run != '' {
util.vab_notice('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 {
util.vab_error('Deployment did not succeed', details: '${err}')
if deploy_opt.kill_adb {
cli.kill_adb()
}
exit(1)
}
deploy_opt.verbose(1, 'Deployed to ${deploy_opt.device_id} successfully')
if deploy_opt.kill_adb {
cli.kill_adb()
}
}