Kaynağa Gözat

fix(tauri) env_var build errors. Fixes #631 (#632)

* fix env_var build errors.

* comment out webview test.

* cleanup imports.

* fix test.
Tensor-Programming 5 yıl önce
ebeveyn
işleme
b3617062cb

+ 1 - 0
tauri/Cargo.toml

@@ -24,6 +24,7 @@ threadpool = "1.8"
 uuid = { version = "0.8.1", features = ["v4"] }
 anyhow = "1.0.31"
 thiserror = "1.0.19"
+envmnt = "0.8.2"
 
 tauri-api = { version = "0.5",  path = "../tauri-api" }
 

+ 58 - 14
tauri/src/app/runner.rs

@@ -1,5 +1,11 @@
 #[allow(unused_imports)]
-use std::{fs::read_to_string, path::Path, process::Stdio, thread::spawn};
+use std::{
+  env,
+  fs::{self, read_to_string},
+  path::Path,
+  process::Stdio,
+  thread::spawn,
+};
 
 use web_view::{builder, Content, WebView};
 
@@ -63,7 +69,14 @@ fn setup_content(config: Config) -> crate::Result<Content<String>> {
   if config.build.dev_path.starts_with("http") {
     Ok(Content::Url(config.build.dev_path))
   } else {
-    let dev_path = Path::new(env!("TAURI_DIST_DIR")).join("index.tauri.html");
+    let dist_dir = match option_env!("TAURI_DIST_DIR") {
+      Some(d) => d.to_string(),
+      None => env::current_dir()?
+        .into_os_string()
+        .into_string()
+        .expect("Unable to convert to normal String"),
+    };
+    let dev_path = Path::new(&dist_dir).join("index.tauri.html");
     Ok(Content::Html(read_to_string(dev_path)?))
   }
 }
@@ -80,8 +93,16 @@ fn setup_content(config: Config) -> crate::Result<Content<String>> {
 // setup content for no-server
 #[cfg(feature = "no-server")]
 fn setup_content(_: Config) -> crate::Result<Content<String>> {
-  let html = include_str!(concat!(env!("TAURI_DIST_DIR"), "/index.tauri.html"));
-  Ok(Content::Html(String::from(html)))
+  let dist_dir = match option_env!("TAURI_DIST_DIR") {
+    Some(d) => d.to_string(),
+    None => env::current_dir()?
+      .into_os_string()
+      .into_string()
+      .expect("Unable to convert to normal String"),
+  };
+  let index_path = Path::new(dist_dir).join("index.tauri.html");
+
+  Ok(Content::Html(read_to_string(index_path)?))
 }
 
 // get the port for the embedded server
@@ -234,10 +255,13 @@ fn build_webview(
   webview.set_fullscreen(fullscreen);
 
   if has_splashscreen {
+    let env_var = envmnt::get_or("TAURI_DIR", "../dist");
+    let path = Path::new(&env_var);
+    let contents = fs::read_to_string(path.join("/tauri.js"))?;
     // inject the tauri.js entry point
     webview
       .handle()
-      .dispatch(|_webview| _webview.eval(include_str!(concat!(env!("TAURI_DIR"), "/tauri.js"))))?;
+      .dispatch(move |_webview| _webview.eval(&contents))?;
   }
 
   Ok(webview)
@@ -257,7 +281,7 @@ mod test {
   use web_view::Content;
 
   #[cfg(not(feature = "embedded-server"))]
-  use std::{fs::read_to_string, path::Path};
+  use std::{env, fs::read_to_string, path::Path};
 
   fn init_config() -> crate::config::Config {
     crate::config::get().expect("unable to setup default config")
@@ -278,20 +302,40 @@ mod test {
 
     #[cfg(feature = "no-server")]
     match res {
-      Ok(Content::Html(s)) => assert_eq!(
-        s,
-        read_to_string(Path::new(env!("TAURI_DIST_DIR")).join("index.tauri.html")).unwrap()
-      ),
+      Ok(Content::Html(s)) => {
+        let dist_dir = match option_env!("TAURI_DIST_DIR") {
+          Some(d) => d.to_string(),
+          None => env::current_dir()
+            .unwrap()
+            .into_os_string()
+            .into_string()
+            .expect("Unable to convert to normal String"),
+        };
+        assert_eq!(
+          s,
+          read_to_string(Path::new(&dist_dir).join("index.tauri.html")).unwrap()
+        );
+      }
       _ => assert!(false),
     }
 
     #[cfg(not(any(feature = "embedded-server", feature = "no-server")))]
     match res {
       Ok(Content::Url(dp)) => assert_eq!(dp, _c.build.dev_path),
-      Ok(Content::Html(s)) => assert_eq!(
-        s,
-        read_to_string(Path::new(env!("TAURI_DIST_DIR")).join("index.tauri.html")).unwrap()
-      ),
+      Ok(Content::Html(s)) => {
+        let dist_dir = match option_env!("TAURI_DIST_DIR") {
+          Some(d) => d.to_string(),
+          None => env::current_dir()
+            .unwrap()
+            .into_os_string()
+            .into_string()
+            .expect("Unable to convert to normal String"),
+        };
+        assert_eq!(
+          s,
+          read_to_string(Path::new(&dist_dir).join("index.tauri.html")).unwrap()
+        );
+      }
       _ => assert!(false),
     }
   }

+ 12 - 6
tauri/src/config.rs

@@ -1,6 +1,6 @@
 use serde::Deserialize;
 
-use std::env;
+use std::{fs, path};
 
 #[derive(PartialEq, Deserialize, Clone, Debug)]
 #[serde(tag = "window", rename_all = "camelCase")]
@@ -112,10 +112,13 @@ fn default_build() -> BuildConfig {
 pub fn get() -> crate::Result<Config> {
   match option_env!("TAURI_CONFIG") {
     Some(config) => Ok(serde_json::from_str(config).expect("failed to parse TAURI_CONFIG env")),
-    None => Ok(
-      serde_json::from_str(include_str!(concat!(env!("TAURI_DIR"), "/tauri.conf.json")))
-        .expect("failed to read tauri.conf.json"),
-    ),
+    None => {
+      let env_var = envmnt::get_or("TAURI_DIR", "../dist");
+      let path = path::Path::new(&env_var);
+      let contents = fs::read_to_string(path.join("tauri.conf.json"))?;
+
+      Ok(serde_json::from_str(&contents).expect("failed to read tauri.conf.json"))
+    }
   }
 }
 
@@ -156,7 +159,10 @@ mod test {
     // check to see if there is an OK or Err, on Err fail test.
     match config {
       // On Ok, check that the config is the same as the test config.
-      Ok(c) => assert_eq!(c, test_config),
+      Ok(c) => {
+        println!("{:?}", c);
+        assert_eq!(c, test_config)
+      }
       Err(_) => assert!(false),
     }
   }

+ 18 - 18
tauri/src/endpoints/file_system.rs

@@ -233,27 +233,27 @@ pub fn read_binary_file<T: 'static>(
 // test webview functionality.
 #[cfg(test)]
 mod test {
-  use super::*;
-  use web_view::*;
+  // use super::*;
+  // use web_view::*;
 
   // create a makeshift webview
-  fn create_test_webview() -> crate::Result<WebView<'static, ()>> {
-    // basic html set into webview
-    let content = r#"<html><head></head><body></body></html>"#;
+  // fn create_test_webview() -> crate::Result<WebView<'static, ()>> {
+  //   // basic html set into webview
+  //   let content = r#"<html><head></head><body></body></html>"#;
 
-    Ok(
-      // use webview builder to create simple webview
-      WebViewBuilder::new()
-        .title("test")
-        .size(800, 800)
-        .resizable(true)
-        .debug(true)
-        .user_data(())
-        .invoke_handler(|_wv, _arg| Ok(()))
-        .content(Content::Html(content))
-        .build()?,
-    )
-  }
+  //   Ok(
+  //     // use webview builder to create simple webview
+  //     WebViewBuilder::new()
+  //       .title("test")
+  //       .size(800, 800)
+  //       .resizable(true)
+  //       .debug(true)
+  //       .user_data(())
+  //       .invoke_handler(|_wv, _arg| Ok(()))
+  //       .content(Content::Html(content))
+  //       .build()?,
+  //   )
+  // }
 
   /* #[test]
   #[cfg(not(any(target_os = "linux", target_os = "macos")))]