/
dev.rs
266 lines (245 loc) · 7.25 KB
/
dev.rs
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
// Copyright 2019-2023 Tauri Programme within The Commons Conservancy
// SPDX-License-Identifier: Apache-2.0
// SPDX-License-Identifier: MIT
use super::{
configure_cargo, delete_codegen_vars, device_prompt, ensure_init, env, inject_assets,
open_and_wait, setup_dev_config, with_config, MobileTarget,
};
use crate::{
dev::Options as DevOptions,
helpers::{config::get as get_config, flock},
interface::{AppSettings, Interface, MobileOptions, Options as InterfaceOptions},
mobile::{write_options, CliOptions, DevChild, DevProcess},
Result,
};
use clap::{ArgAction, Parser};
use tauri_mobile::{
android::{
config::{Config as AndroidConfig, Metadata as AndroidMetadata},
device::Device,
env::Env,
target::Target,
},
config::app::App,
opts::{FilterLevel, NoiseLevel, Profile},
target::TargetTrait,
};
use std::env::set_var;
const WEBVIEW_CLIENT_CLASS_EXTENSION: &str = "
@android.annotation.SuppressLint(\"WebViewClientOnReceivedSslError\")
override fun onReceivedSslError(view: WebView?, handler: SslErrorHandler, error: android.net.http.SslError) {
handler.proceed()
}
";
const WEBVIEW_CLASS_INIT: &str =
"this.settings.mixedContentMode = android.webkit.WebSettings.MIXED_CONTENT_ALWAYS_ALLOW";
#[derive(Debug, Clone, Parser)]
#[clap(about = "Android dev")]
pub struct Options {
/// List of cargo features to activate
#[clap(short, long, action = ArgAction::Append, num_args(0..))]
pub features: Option<Vec<String>>,
/// Exit on panic
#[clap(short, long)]
exit_on_panic: bool,
/// JSON string or path to JSON file to merge with tauri.conf.json
#[clap(short, long)]
pub config: Option<String>,
/// Disable the file watcher
#[clap(long)]
pub no_watch: bool,
/// Disable the dev server for static files.
#[clap(long)]
pub no_dev_server: bool,
/// Open Android Studio instead of trying to run on a connected device
#[clap(short, long)]
pub open: bool,
/// Runs on the given device name
pub device: Option<String>,
/// Specify port for the dev server for static files. Defaults to 1430
/// Can also be set using `TAURI_DEV_SERVER_PORT` env var.
#[clap(long)]
pub port: Option<u16>,
/// Force prompting for an IP to use to connect to the dev server on mobile.
#[clap(long)]
pub force_ip_prompt: bool,
/// Run the code in release mode
#[clap(long = "release")]
pub release_mode: bool,
}
impl From<Options> for DevOptions {
fn from(options: Options) -> Self {
Self {
runner: None,
target: None,
features: options.features,
exit_on_panic: options.exit_on_panic,
config: options.config,
args: Vec::new(),
no_watch: options.no_watch,
no_dev_server: options.no_dev_server,
port: options.port,
force_ip_prompt: options.force_ip_prompt,
release_mode: options.release_mode,
}
}
}
pub fn command(options: Options, noise_level: NoiseLevel) -> Result<()> {
delete_codegen_vars();
with_config(
Some(Default::default()),
|app, config, metadata, _cli_options| {
set_var(
"WRY_RUSTWEBVIEWCLIENT_CLASS_EXTENSION",
WEBVIEW_CLIENT_CLASS_EXTENSION,
);
set_var("WRY_RUSTWEBVIEW_CLASS_INIT", WEBVIEW_CLASS_INIT);
ensure_init(config.project_dir(), MobileTarget::Android)?;
run_dev(options, app, config, metadata, noise_level).map_err(Into::into)
},
)
.map_err(Into::into)
}
fn run_dev(
mut options: Options,
app: &App,
config: &AndroidConfig,
metadata: &AndroidMetadata,
noise_level: NoiseLevel,
) -> Result<()> {
setup_dev_config(&mut options.config, options.force_ip_prompt)?;
let mut env = env()?;
let device = if options.open {
None
} else {
match device_prompt(&env, options.device.as_deref()) {
Ok(d) => Some(d),
Err(e) => {
log::error!("{e}");
None
}
}
};
let mut dev_options: DevOptions = options.clone().into();
let target_triple = device
.as_ref()
.map(|d| d.target().triple.to_string())
.unwrap_or_else(|| Target::all().values().next().unwrap().triple.into());
dev_options.target = Some(target_triple.clone());
let mut interface = crate::dev::setup(&mut dev_options, true)?;
let interface_options = InterfaceOptions {
debug: !dev_options.release_mode,
target: dev_options.target.clone(),
..Default::default()
};
let app_settings = interface.app_settings();
let bin_path = app_settings.app_binary_path(&interface_options)?;
let out_dir = bin_path.parent().unwrap();
let _lock = flock::open_rw(out_dir.join("lock").with_extension("android"), "Android")?;
configure_cargo(app, Some((&mut env, config)))?;
// run an initial build to initialize plugins
let target = Target::all()
.values()
.find(|t| t.triple == target_triple)
.unwrap_or_else(|| Target::all().values().next().unwrap());
target.build(
config,
metadata,
&env,
noise_level,
true,
if options.release_mode {
Profile::Release
} else {
Profile::Debug
},
)?;
let open = options.open;
let exit_on_panic = options.exit_on_panic;
let no_watch = options.no_watch;
interface.mobile_dev(
MobileOptions {
debug: !options.release_mode,
features: options.features,
args: Vec::new(),
config: options.config,
no_watch: options.no_watch,
},
|options| {
let cli_options = CliOptions {
features: options.features.clone(),
args: options.args.clone(),
noise_level,
vars: Default::default(),
};
let tauri_config = get_config(options.config.as_deref())?;
let _handle = write_options(
&tauri_config
.lock()
.unwrap()
.as_ref()
.unwrap()
.tauri
.bundle
.identifier,
cli_options,
)?;
inject_assets(config, tauri_config.lock().unwrap().as_ref().unwrap())?;
if open {
open_and_wait(config, &env)
} else if let Some(device) = &device {
match run(device, options, config, &env, metadata, noise_level) {
Ok(c) => {
crate::dev::wait_dev_process(c.clone(), move |status, reason| {
crate::dev::on_app_exit(status, reason, exit_on_panic, no_watch)
});
Ok(Box::new(c) as Box<dyn DevProcess>)
}
Err(e) => {
crate::dev::kill_before_dev_process();
Err(e.into())
}
}
} else {
open_and_wait(config, &env)
}
},
)
}
#[derive(Debug, thiserror::Error)]
enum RunError {
#[error("{0}")]
RunFailed(String),
}
fn run(
device: &Device<'_>,
options: MobileOptions,
config: &AndroidConfig,
env: &Env,
metadata: &AndroidMetadata,
noise_level: NoiseLevel,
) -> Result<DevChild, RunError> {
let profile = if options.debug {
Profile::Debug
} else {
Profile::Release
};
let build_app_bundle = metadata.asset_packs().is_some();
device
.run(
config,
env,
noise_level,
profile,
Some(match noise_level {
NoiseLevel::Polite => FilterLevel::Info,
NoiseLevel::LoudAndProud => FilterLevel::Debug,
NoiseLevel::FranklyQuitePedantic => FilterLevel::Verbose,
}),
build_app_bundle,
false,
".MainActivity".into(),
)
.map(DevChild::new)
.map_err(|e| RunError::RunFailed(e.to_string()))
}