Skip to content

Commit f7acb06

Browse files
authored
feat(cli): use plugin::Builder syntax on the plugin template (#3606)
1 parent 983ccb8 commit f7acb06

File tree

6 files changed

+26
-39
lines changed

6 files changed

+26
-39
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"cli.rs": patch
3+
"cli.js": patch
4+
---
5+
6+
Change the `plugin init` templates to use the new `tauri::plugin::Builder` syntax.

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
fn main() {
77
tauri::Builder::default()
8-
.plugin(tauri_plugin_{{ plugin_name_snake_case }}::YourPlugin::default())
8+
.plugin(tauri_plugin_{{ plugin_name_snake_case }}::init())
99
.run(tauri::generate_context!())
1010
.expect("error while running tauri application");
1111
}
Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,9 @@
11
{{#if license_header}}
22
{{ license_header }}
33
{{/if}}
4-
use tauri::{plugin::Plugin, Runtime};
4+
use tauri::{plugin::{Builder, TauriPlugin}, runtime::Runtime};
55

6-
#[derive(Default)]
7-
pub struct YourPlugin {}
8-
9-
impl<R: Runtime> Plugin<R> for YourPlugin {
10-
fn name(&self) -> &'static str {
11-
"{{ plugin_name }}"
12-
}
6+
/// Initializes the plugin.
7+
pub fn init<R: Runtime>() -> TauriPlugin<R> {
8+
Builder::new("{{ plugin_name }}").build()
139
}

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,4 @@ exclude = ["/examples", "/webview-dist", "/webview-src", "node_modules"]
1010
[dependencies]
1111
tauri = {{{ tauri_dep }}}
1212
serde = "1.0"
13-
serde_json = "1.0"
1413
thiserror = "1.0"

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
fn main() {
77
tauri::Builder::default()
8-
.plugin(tauri_plugin_{{ plugin_name_snake_case }}::YourPlugin::default())
8+
.plugin(tauri_plugin_{{ plugin_name_snake_case }}::init())
99
.run(tauri::generate_context!())
1010
.expect("failed to run app");
1111
}

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

Lines changed: 14 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,11 @@
33
{{/if}}
44

55
use serde::{ser::Serializer, Serialize};
6-
use serde_json::Value as JsonValue;
7-
use tauri::{command, plugin::Plugin, AppHandle, Invoke, Manager, Runtime, State, Window};
6+
use tauri::{
7+
command,
8+
plugin::{Builder, TauriPlugin},
9+
AppHandle, Manager, Runtime, State, Window,
10+
};
811

912
use std::{collections::HashMap, sync::Mutex};
1013

@@ -38,30 +41,13 @@ async fn execute<R: Runtime>(
3841
Ok("success".to_string())
3942
}
4043

41-
/// Tauri plugin.
42-
pub struct YourPlugin<R: Runtime> {
43-
invoke_handler: Box<dyn Fn(Invoke<R>) + Send + Sync>,
44-
}
45-
46-
impl<R: Runtime> Default for YourPlugin<R> {
47-
fn default() -> Self {
48-
Self {
49-
invoke_handler: Box::new(tauri::generate_handler![execute]),
50-
}
51-
}
52-
}
53-
54-
impl<R: Runtime> Plugin<R> for YourPlugin<R> {
55-
fn name(&self) -> &'static str {
56-
"{{ plugin_name }}"
57-
}
58-
59-
fn initialize(&mut self, app: &AppHandle<R>, _config: JsonValue) -> tauri::plugin::Result<()> {
60-
app.manage(MyState::default());
61-
Ok(())
62-
}
63-
64-
fn extend_api(&mut self, message: Invoke<R>) {
65-
(self.invoke_handler)(message)
66-
}
44+
/// Initializes the plugin.
45+
pub fn init<R: Runtime>() -> TauriPlugin<R> {
46+
Builder::new("{{ plugin_name }}")
47+
.invoke_handler(tauri::generate_handler![execute])
48+
.setup(|app| {
49+
app.manage(MyState::default());
50+
Ok(())
51+
})
52+
.build()
6753
}

0 commit comments

Comments
 (0)