Skip to content

Commit 4d09074

Browse files
authored
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
1 parent 58d4709 commit 4d09074

File tree

5 files changed

+29
-11
lines changed

5 files changed

+29
-11
lines changed

.changes/cli-mobile-auto-ip.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22
'cli.rs': 'patch'
33
---
44

5-
Auto select an external IP for mobile development and fallback to prompting the user.
5+
Auto select an external IP for mobile development and fallback to prompting the user. Use `--force-ip-prompt` to force prompting.

tooling/cli/src/dev.rs

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,9 @@ pub struct Options {
6565
/// Disable the dev server for static files.
6666
#[clap(long)]
6767
pub no_dev_server: bool,
68+
/// Force prompting for an IP to use to connect to the dev server on mobile.
69+
#[clap(long)]
70+
pub force_ip_prompt: bool,
6871
}
6972

7073
pub fn command(options: Options) -> Result<()> {
@@ -86,10 +89,10 @@ fn command_internal(mut options: Options) -> Result<()> {
8689
})
8790
}
8891

89-
pub fn local_ip_address() -> &'static IpAddr {
92+
pub fn local_ip_address(force: bool) -> &'static IpAddr {
9093
static LOCAL_IP: OnceCell<IpAddr> = OnceCell::new();
9194
LOCAL_IP.get_or_init(|| {
92-
let ip = local_ip_address::local_ip().unwrap_or_else(|_| {
95+
let prompt_for_ip = || {
9396
let addresses: Vec<IpAddr> = local_ip_address::list_afinet_netifas()
9497
.expect("failed to list networks")
9598
.into_iter()
@@ -117,10 +120,14 @@ pub fn local_ip_address() -> &'static IpAddr {
117120
*addresses.get(selected).unwrap()
118121
}
119122
}
120-
});
123+
};
121124

125+
let ip = if force {
126+
prompt_for_ip()
127+
} else {
128+
local_ip_address::local_ip().unwrap_or_else(|_| prompt_for_ip())
129+
};
122130
log::info!("Using {ip} to access the development server.");
123-
124131
ip
125132
})
126133
}
@@ -175,7 +182,7 @@ pub fn setup(options: &mut Options, mobile: bool) -> Result<AppInterface> {
175182
if let Some(mut before_dev) = script {
176183
if before_dev.contains("$HOST") {
177184
if mobile {
178-
let local_ip_address = local_ip_address().to_string();
185+
let local_ip_address = local_ip_address(options.force_ip_prompt).to_string();
179186
before_dev = before_dev.replace("$HOST", &local_ip_address);
180187
if let AppUrl::Url(WindowUrl::External(url)) = &mut dev_path {
181188
url.set_host(Some(&local_ip_address))?;
@@ -293,7 +300,7 @@ pub fn setup(options: &mut Options, mobile: bool) -> Result<AppInterface> {
293300
use crate::helpers::web_dev_server::start_dev_server;
294301
if path.exists() {
295302
let ip = if mobile {
296-
*local_ip_address()
303+
*local_ip_address(options.force_ip_prompt)
297304
} else {
298305
Ipv4Addr::new(127, 0, 0, 1).into()
299306
};

tooling/cli/src/mobile/android/dev.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,9 @@ pub struct Options {
6161
pub open: bool,
6262
/// Runs on the given device name
6363
pub device: Option<String>,
64+
/// Force prompting for an IP to use to connect to the dev server on mobile.
65+
#[clap(long)]
66+
pub force_ip_prompt: bool,
6467
}
6568

6669
impl From<Options> for DevOptions {
@@ -75,6 +78,7 @@ impl From<Options> for DevOptions {
7578
args: Vec::new(),
7679
no_watch: options.no_watch,
7780
no_dev_server: options.no_dev_server,
81+
force_ip_prompt: options.force_ip_prompt,
7882
}
7983
}
8084
}
@@ -103,7 +107,7 @@ fn run_dev(
103107
metadata: &AndroidMetadata,
104108
noise_level: NoiseLevel,
105109
) -> Result<()> {
106-
setup_dev_config(&mut options.config)?;
110+
setup_dev_config(&mut options.config, options.force_ip_prompt)?;
107111
let mut env = env()?;
108112
let device = if options.open {
109113
None

tooling/cli/src/mobile/ios/dev.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,9 @@ pub struct Options {
5151
pub open: bool,
5252
/// Runs on the given device name
5353
pub device: Option<String>,
54+
/// Force prompting for an IP to use to connect to the dev server on mobile.
55+
#[clap(long)]
56+
pub force_ip_prompt: bool,
5457
}
5558

5659
impl From<Options> for DevOptions {
@@ -65,6 +68,7 @@ impl From<Options> for DevOptions {
6568
args: Vec::new(),
6669
no_watch: options.no_watch,
6770
no_dev_server: options.no_dev_server,
71+
force_ip_prompt: options.force_ip_prompt,
6872
}
6973
}
7074
}
@@ -115,7 +119,7 @@ fn run_dev(
115119
config: &AppleConfig,
116120
noise_level: NoiseLevel,
117121
) -> Result<()> {
118-
setup_dev_config(&mut options.config)?;
122+
setup_dev_config(&mut options.config, options.force_ip_prompt)?;
119123
let env = env()?;
120124
let device = if options.open {
121125
None

tooling/cli/src/mobile/mod.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,10 @@ pub struct CliOptions {
133133
pub vars: HashMap<String, OsString>,
134134
}
135135

136-
fn setup_dev_config(config_extension: &mut Option<String>) -> crate::Result<()> {
136+
fn setup_dev_config(
137+
config_extension: &mut Option<String>,
138+
force_ip_prompt: bool,
139+
) -> crate::Result<()> {
137140
let config = get_config(config_extension.as_deref())?;
138141

139142
let mut dev_path = config
@@ -154,7 +157,7 @@ fn setup_dev_config(config_extension: &mut Option<String>) -> crate::Result<()>
154157
_ => false,
155158
};
156159
if localhost {
157-
let ip = crate::dev::local_ip_address();
160+
let ip = crate::dev::local_ip_address(force_ip_prompt);
158161
url.set_host(Some(&ip.to_string())).unwrap();
159162
if let Some(c) = config_extension {
160163
let mut c: tauri_utils::config::Config = serde_json::from_str(c)?;

0 commit comments

Comments
 (0)