refactor(cli): use environment variables instead of .config/cargo (#6230)

This commit is contained in:
Lucas Fernandes Nogueira 2023-02-09 08:11:02 -08:00 committed by GitHub
parent 894a8d060c
commit f2da21c7b4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 34 additions and 25 deletions

View File

@ -26,7 +26,7 @@ use tauri_mobile::{
use super::{
ensure_init, get_app,
init::{command as init_command, init_dot_cargo},
init::{command as init_command, configure_cargo},
log_finished, read_options, setup_dev_config, CliOptions, Target as MobileTarget,
MIN_DEVICE_MATCH_SCORE,
};

View File

@ -1,5 +1,5 @@
use super::{
delete_codegen_vars, ensure_init, env, init_dot_cargo, log_finished, open_and_wait, with_config,
configure_cargo, delete_codegen_vars, ensure_init, env, log_finished, open_and_wait, with_config,
MobileTarget,
};
use crate::{
@ -79,7 +79,7 @@ pub fn command(options: Options, noise_level: NoiseLevel) -> Result<()> {
ensure_init(config.project_dir(), MobileTarget::Android)?;
let mut env = env()?;
init_dot_cargo(app, Some((&env, config)))?;
configure_cargo(app, Some((&mut env, config)))?;
let open = options.open;
run_build(options, config, &mut env, noise_level)?;

View File

@ -1,5 +1,5 @@
use super::{
delete_codegen_vars, device_prompt, ensure_init, env, init_dot_cargo, open_and_wait,
configure_cargo, delete_codegen_vars, device_prompt, ensure_init, env, open_and_wait,
setup_dev_config, with_config, MobileTarget,
};
use crate::{
@ -98,7 +98,7 @@ fn run_dev(
noise_level: NoiseLevel,
) -> Result<()> {
setup_dev_config(&mut options.config)?;
let env = env()?;
let mut env = env()?;
let device = if options.open {
None
} else {
@ -131,7 +131,7 @@ fn run_dev(
let out_dir = bin_path.parent().unwrap();
let _lock = flock::open_rw(out_dir.join("lock").with_extension("android"), "Android")?;
init_dot_cargo(app, Some((&env, config)))?;
configure_cargo(app, Some((&mut env, config)))?;
// run an initial build to initialize plugins
interface.build(interface_options)?;

View File

@ -33,7 +33,27 @@ pub fn command(target: Target, ci: bool, reinstall_deps: bool) -> Result<()> {
Ok(())
}
pub fn init_dot_cargo(app: &App, android: Option<(&AndroidEnv, &AndroidConfig)>) -> Result<()> {
pub fn configure_cargo(
app: &App,
android: Option<(&mut AndroidEnv, &AndroidConfig)>,
) -> Result<()> {
if let Some((env, config)) = android {
for target in AndroidTarget::all().values() {
let config = target.generate_cargo_config(config, env)?;
let target_var_name = target.triple.replace('-', "_").to_uppercase();
if let Some(linker) = config.linker {
env.base.insert_env_var(
format!("CARGO_TARGET_{target_var_name}_LINKER"),
linker.into(),
);
}
env.base.insert_env_var(
format!("CARGO_TARGET_{target_var_name}_RUSTFLAGS"),
config.rustflags.join(" ").into(),
);
}
}
let mut dot_cargo = dot_cargo::DotCargo::load(app)?;
// Mysteriously, builds that don't specify `--target` seem to fight over
// the build cache with builds that use `--target`! This means that
@ -47,15 +67,6 @@ pub fn init_dot_cargo(app: &App, android: Option<(&AndroidEnv, &AndroidConfig)>)
// https://doc.rust-lang.org/cargo/reference/config.html#buildrustflags
dot_cargo.set_default_target(util::host_target_triple()?);
if let Some((env, config)) = android {
for target in AndroidTarget::all().values() {
dot_cargo.insert_target(
target.triple.to_owned(),
target.generate_cargo_config(config, env)?,
);
}
}
dot_cargo.write(app).map_err(Into::into)
}
@ -110,12 +121,11 @@ pub fn exec(
let app = match target {
// Generate Android Studio project
Target::Android => match AndroidEnv::new() {
Ok(env) => {
Ok(_env) => {
let (app, config, metadata) =
super::android::get_config(Some(app), tauri_config_, &Default::default());
map.insert("android", &config);
super::android::project::gen(&config, &metadata, (handlebars, map), wrapper)?;
init_dot_cargo(&app, Some((&env, &config)))?;
app
}
Err(err) => {
@ -125,7 +135,6 @@ pub fn exec(
err,
)
.print(wrapper);
init_dot_cargo(&app, None)?;
app
} else {
return Err(err.into());
@ -146,7 +155,6 @@ pub fn exec(
non_interactive,
reinstall_deps,
)?;
init_dot_cargo(&app, None)?;
app
}
};

View File

@ -24,7 +24,7 @@ use tauri_mobile::{
use super::{
ensure_init, env, get_app,
init::{command as init_command, init_dot_cargo},
init::{command as init_command, configure_cargo},
log_finished, read_options, setup_dev_config, CliOptions, Target as MobileTarget,
MIN_DEVICE_MATCH_SCORE,
};

View File

@ -1,5 +1,5 @@
use super::{
detect_target_ok, ensure_init, env, init_dot_cargo, log_finished, open_and_wait, with_config,
configure_cargo, detect_target_ok, ensure_init, env, log_finished, open_and_wait, with_config,
MobileTarget,
};
use crate::{
@ -71,7 +71,7 @@ pub fn command(options: Options, noise_level: NoiseLevel) -> Result<()> {
ensure_init(config.project_dir(), MobileTarget::Ios)?;
let mut env = env()?;
init_dot_cargo(app, None)?;
configure_cargo(app, None)?;
let open = options.open;
run_build(options, config, &mut env, noise_level)?;

View File

@ -1,5 +1,5 @@
use super::{
device_prompt, ensure_init, env, init_dot_cargo, open_and_wait, setup_dev_config, with_config,
configure_cargo, device_prompt, ensure_init, env, open_and_wait, setup_dev_config, with_config,
MobileTarget, APPLE_DEVELOPMENT_TEAM_ENV_VAR_NAME,
};
use crate::{
@ -142,7 +142,7 @@ fn run_dev(
let out_dir = bin_path.parent().unwrap();
let _lock = flock::open_rw(&out_dir.join("lock").with_extension("ios"), "iOS")?;
init_dot_cargo(app, None)?;
configure_cargo(app, None)?;
let open = options.open;
let exit_on_panic = options.exit_on_panic;

View File

@ -179,6 +179,7 @@ fn env_vars() -> HashMap<String, OsString> {
let k = k.to_string_lossy();
if (k.starts_with("TAURI") && k != "TAURI_PRIVATE_KEY" && k != "TAURI_KEY_PASSWORD")
|| k.starts_with("WRY")
|| k.starts_with("CARGO_")
|| k == "TMPDIR"
|| k == "PATH"
{