Browse Source

feat(cli): run local dev server on public IP on mobile development (#5705)

Lucas Fernandes Nogueira 2 năm trước cách đây
mục cha
commit
d38204907e

+ 83 - 3
tooling/cli/Cargo.lock

@@ -2153,6 +2153,18 @@ dependencies = [
  "safemem",
 ]
 
+[[package]]
+name = "local-ip-address"
+version = "0.4.9"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "9eab7f092fb08006040e656c2adce6670e6a516bea831a8b2b3d0fb24d4488f5"
+dependencies = [
+ "libc",
+ "neli",
+ "thiserror",
+ "windows-sys 0.42.0",
+]
+
 [[package]]
 name = "lock_api"
 version = "0.4.7"
@@ -2282,7 +2294,7 @@ dependencies = [
  "libc",
  "log",
  "wasi 0.11.0+wasi-snapshot-preview1",
- "windows-sys",
+ "windows-sys 0.36.1",
 ]
 
 [[package]]
@@ -2367,6 +2379,16 @@ dependencies = [
  "tempfile",
 ]
 
+[[package]]
+name = "neli"
+version = "0.5.3"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "9053554eb5dcb7e10d9cdab1206965bde870eed5d0d341532ca035e3ba221508"
+dependencies = [
+ "byteorder",
+ "libc",
+]
+
 [[package]]
 name = "new_debug_unreachable"
 version = "1.0.4"
@@ -2685,7 +2707,7 @@ dependencies = [
  "libc",
  "redox_syscall",
  "smallvec",
- "windows-sys",
+ "windows-sys 0.36.1",
 ]
 
 [[package]]
@@ -3319,7 +3341,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
 checksum = "88d6731146462ea25d9244b2ed5fd1d716d25c52e4d54aa4fb0f3c4e9854dbe2"
 dependencies = [
  "lazy_static",
- "windows-sys",
+ "windows-sys 0.36.1",
 ]
 
 [[package]]
@@ -3891,6 +3913,7 @@ dependencies = [
  "jsonschema",
  "kuchiki",
  "libc",
+ "local-ip-address",
  "log",
  "minisign",
  "notify",
@@ -4714,12 +4737,33 @@ dependencies = [
  "windows_x86_64_msvc 0.36.1",
 ]
 
+[[package]]
+name = "windows-sys"
+version = "0.42.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "5a3e1820f08b8513f676f7ab6c1f99ff312fb97b553d30ff4dd86f9f15728aa7"
+dependencies = [
+ "windows_aarch64_gnullvm",
+ "windows_aarch64_msvc 0.42.0",
+ "windows_i686_gnu 0.42.0",
+ "windows_i686_msvc 0.42.0",
+ "windows_x86_64_gnu 0.42.0",
+ "windows_x86_64_gnullvm",
+ "windows_x86_64_msvc 0.42.0",
+]
+
 [[package]]
 name = "windows-tokens"
 version = "0.39.0"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 checksum = "f838de2fe15fe6bac988e74b798f26499a8b21a9d97edec321e79b28d1d7f597"
 
+[[package]]
+name = "windows_aarch64_gnullvm"
+version = "0.42.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "41d2aa71f6f0cbe00ae5167d90ef3cfe66527d6f613ca78ac8024c3ccab9a19e"
+
 [[package]]
 name = "windows_aarch64_msvc"
 version = "0.36.1"
@@ -4732,6 +4776,12 @@ version = "0.39.0"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 checksum = "ec7711666096bd4096ffa835238905bb33fb87267910e154b18b44eaabb340f2"
 
+[[package]]
+name = "windows_aarch64_msvc"
+version = "0.42.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "dd0f252f5a35cac83d6311b2e795981f5ee6e67eb1f9a7f64eb4500fbc4dcdb4"
+
 [[package]]
 name = "windows_i686_gnu"
 version = "0.36.1"
@@ -4744,6 +4794,12 @@ version = "0.39.0"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 checksum = "763fc57100a5f7042e3057e7e8d9bdd7860d330070251a73d003563a3bb49e1b"
 
+[[package]]
+name = "windows_i686_gnu"
+version = "0.42.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "fbeae19f6716841636c28d695375df17562ca208b2b7d0dc47635a50ae6c5de7"
+
 [[package]]
 name = "windows_i686_msvc"
 version = "0.36.1"
@@ -4756,6 +4812,12 @@ version = "0.39.0"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 checksum = "7bc7cbfe58828921e10a9f446fcaaf649204dcfe6c1ddd712c5eebae6bda1106"
 
+[[package]]
+name = "windows_i686_msvc"
+version = "0.42.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "84c12f65daa39dd2babe6e442988fc329d6243fdce47d7d2d155b8d874862246"
+
 [[package]]
 name = "windows_x86_64_gnu"
 version = "0.36.1"
@@ -4768,6 +4830,18 @@ version = "0.39.0"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 checksum = "6868c165637d653ae1e8dc4d82c25d4f97dd6605eaa8d784b5c6e0ab2a252b65"
 
+[[package]]
+name = "windows_x86_64_gnu"
+version = "0.42.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "bf7b1b21b5362cbc318f686150e5bcea75ecedc74dd157d874d754a2ca44b0ed"
+
+[[package]]
+name = "windows_x86_64_gnullvm"
+version = "0.42.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "09d525d2ba30eeb3297665bd434a54297e4170c7f1a44cad4ef58095b4cd2028"
+
 [[package]]
 name = "windows_x86_64_msvc"
 version = "0.36.1"
@@ -4780,6 +4854,12 @@ version = "0.39.0"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 checksum = "5e4d40883ae9cae962787ca76ba76390ffa29214667a111db9e0a1ad8377e809"
 
+[[package]]
+name = "windows_x86_64_msvc"
+version = "0.42.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "f40009d85759725a34da6d89a94e63d7bdc50a862acf0dbc7c8e488f1edcb6f5"
+
 [[package]]
 name = "winreg"
 version = "0.10.1"

+ 1 - 0
tooling/cli/Cargo.toml

@@ -85,6 +85,7 @@ html5ever = "0.25"
 kuchiki = "0.8"
 tokio = { version = "1", features = [ "macros", "sync" ] }
 common-path = "1"
+local-ip-address = "0.4"
 
 [target."cfg(windows)".dependencies]
 winapi = { version = "0.3", features = [ "handleapi", "processenv", "winbase", "wincon", "winnt" ] }

+ 15 - 6
tooling/cli/src/dev.rs

@@ -20,6 +20,7 @@ use shared_child::SharedChild;
 
 use std::{
   env::set_current_dir,
+  net::{Ipv4Addr, SocketAddr},
   process::{exit, Command, ExitStatus, Stdio},
   sync::{
     atomic::{AtomicBool, Ordering},
@@ -74,7 +75,7 @@ pub fn command(options: Options) -> Result<()> {
 }
 
 fn command_internal(mut options: Options) -> Result<()> {
-  let mut interface = setup(&mut options)?;
+  let mut interface = setup(&mut options, false)?;
   let exit_on_panic = options.exit_on_panic;
   let no_watch = options.no_watch;
   interface.dev(options.into(), move |status, reason| {
@@ -82,7 +83,7 @@ fn command_internal(mut options: Options) -> Result<()> {
   })
 }
 
-pub fn setup(options: &mut Options) -> Result<AppInterface> {
+pub fn setup(options: &mut Options, mobile: bool) -> Result<AppInterface> {
   let tauri_path = tauri_dir();
   options.config = if let Some(config) = &options.config {
     Some(if config.starts_with('{') {
@@ -228,11 +229,19 @@ pub fn setup(options: &mut Options) -> Result<AppInterface> {
     .dev_path
     .clone();
   if let AppUrl::Url(WindowUrl::App(path)) = &dev_path {
-    use crate::helpers::web_dev_server::{start_dev_server, SERVER_URL};
+    use crate::helpers::web_dev_server::start_dev_server;
     if path.exists() {
+      let ip = if mobile {
+        local_ip_address::local_ip().expect("failed to resolve local IP address")
+      } else {
+        Ipv4Addr::new(127, 0, 0, 1).into()
+      };
+      let port = 1430;
+      let server_address = SocketAddr::new(ip, port);
       let path = path.canonicalize()?;
-      start_dev_server(path);
-      dev_path = AppUrl::Url(WindowUrl::External(SERVER_URL.parse().unwrap()));
+      start_dev_server(server_address, path);
+      let server_url = format!("http://{}", server_address);
+      dev_path = AppUrl::Url(WindowUrl::External(server_url.parse().unwrap()));
 
       // TODO: in v2, use an env var to pass the url to the app context
       // or better separate the config passed from the cli internally and
@@ -245,7 +254,7 @@ pub fn setup(options: &mut Options) -> Result<AppInterface> {
       } else {
         options.config = Some(format!(
           r#"{{ "build": {{ "devPath": "{}" }} }}"#,
-          SERVER_URL
+          server_url
         ))
       }
     }

+ 2 - 4
tooling/cli/src/helpers/web_dev_server.rs

@@ -12,7 +12,6 @@ use notify_debouncer_mini::new_debouncer;
 use std::{
   net::SocketAddr,
   path::{Path, PathBuf},
-  str::FromStr,
   sync::{mpsc::sync_channel, Arc},
   thread,
   time::Duration,
@@ -21,14 +20,13 @@ use tauri_utils::mime_type::MimeType;
 use tokio::sync::broadcast::{channel, Sender};
 
 const AUTO_RELOAD_SCRIPT: &str = include_str!("./auto-reload.js");
-pub const SERVER_URL: &str = "http://127.0.0.1:1430";
 
 struct State {
   serve_dir: PathBuf,
   tx: Sender<()>,
 }
 
-pub fn start_dev_server<P: AsRef<Path>>(path: P) {
+pub fn start_dev_server<P: AsRef<Path>>(address: SocketAddr, path: P) {
   let serve_dir = path.as_ref().to_path_buf();
 
   std::thread::spawn(move || {
@@ -80,7 +78,7 @@ pub fn start_dev_server<P: AsRef<Path>>(path: P) {
               ws.on_upgrade(|socket| async move { ws_handler(socket, state).await })
             }),
           );
-        Server::bind(&SocketAddr::from_str(SERVER_URL.split('/').nth(2).unwrap()).unwrap())
+        Server::bind(&address)
           .serve(router.into_make_service())
           .await
           .unwrap();

+ 1 - 1
tooling/cli/src/mobile/android/dev.rs

@@ -92,7 +92,7 @@ fn run_dev(
   noise_level: NoiseLevel,
 ) -> Result<()> {
   let mut dev_options = options.clone().into();
-  let mut interface = crate::dev::setup(&mut dev_options)?;
+  let mut interface = crate::dev::setup(&mut dev_options, true)?;
 
   let app_settings = interface.app_settings();
   let bin_path = app_settings.app_binary_path(&InterfaceOptions {

+ 1 - 1
tooling/cli/src/mobile/ios/dev.rs

@@ -107,7 +107,7 @@ fn run_dev(
   noise_level: NoiseLevel,
 ) -> Result<()> {
   let mut dev_options = options.clone().into();
-  let mut interface = crate::dev::setup(&mut dev_options)?;
+  let mut interface = crate::dev::setup(&mut dev_options, true)?;
 
   let app_settings = interface.app_settings();
   let bin_path = app_settings.app_binary_path(&InterfaceOptions {