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

This commit is contained in:
Lucas Fernandes Nogueira
2022-03-03 17:40:25 -03:00
committed by GitHub
parent 983ccb815b
commit f7acb061e4
6 changed files with 26 additions and 39 deletions

View File

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

View File

@@ -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");
}

View File

@@ -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()
}

View File

@@ -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"

View File

@@ -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");
}

View File

@@ -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()
}