From 894a8d060c12a482a0fc5b3714f3848189b809de Mon Sep 17 00:00:00 2001 From: Lucas Fernandes Nogueira Date: Wed, 8 Feb 2023 18:19:34 -0800 Subject: [PATCH] refactor(cli): use temp file to communicate IPC websocket address (#6219) --- .changes/cli-refactor-ipc-mobile.md | 6 ++++++ tooling/cli/src/mobile/android.rs | 3 ++- tooling/cli/src/mobile/android/build.rs | 14 ++++++++++++-- tooling/cli/src/mobile/android/dev.rs | 15 ++++++++++++--- tooling/cli/src/mobile/ios.rs | 3 ++- tooling/cli/src/mobile/ios/build.rs | 14 ++++++++++++-- tooling/cli/src/mobile/ios/dev.rs | 15 ++++++++++++--- tooling/cli/src/mobile/mod.rs | 17 ++++++++++------- 8 files changed, 68 insertions(+), 19 deletions(-) create mode 100644 .changes/cli-refactor-ipc-mobile.md diff --git a/.changes/cli-refactor-ipc-mobile.md b/.changes/cli-refactor-ipc-mobile.md new file mode 100644 index 000000000..dd3ede2c8 --- /dev/null +++ b/.changes/cli-refactor-ipc-mobile.md @@ -0,0 +1,6 @@ +--- +"cli.rs": patch +"cli.js": patch +--- + +Use temp file instead of environment variable to pass CLI IPC websocket address to the IDE. diff --git a/tooling/cli/src/mobile/android.rs b/tooling/cli/src/mobile/android.rs index 085c9b9f6..2284fde41 100644 --- a/tooling/cli/src/mobile/android.rs +++ b/tooling/cli/src/mobile/android.rs @@ -161,7 +161,8 @@ pub fn with_config( let tauri_config = get_tauri_config(None)?; let tauri_config_guard = tauri_config.lock().unwrap(); let tauri_config_ = tauri_config_guard.as_ref().unwrap(); - let cli_options = cli_options.unwrap_or_else(read_options); + let cli_options = + cli_options.unwrap_or_else(|| read_options(&tauri_config_.tauri.bundle.identifier)); let (app, config, metadata) = get_config(None, tauri_config_, &cli_options); (app, config, metadata, cli_options) }; diff --git a/tooling/cli/src/mobile/android/build.rs b/tooling/cli/src/mobile/android/build.rs index ed31548d2..e475e96fe 100644 --- a/tooling/cli/src/mobile/android/build.rs +++ b/tooling/cli/src/mobile/android/build.rs @@ -4,7 +4,7 @@ use super::{ }; use crate::{ build::Options as BuildOptions, - helpers::flock, + helpers::{config::get as get_config, flock}, interface::{AppSettings, Interface, Options as InterfaceOptions}, mobile::{write_options, CliOptions}, Result, @@ -139,7 +139,17 @@ fn run_build( noise_level, vars: Default::default(), }; - let _handle = write_options(cli_options, &mut env.base)?; + let _handle = write_options( + &get_config(options.config.as_deref())? + .lock() + .unwrap() + .as_ref() + .unwrap() + .tauri + .bundle + .identifier, + cli_options, + )?; options .features diff --git a/tooling/cli/src/mobile/android/dev.rs b/tooling/cli/src/mobile/android/dev.rs index 897db7fa5..402d8d2cd 100644 --- a/tooling/cli/src/mobile/android/dev.rs +++ b/tooling/cli/src/mobile/android/dev.rs @@ -4,7 +4,7 @@ use super::{ }; use crate::{ dev::Options as DevOptions, - helpers::flock, + helpers::{config::get as get_config, flock}, interface::{AppSettings, Interface, MobileOptions, Options as InterfaceOptions}, mobile::{write_options, CliOptions, DevChild, DevProcess}, Result, @@ -148,14 +148,23 @@ fn run_dev( no_watch: options.no_watch, }, |options| { - let mut env = env.clone(); let cli_options = CliOptions { features: options.features.clone(), args: options.args.clone(), noise_level, vars: Default::default(), }; - let _handle = write_options(cli_options, &mut env.base)?; + let _handle = write_options( + &get_config(options.config.as_deref())? + .lock() + .unwrap() + .as_ref() + .unwrap() + .tauri + .bundle + .identifier, + cli_options, + )?; if open { open_and_wait(config, &env) diff --git a/tooling/cli/src/mobile/ios.rs b/tooling/cli/src/mobile/ios.rs index 33dc9751d..aed2ec3e3 100644 --- a/tooling/cli/src/mobile/ios.rs +++ b/tooling/cli/src/mobile/ios.rs @@ -149,7 +149,8 @@ fn with_config( let tauri_config = get_tauri_config(None)?; let tauri_config_guard = tauri_config.lock().unwrap(); let tauri_config_ = tauri_config_guard.as_ref().unwrap(); - let cli_options = cli_options.unwrap_or_else(read_options); + let cli_options = + cli_options.unwrap_or_else(|| read_options(&tauri_config_.tauri.bundle.identifier)); let (app, config, metadata) = get_config(None, tauri_config_, &cli_options); (app, config, metadata, cli_options) }; diff --git a/tooling/cli/src/mobile/ios/build.rs b/tooling/cli/src/mobile/ios/build.rs index a872d036c..c0b8c9484 100644 --- a/tooling/cli/src/mobile/ios/build.rs +++ b/tooling/cli/src/mobile/ios/build.rs @@ -4,7 +4,7 @@ use super::{ }; use crate::{ build::Options as BuildOptions, - helpers::flock, + helpers::{config::get as get_config, flock}, interface::{AppSettings, Interface, Options as InterfaceOptions}, mobile::{write_options, CliOptions}, Result, @@ -122,7 +122,17 @@ fn run_build( noise_level, vars: Default::default(), }; - let _handle = write_options(cli_options, env)?; + let _handle = write_options( + &get_config(options.config.as_deref())? + .lock() + .unwrap() + .as_ref() + .unwrap() + .tauri + .bundle + .identifier, + cli_options, + )?; options .features diff --git a/tooling/cli/src/mobile/ios/dev.rs b/tooling/cli/src/mobile/ios/dev.rs index 0d7b7025b..7efb3699a 100644 --- a/tooling/cli/src/mobile/ios/dev.rs +++ b/tooling/cli/src/mobile/ios/dev.rs @@ -4,7 +4,7 @@ use super::{ }; use crate::{ dev::Options as DevOptions, - helpers::flock, + helpers::{config::get as get_config, flock}, interface::{AppSettings, Interface, MobileOptions, Options as InterfaceOptions}, mobile::{write_options, CliOptions, DevChild, DevProcess}, Result, @@ -156,14 +156,23 @@ fn run_dev( no_watch: options.no_watch, }, |options| { - let mut env = env.clone(); let cli_options = CliOptions { features: options.features.clone(), args: options.args.clone(), noise_level, vars: Default::default(), }; - let _handle = write_options(cli_options, &mut env)?; + let _handle = write_options( + &get_config(options.config.as_deref())? + .lock() + .unwrap() + .as_ref() + .unwrap() + .tauri + .bundle + .identifier, + cli_options, + )?; if open { open_and_wait(config, &env) diff --git a/tooling/cli/src/mobile/mod.rs b/tooling/cli/src/mobile/mod.rs index b5eb14f33..d3d7eddd3 100644 --- a/tooling/cli/src/mobile/mod.rs +++ b/tooling/cli/src/mobile/mod.rs @@ -22,11 +22,10 @@ use shared_child::SharedChild; use std::{ collections::HashMap, - env::set_var, - env::var, + env::{set_var, temp_dir}, ffi::OsString, fmt::Write, - fs::{create_dir_all, remove_dir_all}, + fs::{create_dir_all, read_to_string, remove_dir_all, write}, net::SocketAddr, path::PathBuf, process::ExitStatus, @@ -196,8 +195,8 @@ fn env() -> Result { /// Writes CLI options to be used later on the Xcode and Android Studio build commands pub fn write_options( + identifier: &str, mut options: CliOptions, - env: &mut Env, ) -> crate::Result<(Runtime, ServerHandle)> { options.vars.extend(env_vars()); @@ -215,12 +214,15 @@ pub fn write_options( }); let (handle, addr) = r?; - env.insert_env_var("TAURI_OPTIONS_SERVER_ADDR".into(), addr.to_string().into()); + write( + temp_dir().join(format!("{identifier}-server-addr")), + addr.to_string(), + )?; Ok((runtime, handle)) } -fn read_options() -> CliOptions { +fn read_options(identifier: &str) -> CliOptions { let runtime = tokio::runtime::Runtime::new().unwrap(); let options = runtime .block_on(async move { @@ -228,7 +230,8 @@ fn read_options() -> CliOptions { .build( format!( "ws://{}", - var("TAURI_OPTIONS_SERVER_ADDR").expect("missing addr environment variable") + read_to_string(temp_dir().join(format!("{identifier}-server-addr"))) + .expect("missing addr file") ) .parse() .unwrap(),