Browse Source

refactor(core): move local ip address resolve to CLI

Lucas Nogueira 2 years ago
parent
commit
ad65b95070

+ 0 - 3
core/tauri-codegen/Cargo.toml

@@ -31,9 +31,6 @@ png = "0.17"
 json-patch = "0.2"
 url = "2"
 
-[target."cfg(any(windows, target_os = \"linux\", target_os = \"macos\"))".dependencies]
-local-ip-address = "0.4"
-
 [target."cfg(target_os = \"macos\")".dependencies]
 plist = "1"
 time = { version = "0.3", features = [ "parsing", "formatting" ] }

+ 1 - 18
core/tauri-codegen/src/context.rs

@@ -124,7 +124,7 @@ enum Target {
 pub fn context_codegen(data: ContextData) -> Result<TokenStream, EmbeddedAssetsError> {
   let ContextData {
     dev,
-    mut config,
+    config,
     config_parent,
     root,
   } = data;
@@ -158,23 +158,6 @@ pub fn context_codegen(data: ContextData) -> Result<TokenStream, EmbeddedAssetsE
       panic!("unknown codegen target")
     };
 
-  #[cfg(any(windows, target_os = "linux", target_os = "macos"))]
-  if dev && (target == Target::Ios || target == Target::Android) {
-    if let AppUrl::Url(WindowUrl::External(url)) = &mut config.build.dev_path {
-      let localhost = match url.host() {
-        Some(url::Host::Domain(d)) => d == "localhost",
-        Some(url::Host::Ipv4(i)) => {
-          i == std::net::Ipv4Addr::LOCALHOST || i == std::net::Ipv4Addr::UNSPECIFIED
-        }
-        _ => false,
-      };
-      if localhost {
-        let ip = local_ip_address::local_ip().expect("failed to resolve local IP address");
-        url.set_host(Some(&ip.to_string())).unwrap();
-      }
-    }
-  }
-
   let mut options = AssetOptions::new(config.tauri.pattern.clone())
     .freeze_prototype(config.tauri.security.freeze_prototype)
     .dangerous_disable_asset_csp_modification(

+ 1 - 0
core/tauri/src/endpoints/window.rs

@@ -345,6 +345,7 @@ impl Cmd {
           window.open_devtools();
         }
       }
+      #[allow(unreachable_patterns)]
       _ => (),
     }
     #[allow(unreachable_code)]

+ 0 - 23
examples/api/src-tauri/Cargo.lock

@@ -1585,18 +1585,6 @@ dependencies = [
  "safemem",
 ]
 
-[[package]]
-name = "local-ip-address"
-version = "0.4.7"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "74d6f43d42d775afa8073bfa6aba569b340a3635efa8c9f7702c9c6ed209692f"
-dependencies = [
- "libc",
- "neli",
- "thiserror",
- "windows-sys 0.36.1",
-]
-
 [[package]]
 name = "lock_api"
 version = "0.4.9"
@@ -1796,16 +1784,6 @@ dependencies = [
  "jni-sys",
 ]
 
-[[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"
@@ -3093,7 +3071,6 @@ dependencies = [
  "brotli",
  "ico",
  "json-patch",
- "local-ip-address",
  "plist",
  "png 0.17.7",
  "proc-macro2",

+ 32 - 5
tooling/cli/src/dev.rs

@@ -20,7 +20,7 @@ use shared_child::SharedChild;
 
 use std::{
   env::set_current_dir,
-  net::{Ipv4Addr, SocketAddr},
+  net::{IpAddr, Ipv4Addr, SocketAddr},
   process::{exit, Command, ExitStatus, Stdio},
   sync::{
     atomic::{AtomicBool, Ordering},
@@ -83,6 +83,11 @@ fn command_internal(mut options: Options) -> Result<()> {
   })
 }
 
+fn local_ip_address() -> &'static IpAddr {
+  static LOCAL_IP: OnceCell<IpAddr> = OnceCell::new();
+  LOCAL_IP.get_or_init(|| local_ip_address::local_ip().expect("failed to resolve local IP address"))
+}
+
 pub fn setup(options: &mut Options, mobile: bool) -> Result<AppInterface> {
   let tauri_path = tauri_dir();
   options.config = if let Some(config) = &options.config {
@@ -113,6 +118,30 @@ pub fn setup(options: &mut Options, mobile: bool) -> Result<AppInterface> {
     .dev_path
     .clone();
 
+  if mobile {
+    if let AppUrl::Url(WindowUrl::External(url)) = &mut dev_path {
+      let localhost = match url.host() {
+        Some(url::Host::Domain(d)) => d == "localhost",
+        Some(url::Host::Ipv4(i)) => {
+          i == std::net::Ipv4Addr::LOCALHOST || i == std::net::Ipv4Addr::UNSPECIFIED
+        }
+        _ => false,
+      };
+      if localhost {
+        let ip = local_ip_address();
+        url.set_host(Some(&ip.to_string())).unwrap();
+        if let Some(c) = &options.config {
+          let mut c: tauri_utils::config::Config = serde_json::from_str(c)?;
+          c.build.dev_path = dev_path.clone();
+          options.config = Some(serde_json::to_string(&c).unwrap());
+        } else {
+          options.config = Some(format!(r#"{{ "build": {{ "devPath": "{}" }} }}"#, url))
+        }
+        reload_config(options.config.as_deref())?;
+      }
+    }
+  }
+
   if let Some(before_dev) = config
     .lock()
     .unwrap()
@@ -133,9 +162,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::local_ip()
-            .expect("failed to resolve local IP address")
-            .to_string();
+          let local_ip_address = local_ip_address().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))?;
@@ -253,7 +280,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().expect("failed to resolve local IP address")
+        *local_ip_address()
       } else {
         Ipv4Addr::new(127, 0, 0, 1).into()
       };