From 4d09074454068ae282ccd4670ae0b1df30510a3a Mon Sep 17 00:00:00 2001 From: Amr Bashir Date: Thu, 16 Mar 2023 14:04:09 +0200 Subject: [PATCH] feat(cli): add `--force-ip-prompt` (#6406) * feat(cli): add `--force-ip-prompt` * Restore tooling/cli/Cargo.lock * Restore tooling/cli/Cargo.toml * fix macos build --- .changes/cli-mobile-auto-ip.md | 2 +- tooling/cli/src/dev.rs | 19 +++++++++++++------ tooling/cli/src/mobile/android/dev.rs | 6 +++++- tooling/cli/src/mobile/ios/dev.rs | 6 +++++- tooling/cli/src/mobile/mod.rs | 7 +++++-- 5 files changed, 29 insertions(+), 11 deletions(-) diff --git a/.changes/cli-mobile-auto-ip.md b/.changes/cli-mobile-auto-ip.md index c85ff8458..022f5f100 100644 --- a/.changes/cli-mobile-auto-ip.md +++ b/.changes/cli-mobile-auto-ip.md @@ -2,4 +2,4 @@ 'cli.rs': 'patch' --- -Auto select an external IP for mobile development and fallback to prompting the user. +Auto select an external IP for mobile development and fallback to prompting the user. Use `--force-ip-prompt` to force prompting. diff --git a/tooling/cli/src/dev.rs b/tooling/cli/src/dev.rs index a109c2004..c16829da5 100644 --- a/tooling/cli/src/dev.rs +++ b/tooling/cli/src/dev.rs @@ -65,6 +65,9 @@ pub struct Options { /// Disable the dev server for static files. #[clap(long)] pub no_dev_server: bool, + /// Force prompting for an IP to use to connect to the dev server on mobile. + #[clap(long)] + pub force_ip_prompt: bool, } pub fn command(options: Options) -> Result<()> { @@ -86,10 +89,10 @@ fn command_internal(mut options: Options) -> Result<()> { }) } -pub fn local_ip_address() -> &'static IpAddr { +pub fn local_ip_address(force: bool) -> &'static IpAddr { static LOCAL_IP: OnceCell = OnceCell::new(); LOCAL_IP.get_or_init(|| { - let ip = local_ip_address::local_ip().unwrap_or_else(|_| { + let prompt_for_ip = || { let addresses: Vec = local_ip_address::list_afinet_netifas() .expect("failed to list networks") .into_iter() @@ -117,10 +120,14 @@ pub fn local_ip_address() -> &'static IpAddr { *addresses.get(selected).unwrap() } } - }); + }; + let ip = if force { + prompt_for_ip() + } else { + local_ip_address::local_ip().unwrap_or_else(|_| prompt_for_ip()) + }; log::info!("Using {ip} to access the development server."); - ip }) } @@ -175,7 +182,7 @@ pub fn setup(options: &mut Options, mobile: bool) -> Result { if let Some(mut before_dev) = script { if before_dev.contains("$HOST") { if mobile { - let local_ip_address = local_ip_address().to_string(); + let local_ip_address = local_ip_address(options.force_ip_prompt).to_string(); before_dev = before_dev.replace("$HOST", &local_ip_address); if let AppUrl::Url(WindowUrl::External(url)) = &mut dev_path { url.set_host(Some(&local_ip_address))?; @@ -293,7 +300,7 @@ pub fn setup(options: &mut Options, mobile: bool) -> Result { use crate::helpers::web_dev_server::start_dev_server; if path.exists() { let ip = if mobile { - *local_ip_address() + *local_ip_address(options.force_ip_prompt) } else { Ipv4Addr::new(127, 0, 0, 1).into() }; diff --git a/tooling/cli/src/mobile/android/dev.rs b/tooling/cli/src/mobile/android/dev.rs index a905b2f28..4a9873802 100644 --- a/tooling/cli/src/mobile/android/dev.rs +++ b/tooling/cli/src/mobile/android/dev.rs @@ -61,6 +61,9 @@ pub struct Options { pub open: bool, /// Runs on the given device name pub device: Option, + /// Force prompting for an IP to use to connect to the dev server on mobile. + #[clap(long)] + pub force_ip_prompt: bool, } impl From for DevOptions { @@ -75,6 +78,7 @@ impl From for DevOptions { args: Vec::new(), no_watch: options.no_watch, no_dev_server: options.no_dev_server, + force_ip_prompt: options.force_ip_prompt, } } } @@ -103,7 +107,7 @@ fn run_dev( metadata: &AndroidMetadata, noise_level: NoiseLevel, ) -> Result<()> { - setup_dev_config(&mut options.config)?; + setup_dev_config(&mut options.config, options.force_ip_prompt)?; let mut env = env()?; let device = if options.open { None diff --git a/tooling/cli/src/mobile/ios/dev.rs b/tooling/cli/src/mobile/ios/dev.rs index 99b456aa7..d0596908f 100644 --- a/tooling/cli/src/mobile/ios/dev.rs +++ b/tooling/cli/src/mobile/ios/dev.rs @@ -51,6 +51,9 @@ pub struct Options { pub open: bool, /// Runs on the given device name pub device: Option, + /// Force prompting for an IP to use to connect to the dev server on mobile. + #[clap(long)] + pub force_ip_prompt: bool, } impl From for DevOptions { @@ -65,6 +68,7 @@ impl From for DevOptions { args: Vec::new(), no_watch: options.no_watch, no_dev_server: options.no_dev_server, + force_ip_prompt: options.force_ip_prompt, } } } @@ -115,7 +119,7 @@ fn run_dev( config: &AppleConfig, noise_level: NoiseLevel, ) -> Result<()> { - setup_dev_config(&mut options.config)?; + setup_dev_config(&mut options.config, options.force_ip_prompt)?; let env = env()?; let device = if options.open { None diff --git a/tooling/cli/src/mobile/mod.rs b/tooling/cli/src/mobile/mod.rs index 91ca199d3..d0aca49b4 100644 --- a/tooling/cli/src/mobile/mod.rs +++ b/tooling/cli/src/mobile/mod.rs @@ -133,7 +133,10 @@ pub struct CliOptions { pub vars: HashMap, } -fn setup_dev_config(config_extension: &mut Option) -> crate::Result<()> { +fn setup_dev_config( + config_extension: &mut Option, + force_ip_prompt: bool, +) -> crate::Result<()> { let config = get_config(config_extension.as_deref())?; let mut dev_path = config @@ -154,7 +157,7 @@ fn setup_dev_config(config_extension: &mut Option) -> crate::Result<()> _ => false, }; if localhost { - let ip = crate::dev::local_ip_address(); + let ip = crate::dev::local_ip_address(force_ip_prompt); url.set_host(Some(&ip.to_string())).unwrap(); if let Some(c) = config_extension { let mut c: tauri_utils::config::Config = serde_json::from_str(c)?;