automatically define a capability for each plugin feature

This commit is contained in:
Lucas Nogueira 2023-07-31 08:00:15 -03:00
parent d894ad2896
commit 13a774e0ca
No known key found for this signature in database
GPG Key ID: FFEA6C72E73482F1
5 changed files with 58 additions and 58 deletions

View File

@ -2,6 +2,7 @@
// SPDX-License-Identifier: Apache-2.0
// SPDX-License-Identifier: MIT
use tauri_utils::plugin::Capability;
pub use tauri_utils::plugin::{Manifest, ManifestMap, ScopeType};
use std::{
@ -13,7 +14,24 @@ use std::{
const PLUGIN_METADATA_KEY: &str = "PLUGIN_MANIFEST_PATH";
pub fn set_manifest(manifest: Manifest) {
pub fn set_manifest(mut manifest: Manifest) {
for feature in &manifest.features {
let feature_capability_id = format!("allow-{feature}");
if !manifest
.capabilities
.iter()
.any(|c| c.id == feature_capability_id)
{
manifest.capabilities.push(Capability {
id: feature_capability_id,
component: None,
description: format!("Allows the {feature} functionality"),
features: vec![feature.clone()],
scope: Default::default(),
});
}
}
let manifest_str = serde_json::to_string(&manifest).expect("failed to serialize plugin manifest");
let manifest_path = var_os("OUT_DIR")
.map(PathBuf::from)

View File

@ -133,9 +133,7 @@ fn main() {
}
tauri_build::plugin::set_manifest(
tauri_build::plugin::Manifest::new("event")
.features(["emit", "listen"])
.capability_json(include_str!("./capabilities/event.json")),
tauri_build::plugin::Manifest::new("event").features(["emit", "listen"]),
);
tauri_build::plugin::set_manifest(tauri_build::plugin::Manifest::new("path"));

View File

@ -1,16 +0,0 @@
[
{
"id": "allow-event-emit",
"description": "Allows the event's emit function",
"features": [
"emit"
]
},
{
"id": "allow-event-listen",
"description": "Allows the event's listen function",
"features": [
"listen"
]
}
]

View File

@ -111,6 +111,6 @@
"id": "main",
"description": "Main window namespace",
"members": ["main"],
"capabilities": ["allow-all-api-commands", "allow-ping", "allow-event-emit", "allow-event-listen"]
"capabilities": ["allow-all-api-commands", "allow-ping", "allow-emit", "allow-listen"]
}]
}

View File

@ -10,12 +10,47 @@
"capabilities": [
"allow-all-api-commands",
"allow-ping",
"allow-event-emit",
"allow-event-listen"
"allow-emit",
"allow-listen"
]
}
],
"plugins": {
"event": {
"plugin": "event",
"default_capability": null,
"capabilities": [
{
"id": "allow-emit",
"component": null,
"description": "Allows the emit functionality",
"features": [
"emit"
],
"scope": {
"allowed": [],
"blocked": []
}
},
{
"id": "allow-listen",
"component": null,
"description": "Allows the listen functionality",
"features": [
"listen"
],
"scope": {
"allowed": [],
"blocked": []
}
}
],
"features": [
"emit",
"listen"
],
"scope_type": []
},
"__app__": {
"default_capability": null,
"capabilities": [
@ -36,41 +71,6 @@
"features": [],
"scope_type": []
},
"event": {
"plugin": "event",
"default_capability": null,
"capabilities": [
{
"id": "allow-event-emit",
"component": null,
"description": "Allows the event's emit function",
"features": [
"emit"
],
"scope": {
"allowed": [],
"blocked": []
}
},
{
"id": "allow-event-listen",
"component": null,
"description": "Allows the event's listen function",
"features": [
"listen"
],
"scope": {
"allowed": [],
"blocked": []
}
}
],
"features": [
"emit",
"listen"
],
"scope_type": []
},
"path": {
"plugin": "path",
"default_capability": null,