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
This commit is contained in:
Amr Bashir 2023-03-16 14:04:09 +02:00 committed by GitHub
parent 58d4709f7e
commit 4d09074454
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 29 additions and 11 deletions

View File

@ -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.

View File

@ -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<IpAddr> = OnceCell::new();
LOCAL_IP.get_or_init(|| {
let ip = local_ip_address::local_ip().unwrap_or_else(|_| {
let prompt_for_ip = || {
let addresses: Vec<IpAddr> = 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<AppInterface> {
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<AppInterface> {
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()
};

View File

@ -61,6 +61,9 @@ pub struct Options {
pub open: bool,
/// Runs on the given device name
pub device: Option<String>,
/// Force prompting for an IP to use to connect to the dev server on mobile.
#[clap(long)]
pub force_ip_prompt: bool,
}
impl From<Options> for DevOptions {
@ -75,6 +78,7 @@ impl From<Options> 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

View File

@ -51,6 +51,9 @@ pub struct Options {
pub open: bool,
/// Runs on the given device name
pub device: Option<String>,
/// Force prompting for an IP to use to connect to the dev server on mobile.
#[clap(long)]
pub force_ip_prompt: bool,
}
impl From<Options> for DevOptions {
@ -65,6 +68,7 @@ impl From<Options> 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

View File

@ -133,7 +133,10 @@ pub struct CliOptions {
pub vars: HashMap<String, OsString>,
}
fn setup_dev_config(config_extension: &mut Option<String>) -> crate::Result<()> {
fn setup_dev_config(
config_extension: &mut Option<String>,
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<String>) -> 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)?;