Sfoglia il codice sorgente

refactor: change path and event plugins to follow the same convention as `window` (#8040)

Amr Bashir 1 anno fa
parent
commit
a6ad540696

+ 2 - 2
core/tauri/src/app.rs

@@ -807,8 +807,8 @@ shared_app_impl!(AppHandle<R>);
 
 impl<R: Runtime> App<R> {
   fn register_core_plugins(&self) -> crate::Result<()> {
-    self.handle.plugin(crate::path::init())?;
-    self.handle.plugin(crate::event::init())?;
+    self.handle.plugin(crate::path::plugin::init())?;
+    self.handle.plugin(crate::event::plugin::init())?;
     self.handle.plugin(crate::window::plugin::init())?;
     self.handle.plugin(crate::app::plugin::init())?;
     Ok(())

+ 1 - 17
core/tauri/src/event/mod.rs

@@ -2,15 +2,10 @@
 // SPDX-License-Identifier: Apache-2.0
 // SPDX-License-Identifier: MIT
 
-mod commands;
 mod listener;
+pub(crate) mod plugin;
 pub(crate) use listener::Listeners;
 
-use crate::{
-  plugin::{Builder, TauriPlugin},
-  Runtime,
-};
-
 /// Checks if an event name is valid.
 pub fn is_event_name_valid(event: &str) -> bool {
   event
@@ -47,17 +42,6 @@ impl Event {
   }
 }
 
-/// Initializes the event plugin.
-pub(crate) fn init<R: Runtime>() -> TauriPlugin<R> {
-  Builder::new("event")
-    .invoke_handler(crate::generate_handler![
-      commands::listen,
-      commands::unlisten,
-      commands::emit,
-    ])
-    .build()
-}
-
 pub fn unlisten_js(listeners_object_name: &str, event_name: &str, event_id: EventId) -> String {
   format!(
     "

+ 10 - 1
core/tauri/src/event/commands.rs → core/tauri/src/event/plugin.rs

@@ -2,11 +2,13 @@
 // SPDX-License-Identifier: Apache-2.0
 // SPDX-License-Identifier: MIT
 
-use crate::{command, ipc::CallbackFn, EventId, Manager, Result, Runtime, Window};
 use serde::{Deserialize, Deserializer};
 use serde_json::Value as JsonValue;
 use tauri_runtime::window::is_label_valid;
 
+use crate::plugin::{Builder, TauriPlugin};
+use crate::{command, ipc::CallbackFn, EventId, Manager, Result, Runtime, Window};
+
 use super::is_event_name_valid;
 
 pub struct EventName(String);
@@ -94,3 +96,10 @@ pub fn emit<R: Runtime>(
     window.emit_all(&event.0, payload)
   }
 }
+
+/// Initializes the event plugin.
+pub(crate) fn init<R: Runtime>() -> TauriPlugin<R> {
+  Builder::new("event")
+    .invoke_handler(crate::generate_handler![listen, unlisten, emit,])
+    .build()
+}

+ 3 - 54
core/tauri/src/path/mod.rs

@@ -4,16 +4,13 @@
 
 use std::path::{Component, Display, Path, PathBuf};
 
-use crate::{
-  plugin::{Builder, TauriPlugin},
-  Manager, Runtime,
-};
+use crate::Runtime;
 
 use serde::{de::Error as DeError, Deserialize, Deserializer};
 use serde_repr::{Deserialize_repr, Serialize_repr};
-use serialize_to_javascript::{default_template, DefaultTemplate, Template};
 
-mod commands;
+pub(crate) mod plugin;
+
 pub use crate::error::*;
 
 #[cfg(target_os = "android")]
@@ -330,54 +327,6 @@ fn resolve_path<R: Runtime>(
   Ok(base_dir_path)
 }
 
-#[derive(Template)]
-#[default_template("./init.js")]
-struct InitJavascript {
-  sep: &'static str,
-  delimiter: &'static str,
-}
-
-/// Initializes the plugin.
-pub(crate) fn init<R: Runtime>() -> TauriPlugin<R> {
-  #[cfg(windows)]
-  let (sep, delimiter) = ("\\", ";");
-  #[cfg(not(windows))]
-  let (sep, delimiter) = ("/", ":");
-
-  let init_js = InitJavascript { sep, delimiter }
-    .render_default(&Default::default())
-    // this will never fail with the above sep and delimiter values
-    .unwrap();
-
-  Builder::new("path")
-    .invoke_handler(crate::generate_handler![
-      commands::resolve_directory,
-      commands::resolve,
-      commands::normalize,
-      commands::join,
-      commands::dirname,
-      commands::extname,
-      commands::basename,
-      commands::is_absolute
-    ])
-    .js_init_script(init_js.to_string())
-    .setup(|app, _api| {
-      #[cfg(target_os = "android")]
-      {
-        let handle = _api.register_android_plugin("app.tauri", "PathPlugin")?;
-        app.manage(PathResolver(handle));
-      }
-
-      #[cfg(not(target_os = "android"))]
-      {
-        app.manage(PathResolver(app.clone()));
-      }
-
-      Ok(())
-    })
-    .build()
-}
-
 #[cfg(test)]
 mod test {
   use super::SafePathBuf;

+ 55 - 1
core/tauri/src/path/commands.rs → core/tauri/src/path/plugin.rs

@@ -4,8 +4,14 @@
 
 use std::path::{Component, Path, PathBuf, MAIN_SEPARATOR};
 
+use serialize_to_javascript::{default_template, DefaultTemplate, Template};
+
 use super::{BaseDirectory, Error, PathResolver, Result};
-use crate::{command, AppHandle, Runtime, State};
+use crate::{
+  command,
+  plugin::{Builder, TauriPlugin},
+  AppHandle, Manager, Runtime, State,
+};
 
 /// Normalize a path, removing things like `.` and `..`, this snippet is taken from cargo's paths util.
 /// https://github.com/rust-lang/cargo/blob/46fa867ff7043e3a0545bf3def7be904e1497afd/crates/cargo-util/src/paths.rs#L73-L106
@@ -191,3 +197,51 @@ pub fn basename(path: String, ext: Option<String>) -> Result<String> {
 pub fn is_absolute(path: String) -> bool {
   Path::new(&path).is_absolute()
 }
+
+#[derive(Template)]
+#[default_template("./init.js")]
+struct InitJavascript {
+  sep: &'static str,
+  delimiter: &'static str,
+}
+
+/// Initializes the plugin.
+pub(crate) fn init<R: Runtime>() -> TauriPlugin<R> {
+  #[cfg(windows)]
+  let (sep, delimiter) = ("\\", ";");
+  #[cfg(not(windows))]
+  let (sep, delimiter) = ("/", ":");
+
+  let init_js = InitJavascript { sep, delimiter }
+    .render_default(&Default::default())
+    // this will never fail with the above sep and delimiter values
+    .unwrap();
+
+  Builder::new("path")
+    .invoke_handler(crate::generate_handler![
+      resolve_directory,
+      resolve,
+      normalize,
+      join,
+      dirname,
+      extname,
+      basename,
+      is_absolute
+    ])
+    .js_init_script(init_js.to_string())
+    .setup(|app, _api| {
+      #[cfg(target_os = "android")]
+      {
+        let handle = _api.register_android_plugin("app.tauri", "PathPlugin")?;
+        app.manage(PathResolver(handle));
+      }
+
+      #[cfg(not(target_os = "android"))]
+      {
+        app.manage(PathResolver(app.clone()));
+      }
+
+      Ok(())
+    })
+    .build()
+}