ソースを参照

feat(cli): use plugin::Builder syntax on the plugin template (#3606)

Lucas Fernandes Nogueira 3 年 前
コミット
f7acb061e4

+ 6 - 0
.changes/cli.rs-template-plugin-builder.md

@@ -0,0 +1,6 @@
+---
+"cli.rs": patch
+"cli.js": patch
+---
+
+Change the `plugin init` templates to use the new `tauri::plugin::Builder` syntax.

+ 1 - 1
tooling/cli/templates/plugin/backend/examples/vanilla/src-tauri/src/main.rs

@@ -5,7 +5,7 @@
 
 fn main() {
   tauri::Builder::default()
-    .plugin(tauri_plugin_{{ plugin_name_snake_case }}::YourPlugin::default())
+    .plugin(tauri_plugin_{{ plugin_name_snake_case }}::init())
     .run(tauri::generate_context!())
     .expect("error while running tauri application");
 }

+ 4 - 8
tooling/cli/templates/plugin/backend/src/lib.rs

@@ -1,13 +1,9 @@
 {{#if license_header}}
 {{ license_header }}
 {{/if}}
-use tauri::{plugin::Plugin, Runtime};
+use tauri::{plugin::{Builder, TauriPlugin}, runtime::Runtime};
 
-#[derive(Default)]
-pub struct YourPlugin {}
-
-impl<R: Runtime> Plugin<R> for YourPlugin {
-  fn name(&self) -> &'static str {
-    "{{ plugin_name }}"
-  }
+/// Initializes the plugin.
+pub fn init<R: Runtime>() -> TauriPlugin<R> {
+  Builder::new("{{ plugin_name }}").build()
 }

+ 0 - 1
tooling/cli/templates/plugin/with-api/Cargo.crate-manifest

@@ -10,5 +10,4 @@ exclude = ["/examples", "/webview-dist", "/webview-src", "node_modules"]
 [dependencies]
 tauri = {{{  tauri_dep }}}
 serde = "1.0"
-serde_json = "1.0"
 thiserror = "1.0"

+ 1 - 1
tooling/cli/templates/plugin/with-api/examples/svelte-app/src-tauri/src/main.rs

@@ -5,7 +5,7 @@
 
 fn main() {
   tauri::Builder::default()
-    .plugin(tauri_plugin_{{ plugin_name_snake_case }}::YourPlugin::default())
+    .plugin(tauri_plugin_{{ plugin_name_snake_case }}::init())
     .run(tauri::generate_context!())
     .expect("failed to run app");
 }

+ 14 - 28
tooling/cli/templates/plugin/with-api/src/lib.rs

@@ -3,8 +3,11 @@
 {{/if}}
 
 use serde::{ser::Serializer, Serialize};
-use serde_json::Value as JsonValue;
-use tauri::{command, plugin::Plugin, AppHandle, Invoke, Manager, Runtime, State, Window};
+use tauri::{
+  command,
+  plugin::{Builder, TauriPlugin},
+  AppHandle, Manager, Runtime, State, Window,
+};
 
 use std::{collections::HashMap, sync::Mutex};
 
@@ -38,30 +41,13 @@ async fn execute<R: Runtime>(
   Ok("success".to_string())
 }
 
-/// Tauri plugin.
-pub struct YourPlugin<R: Runtime> {
-  invoke_handler: Box<dyn Fn(Invoke<R>) + Send + Sync>,
-}
-
-impl<R: Runtime> Default for YourPlugin<R> {
-  fn default() -> Self {
-    Self {
-      invoke_handler: Box::new(tauri::generate_handler![execute]),
-    }
-  }
-}
-
-impl<R: Runtime> Plugin<R> for YourPlugin<R> {
-  fn name(&self) -> &'static str {
-    "{{ plugin_name }}"
-  }
-
-  fn initialize(&mut self, app: &AppHandle<R>, _config: JsonValue) -> tauri::plugin::Result<()> {
-    app.manage(MyState::default());
-    Ok(())
-  }
-
-  fn extend_api(&mut self, message: Invoke<R>) {
-    (self.invoke_handler)(message)
-  }
+/// Initializes the plugin.
+pub fn init<R: Runtime>() -> TauriPlugin<R> {
+  Builder::new("{{ plugin_name }}")
+    .invoke_handler(tauri::generate_handler![execute])
+    .setup(|app| {
+      app.manage(MyState::default());
+      Ok(())
+    })
+    .build()
 }