Skip to content

Commit

Permalink
feat(cli): use plugin::Builder syntax on the plugin template (#3606)
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasfernog committed Mar 3, 2022
1 parent 983ccb8 commit f7acb06
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 39 deletions.
6 changes: 6 additions & 0 deletions .changes/cli.rs-template-plugin-builder.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"cli.rs": patch
"cli.js": patch
---

Change the `plugin init` templates to use the new `tauri::plugin::Builder` syntax.
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
12 changes: 4 additions & 8 deletions tooling/cli/templates/plugin/backend/src/lib.rs
Original file line number Diff line number Diff line change
@@ -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()
}
1 change: 0 additions & 1 deletion tooling/cli/templates/plugin/with-api/Cargo.crate-manifest
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
42 changes: 14 additions & 28 deletions tooling/cli/templates/plugin/with-api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};

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

0 comments on commit f7acb06

Please sign in to comment.