mirror of
https://github.com/tauri-apps/tauri.git
synced 2024-08-17 03:40:23 +03:00
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:
parent
5b769948a8
commit
f44a2ec47c
6
.changes/cli-plugin-template-updates.md
Normal file
6
.changes/cli-plugin-template-updates.md
Normal 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.
|
@ -239,9 +239,17 @@ pub fn command(mut options: Options) -> Result<()> {
|
|||||||
.with_context(|| "failed to render plugin Android template")?;
|
.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")?;
|
.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(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -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"
|
||||||
|
]
|
||||||
|
}
|
@ -1,15 +1,15 @@
|
|||||||
<script>
|
<script>
|
||||||
import Greet from './lib/Greet.svelte'
|
import Greet from './lib/Greet.svelte'
|
||||||
import { execute } from 'tauri-plugin-{{ plugin_name }}-api'
|
import { ping } from 'tauri-plugin-{{ plugin_name }}-api'
|
||||||
|
|
||||||
let response = ''
|
let response = ''
|
||||||
|
|
||||||
function updateResponse(returnValue) {
|
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() {
|
function _ping() {
|
||||||
execute().then(updateResponse).catch(updateResponse)
|
ping("Pong!").then(updateResponse).catch(updateResponse)
|
||||||
}
|
}
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
@ -37,7 +37,7 @@
|
|||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div>
|
<div>
|
||||||
<button on:click="{_execute}">Execute</button>
|
<button on:click="{_ping}">Ping</button>
|
||||||
<div>{@html response}</div>
|
<div>{@html response}</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
@ -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"
|
||||||
|
]
|
||||||
|
}
|
@ -1,4 +1,4 @@
|
|||||||
const COMMANDS: &[&str] = &["ping", "execute"];
|
const COMMANDS: &[&str] = &["ping"];
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
tauri_plugin::Builder::new(COMMANDS)
|
tauri_plugin::Builder::new(COMMANDS)
|
||||||
|
@ -3,6 +3,10 @@
|
|||||||
{{/if}}
|
{{/if}}
|
||||||
import { invoke } from '@tauri-apps/api/core'
|
import { invoke } from '@tauri-apps/api/core'
|
||||||
|
|
||||||
export async function execute() {
|
export async function ping(value: string): Promise<string | null> {
|
||||||
await invoke('plugin:{{ plugin_name }}|execute')
|
return await invoke<{value?: string}>('plugin:{{ plugin_name }}|ping', {
|
||||||
|
payload: {
|
||||||
|
value,
|
||||||
|
},
|
||||||
|
}).then((r) => (r.value ? r.value : null));
|
||||||
}
|
}
|
||||||
|
@ -1,16 +1,16 @@
|
|||||||
{{#if license_header}}
|
{{#if license_header}}
|
||||||
{{ license_header }}
|
{{ license_header }}
|
||||||
{{/if}}
|
{{/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]
|
#[command]
|
||||||
pub(crate) async fn execute<R: Runtime>(
|
pub(crate) async fn ping<R: Runtime>(
|
||||||
_app: AppHandle<R>,
|
app: AppHandle<R>,
|
||||||
_window: Window<R>,
|
payload: PingRequest,
|
||||||
state: State<'_, MyState>,
|
) -> Result<PingResponse> {
|
||||||
) -> Result<String> {
|
app.{{ plugin_name_snake_case }}().ping(payload)
|
||||||
state.0.lock().unwrap().insert("key".into(), "value".into());
|
|
||||||
Ok("success".to_string())
|
|
||||||
}
|
}
|
||||||
|
@ -6,8 +6,6 @@ use tauri::{
|
|||||||
Manager, Runtime,
|
Manager, Runtime,
|
||||||
};
|
};
|
||||||
|
|
||||||
use std::{collections::HashMap, sync::Mutex};
|
|
||||||
|
|
||||||
pub use models::*;
|
pub use models::*;
|
||||||
|
|
||||||
#[cfg(desktop)]
|
#[cfg(desktop)]
|
||||||
@ -26,9 +24,6 @@ use desktop::{{ plugin_name_pascal_case }};
|
|||||||
#[cfg(mobile)]
|
#[cfg(mobile)]
|
||||||
use mobile::{{ plugin_name_pascal_case }};
|
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.
|
/// Extensions to [`tauri::App`], [`tauri::AppHandle`] and [`tauri::Window`] to access the {{ plugin_name }} APIs.
|
||||||
pub trait {{ plugin_name_pascal_case }}Ext<R: Runtime> {
|
pub trait {{ plugin_name_pascal_case }}Ext<R: Runtime> {
|
||||||
fn {{ plugin_name_snake_case }}(&self) -> &{{ plugin_name_pascal_case }}<R>;
|
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.
|
/// Initializes the plugin.
|
||||||
pub fn init<R: Runtime>() -> TauriPlugin<R> {
|
pub fn init<R: Runtime>() -> TauriPlugin<R> {
|
||||||
Builder::new("{{ plugin_name }}")
|
Builder::new("{{ plugin_name }}")
|
||||||
.invoke_handler(tauri::generate_handler![commands::execute])
|
.invoke_handler(tauri::generate_handler![commands::ping])
|
||||||
.setup(|app, api| {
|
.setup(|app, api| {
|
||||||
#[cfg(mobile)]
|
#[cfg(mobile)]
|
||||||
let {{ plugin_name_snake_case }} = mobile::init(app, api)?;
|
let {{ plugin_name_snake_case }} = mobile::init(app, api)?;
|
||||||
#[cfg(desktop)]
|
#[cfg(desktop)]
|
||||||
let {{ plugin_name_snake_case }} = desktop::init(app, api)?;
|
let {{ plugin_name_snake_case }} = desktop::init(app, api)?;
|
||||||
app.manage({{ plugin_name_snake_case }});
|
app.manage({{ plugin_name_snake_case }});
|
||||||
|
|
||||||
// manage state so it is accessible by the commands
|
|
||||||
app.manage(MyState::default());
|
|
||||||
Ok(())
|
Ok(())
|
||||||
})
|
})
|
||||||
.build()
|
.build()
|
||||||
|
@ -3,13 +3,13 @@
|
|||||||
{{/if}}
|
{{/if}}
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
#[derive(Debug, Serialize)]
|
#[derive(Debug, Deserialize, Serialize)]
|
||||||
#[serde(rename_all = "camelCase")]
|
#[serde(rename_all = "camelCase")]
|
||||||
pub struct PingRequest {
|
pub struct PingRequest {
|
||||||
pub value: Option<String>,
|
pub value: Option<String>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, Default, Deserialize)]
|
#[derive(Debug, Clone, Default, Deserialize, Serialize)]
|
||||||
#[serde(rename_all = "camelCase")]
|
#[serde(rename_all = "camelCase")]
|
||||||
pub struct PingResponse {
|
pub struct PingResponse {
|
||||||
pub value: Option<String>,
|
pub value: Option<String>,
|
||||||
|
Loading…
Reference in New Issue
Block a user