Skip to content

Commit

Permalink
refactor(cli): use temp file to communicate IPC websocket address (#6219
Browse files Browse the repository at this point in the history
)
  • Loading branch information
lucasfernog authored Feb 9, 2023
1 parent 481d9de commit 894a8d0
Show file tree
Hide file tree
Showing 8 changed files with 68 additions and 19 deletions.
6 changes: 6 additions & 0 deletions .changes/cli-refactor-ipc-mobile.md
Original file line number Diff line number Diff line change
@@ -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.
3 changes: 2 additions & 1 deletion tooling/cli/src/mobile/android.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,8 @@ pub fn with_config<T>(
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)
};
Expand Down
14 changes: 12 additions & 2 deletions tooling/cli/src/mobile/android/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down
15 changes: 12 additions & 3 deletions tooling/cli/src/mobile/android/dev.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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)
Expand Down
3 changes: 2 additions & 1 deletion tooling/cli/src/mobile/ios.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,8 @@ fn with_config<T>(
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)
};
Expand Down
14 changes: 12 additions & 2 deletions tooling/cli/src/mobile/ios/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down
15 changes: 12 additions & 3 deletions tooling/cli/src/mobile/ios/dev.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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)
Expand Down
17 changes: 10 additions & 7 deletions tooling/cli/src/mobile/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -196,8 +195,8 @@ fn env() -> Result<Env, EnvError> {

/// 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());

Expand All @@ -215,20 +214,24 @@ 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 {
let (tx, rx) = WsTransportClientBuilder::default()
.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(),
Expand Down

0 comments on commit 894a8d0

Please sign in to comment.