Skip to content

Commit

Permalink
feat(cli): include default.toml and capabilities in plugin template (#…
Browse files Browse the repository at this point in the history
…10030)

* feat(cli): include default.toml and capabilities in plugin template

* replace execute usage with ping

* add to capabilities

* use default permission set
  • Loading branch information
amrbashir authored Jun 11, 2024
1 parent 5b76994 commit f44a2ec
Show file tree
Hide file tree
Showing 10 changed files with 75 additions and 29 deletions.
6 changes: 6 additions & 0 deletions .changes/cli-plugin-template-updates.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"tauri-cli": "patch:enhance"
"@tauri-apps/cli": "patch:enhance"
---

Enhance the plugin template to include `permissions/default.toml` and default capabilities file for the example application.
10 changes: 9 additions & 1 deletion tooling/cli/src/plugin/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -239,9 +239,17 @@ pub fn command(mut options: Options) -> Result<()> {
.with_context(|| "failed to render plugin Android template")?;
}

std::fs::create_dir(template_target_path.join("permissions"))
let permissions_dir = template_target_path.join("permissions");
std::fs::create_dir(&permissions_dir)
.with_context(|| "failed to create `permissions` directory")?;

let default_permissions = r#"[default]
description = "Default permissions for the plugin"
permissions = ["allow-ping"]
"#;
std::fs::write(permissions_dir.join("default.toml"), default_permissions)
.with_context(|| "failed to write `permissions/default.toml`")?;

Ok(())
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"$schema": "../gen/schemas/desktop-schema.json",
"identifier": "default",
"description": "enables the default permissions",
"windows": ["main"],
"permissions": [
"path:default",
"event:default",
"window:default",
"webview:default",
"app:default",
"resources:default",
"image:default",
"menu:default",
"tray:default",
"{{ plugin_name }}:default"
]
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
<script>
import Greet from './lib/Greet.svelte'
import { execute } from 'tauri-plugin-{{ plugin_name }}-api'
import { ping } from 'tauri-plugin-{{ plugin_name }}-api'
let response = ''
function updateResponse(returnValue) {
response += `[${new Date().toLocaleTimeString()}]` + (typeof returnValue === 'string' ? returnValue : JSON.stringify(returnValue)) + '<br>'
response += `[${new Date().toLocaleTimeString()}] ` + (typeof returnValue === 'string' ? returnValue : JSON.stringify(returnValue)) + '<br>'
}
function _execute() {
execute().then(updateResponse).catch(updateResponse)
function _ping() {
ping("Pong!").then(updateResponse).catch(updateResponse)
}
</script>

Expand Down Expand Up @@ -37,7 +37,7 @@
</div>

<div>
<button on:click="{_execute}">Execute</button>
<button on:click="{_ping}">Ping</button>
<div>{@html response}</div>
</div>

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"$schema": "../gen/schemas/desktop-schema.json",
"identifier": "default",
"description": "enables the default permissions",
"windows": ["main"],
"permissions": [
"path:default",
"event:default",
"window:default",
"webview:default",
"app:default",
"resources:default",
"image:default",
"menu:default",
"tray:default",
"{{ plugin_name }}:default"
]
}
2 changes: 1 addition & 1 deletion tooling/cli/templates/plugin/build.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const COMMANDS: &[&str] = &["ping", "execute"];
const COMMANDS: &[&str] = &["ping"];

fn main() {
tauri_plugin::Builder::new(COMMANDS)
Expand Down
8 changes: 6 additions & 2 deletions tooling/cli/templates/plugin/guest-js/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
{{/if}}
import { invoke } from '@tauri-apps/api/core'

export async function execute() {
await invoke('plugin:{{ plugin_name }}|execute')
export async function ping(value: string): Promise<string | null> {
return await invoke<{value?: string}>('plugin:{{ plugin_name }}|ping', {
payload: {
value,
},
}).then((r) => (r.value ? r.value : null));
}
18 changes: 9 additions & 9 deletions tooling/cli/templates/plugin/src/commands.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
{{#if license_header}}
{{ license_header }}
{{/if}}
use tauri::{AppHandle, command, Runtime, State, Window};
use tauri::{AppHandle, command, Runtime};

use crate::{MyState, Result};
use crate::models::*;
use crate::Result;
use crate::{{ plugin_name_pascal_case }}Ext;

#[command]
pub(crate) async fn execute<R: Runtime>(
_app: AppHandle<R>,
_window: Window<R>,
state: State<'_, MyState>,
) -> Result<String> {
state.0.lock().unwrap().insert("key".into(), "value".into());
Ok("success".to_string())
pub(crate) async fn ping<R: Runtime>(
app: AppHandle<R>,
payload: PingRequest,
) -> Result<PingResponse> {
app.{{ plugin_name_snake_case }}().ping(payload)
}
10 changes: 1 addition & 9 deletions tooling/cli/templates/plugin/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ use tauri::{
Manager, Runtime,
};

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

pub use models::*;

#[cfg(desktop)]
Expand All @@ -26,9 +24,6 @@ use desktop::{{ plugin_name_pascal_case }};
#[cfg(mobile)]
use mobile::{{ plugin_name_pascal_case }};

#[derive(Default)]
struct MyState(Mutex<HashMap<String, String>>);

/// Extensions to [`tauri::App`], [`tauri::AppHandle`] and [`tauri::Window`] to access the {{ plugin_name }} APIs.
pub trait {{ plugin_name_pascal_case }}Ext<R: Runtime> {
fn {{ plugin_name_snake_case }}(&self) -> &{{ plugin_name_pascal_case }}<R>;
Expand All @@ -43,16 +38,13 @@ impl<R: Runtime, T: Manager<R>> crate::{{ plugin_name_pascal_case }}Ext<R> for T
/// Initializes the plugin.
pub fn init<R: Runtime>() -> TauriPlugin<R> {
Builder::new("{{ plugin_name }}")
.invoke_handler(tauri::generate_handler![commands::execute])
.invoke_handler(tauri::generate_handler![commands::ping])
.setup(|app, api| {
#[cfg(mobile)]
let {{ plugin_name_snake_case }} = mobile::init(app, api)?;
#[cfg(desktop)]
let {{ plugin_name_snake_case }} = desktop::init(app, api)?;
app.manage({{ plugin_name_snake_case }});

// manage state so it is accessible by the commands
app.manage(MyState::default());
Ok(())
})
.build()
Expand Down
4 changes: 2 additions & 2 deletions tooling/cli/templates/plugin/src/models.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
{{/if}}
use serde::{Deserialize, Serialize};

#[derive(Debug, Serialize)]
#[derive(Debug, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct PingRequest {
pub value: Option<String>,
}

#[derive(Debug, Clone, Default, Deserialize)]
#[derive(Debug, Clone, Default, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct PingResponse {
pub value: Option<String>,
Expand Down

0 comments on commit f44a2ec

Please sign in to comment.