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
This commit is contained in:
Amr Bashir 2024-06-11 07:06:46 +03:00 committed by GitHub
parent 5b769948a8
commit f44a2ec47c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
10 changed files with 75 additions and 29 deletions

View File

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

View File

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

View File

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

View File

@ -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>
@ -37,7 +37,7 @@
</div>
<div>
<button on:click="{_execute}">Execute</button>
<button on:click="{_ping}">Ping</button>
<div>{@html response}</div>
</div>

View File

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

View File

@ -1,4 +1,4 @@
const COMMANDS: &[&str] = &["ping", "execute"];
const COMMANDS: &[&str] = &["ping"];
fn main() {
tauri_plugin::Builder::new(COMMANDS)

View File

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

View File

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

View File

@ -6,8 +6,6 @@ use tauri::{
Manager, Runtime,
};
use std::{collections::HashMap, sync::Mutex};
pub use models::*;
#[cfg(desktop)]
@ -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>;
@ -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()

View File

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