Skip to content

Commit f44a2ec

Browse files
authored
feat(cli): include default.toml and capabilities in plugin template (#10030)
* feat(cli): include default.toml and capabilities in plugin template * replace execute usage with ping * add to capabilities * use default permission set
1 parent 5b76994 commit f44a2ec

File tree

10 files changed

+75
-29
lines changed

10 files changed

+75
-29
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"tauri-cli": "patch:enhance"
3+
"@tauri-apps/cli": "patch:enhance"
4+
---
5+
6+
Enhance the plugin template to include `permissions/default.toml` and default capabilities file for the example application.

tooling/cli/src/plugin/init.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -239,9 +239,17 @@ pub fn command(mut options: Options) -> Result<()> {
239239
.with_context(|| "failed to render plugin Android template")?;
240240
}
241241

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

246+
let default_permissions = r#"[default]
247+
description = "Default permissions for the plugin"
248+
permissions = ["allow-ping"]
249+
"#;
250+
std::fs::write(permissions_dir.join("default.toml"), default_permissions)
251+
.with_context(|| "failed to write `permissions/default.toml`")?;
252+
245253
Ok(())
246254
}
247255

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"$schema": "../gen/schemas/desktop-schema.json",
3+
"identifier": "default",
4+
"description": "enables the default permissions",
5+
"windows": ["main"],
6+
"permissions": [
7+
"path:default",
8+
"event:default",
9+
"window:default",
10+
"webview:default",
11+
"app:default",
12+
"resources:default",
13+
"image:default",
14+
"menu:default",
15+
"tray:default",
16+
"{{ plugin_name }}:default"
17+
]
18+
}

tooling/cli/templates/plugin/__example-api/tauri-app/src/App.svelte

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
<script>
22
import Greet from './lib/Greet.svelte'
3-
import { execute } from 'tauri-plugin-{{ plugin_name }}-api'
3+
import { ping } from 'tauri-plugin-{{ plugin_name }}-api'
44
55
let response = ''
66
77
function updateResponse(returnValue) {
8-
response += `[${new Date().toLocaleTimeString()}]` + (typeof returnValue === 'string' ? returnValue : JSON.stringify(returnValue)) + '<br>'
8+
response += `[${new Date().toLocaleTimeString()}] ` + (typeof returnValue === 'string' ? returnValue : JSON.stringify(returnValue)) + '<br>'
99
}
1010
11-
function _execute() {
12-
execute().then(updateResponse).catch(updateResponse)
11+
function _ping() {
12+
ping("Pong!").then(updateResponse).catch(updateResponse)
1313
}
1414
</script>
1515

@@ -37,7 +37,7 @@
3737
</div>
3838

3939
<div>
40-
<button on:click="{_execute}">Execute</button>
40+
<button on:click="{_ping}">Ping</button>
4141
<div>{@html response}</div>
4242
</div>
4343

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"$schema": "../gen/schemas/desktop-schema.json",
3+
"identifier": "default",
4+
"description": "enables the default permissions",
5+
"windows": ["main"],
6+
"permissions": [
7+
"path:default",
8+
"event:default",
9+
"window:default",
10+
"webview:default",
11+
"app:default",
12+
"resources:default",
13+
"image:default",
14+
"menu:default",
15+
"tray:default",
16+
"{{ plugin_name }}:default"
17+
]
18+
}

tooling/cli/templates/plugin/build.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const COMMANDS: &[&str] = &["ping", "execute"];
1+
const COMMANDS: &[&str] = &["ping"];
22

33
fn main() {
44
tauri_plugin::Builder::new(COMMANDS)

tooling/cli/templates/plugin/guest-js/index.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@
33
{{/if}}
44
import { invoke } from '@tauri-apps/api/core'
55

6-
export async function execute() {
7-
await invoke('plugin:{{ plugin_name }}|execute')
6+
export async function ping(value: string): Promise<string | null> {
7+
return await invoke<{value?: string}>('plugin:{{ plugin_name }}|ping', {
8+
payload: {
9+
value,
10+
},
11+
}).then((r) => (r.value ? r.value : null));
812
}
Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
{{#if license_header}}
22
{{ license_header }}
33
{{/if}}
4-
use tauri::{AppHandle, command, Runtime, State, Window};
4+
use tauri::{AppHandle, command, Runtime};
55

6-
use crate::{MyState, Result};
6+
use crate::models::*;
7+
use crate::Result;
8+
use crate::{{ plugin_name_pascal_case }}Ext;
79

810
#[command]
9-
pub(crate) async fn execute<R: Runtime>(
10-
_app: AppHandle<R>,
11-
_window: Window<R>,
12-
state: State<'_, MyState>,
13-
) -> Result<String> {
14-
state.0.lock().unwrap().insert("key".into(), "value".into());
15-
Ok("success".to_string())
11+
pub(crate) async fn ping<R: Runtime>(
12+
app: AppHandle<R>,
13+
payload: PingRequest,
14+
) -> Result<PingResponse> {
15+
app.{{ plugin_name_snake_case }}().ping(payload)
1616
}

tooling/cli/templates/plugin/src/lib.rs

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@ use tauri::{
66
Manager, Runtime,
77
};
88

9-
use std::{collections::HashMap, sync::Mutex};
10-
119
pub use models::*;
1210

1311
#[cfg(desktop)]
@@ -26,9 +24,6 @@ use desktop::{{ plugin_name_pascal_case }};
2624
#[cfg(mobile)]
2725
use mobile::{{ plugin_name_pascal_case }};
2826

29-
#[derive(Default)]
30-
struct MyState(Mutex<HashMap<String, String>>);
31-
3227
/// Extensions to [`tauri::App`], [`tauri::AppHandle`] and [`tauri::Window`] to access the {{ plugin_name }} APIs.
3328
pub trait {{ plugin_name_pascal_case }}Ext<R: Runtime> {
3429
fn {{ plugin_name_snake_case }}(&self) -> &{{ plugin_name_pascal_case }}<R>;
@@ -43,16 +38,13 @@ impl<R: Runtime, T: Manager<R>> crate::{{ plugin_name_pascal_case }}Ext<R> for T
4338
/// Initializes the plugin.
4439
pub fn init<R: Runtime>() -> TauriPlugin<R> {
4540
Builder::new("{{ plugin_name }}")
46-
.invoke_handler(tauri::generate_handler![commands::execute])
41+
.invoke_handler(tauri::generate_handler![commands::ping])
4742
.setup(|app, api| {
4843
#[cfg(mobile)]
4944
let {{ plugin_name_snake_case }} = mobile::init(app, api)?;
5045
#[cfg(desktop)]
5146
let {{ plugin_name_snake_case }} = desktop::init(app, api)?;
5247
app.manage({{ plugin_name_snake_case }});
53-
54-
// manage state so it is accessible by the commands
55-
app.manage(MyState::default());
5648
Ok(())
5749
})
5850
.build()

tooling/cli/templates/plugin/src/models.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@
33
{{/if}}
44
use serde::{Deserialize, Serialize};
55

6-
#[derive(Debug, Serialize)]
6+
#[derive(Debug, Deserialize, Serialize)]
77
#[serde(rename_all = "camelCase")]
88
pub struct PingRequest {
99
pub value: Option<String>,
1010
}
1111

12-
#[derive(Debug, Clone, Default, Deserialize)]
12+
#[derive(Debug, Clone, Default, Deserialize, Serialize)]
1313
#[serde(rename_all = "camelCase")]
1414
pub struct PingResponse {
1515
pub value: Option<String>,

0 commit comments

Comments
 (0)