Преглед изворни кода

refactor(example): use `path_resolver` API on the resource example

Lucas Nogueira пре 3 година
родитељ
комит
e61162aa9a
2 измењених фајлова са 12 додато и 15 уклоњено
  1. 2 0
      core/tauri-build/src/lib.rs
  2. 10 15
      examples/resources/src-tauri/src/main.rs

+ 2 - 0
core/tauri-build/src/lib.rs

@@ -25,6 +25,8 @@ fn copy_file(from: impl AsRef<Path>, to: impl AsRef<Path>) -> Result<()> {
   if !from.is_file() {
     return Err(anyhow::anyhow!("{:?} is not a file", from));
   }
+  let dest_dir = to.parent().expect("No data in parent");
+  std::fs::create_dir_all(dest_dir)?;
   std::fs::copy(from, to)?;
   Ok(())
 }

+ 10 - 15
examples/resources/src-tauri/src/main.rs

@@ -9,25 +9,20 @@
 
 fn main() {
   use tauri::{
-    api::{
-      path::{resolve_path, BaseDirectory},
-      process::{Command, CommandEvent},
-    },
+    api::process::{Command, CommandEvent},
     Manager,
   };
-  let context = tauri::generate_context!();
-  let script_path = resolve_path(
-    context.config(),
-    context.package_info(),
-    &Default::default(),
-    "assets/index.js",
-    Some(BaseDirectory::Resource),
-  )
-  .unwrap();
+
   tauri::Builder::default()
     .setup(move |app| {
       let window = app.get_window("main").unwrap();
-      let script_path = script_path.to_string_lossy().to_string();
+      let script_path = app
+        .path_resolver()
+        .resource_dir()
+        .unwrap()
+        .join("assets/index.js")
+        .to_string_lossy()
+        .to_string();
       tauri::async_runtime::spawn(async move {
         let (mut rx, _child) = Command::new("node")
           .args(&[script_path])
@@ -46,6 +41,6 @@ fn main() {
 
       Ok(())
     })
-    .run(context)
+    .run(tauri::generate_context!())
     .expect("error while running tauri application");
 }