Skip to content

Commit

Permalink
feat(cli): add --force-ip-prompt (#6406)
Browse files Browse the repository at this point in the history
* feat(cli): add `--force-ip-prompt`

* Restore tooling/cli/Cargo.lock

* Restore tooling/cli/Cargo.toml

* fix macos build
  • Loading branch information
amrbashir authored Mar 16, 2023
1 parent 58d4709 commit 4d09074
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 11 deletions.
2 changes: 1 addition & 1 deletion .changes/cli-mobile-auto-ip.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
19 changes: 13 additions & 6 deletions tooling/cli/src/dev.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<()> {
Expand All @@ -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()
Expand Down Expand Up @@ -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
})
}
Expand Down Expand Up @@ -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))?;
Expand Down Expand Up @@ -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()
};
Expand Down
6 changes: 5 additions & 1 deletion tooling/cli/src/mobile/android/dev.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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,
}
}
}
Expand Down Expand Up @@ -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
Expand Down
6 changes: 5 additions & 1 deletion tooling/cli/src/mobile/ios/dev.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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,
}
}
}
Expand Down Expand Up @@ -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
Expand Down
7 changes: 5 additions & 2 deletions tooling/cli/src/mobile/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)?;
Expand Down

0 comments on commit 4d09074

Please sign in to comment.