diff --git a/core/tauri/src/app.rs b/core/tauri/src/app.rs index ab65a96e2..b80f11744 100644 --- a/core/tauri/src/app.rs +++ b/core/tauri/src/app.rs @@ -807,8 +807,8 @@ shared_app_impl!(AppHandle); impl App { 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(()) diff --git a/core/tauri/src/event/mod.rs b/core/tauri/src/event/mod.rs index 7df0f7f25..decb189b2 100644 --- a/core/tauri/src/event/mod.rs +++ b/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() -> TauriPlugin { - 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!( " diff --git a/core/tauri/src/event/commands.rs b/core/tauri/src/event/plugin.rs similarity index 91% rename from core/tauri/src/event/commands.rs rename to core/tauri/src/event/plugin.rs index 42d79bb0e..236c1a6eb 100644 --- a/core/tauri/src/event/commands.rs +++ b/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( window.emit_all(&event.0, payload) } } + +/// Initializes the event plugin. +pub(crate) fn init() -> TauriPlugin { + Builder::new("event") + .invoke_handler(crate::generate_handler![listen, unlisten, emit,]) + .build() +} diff --git a/core/tauri/src/path/mod.rs b/core/tauri/src/path/mod.rs index 795884738..f51f7ac2b 100644 --- a/core/tauri/src/path/mod.rs +++ b/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( 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() -> TauriPlugin { - #[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; diff --git a/core/tauri/src/path/commands.rs b/core/tauri/src/path/plugin.rs similarity index 82% rename from core/tauri/src/path/commands.rs rename to core/tauri/src/path/plugin.rs index 8a343c5fe..ff4326d9e 100644 --- a/core/tauri/src/path/commands.rs +++ b/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) -> Result { 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() -> TauriPlugin { + #[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() +}