Prechádzať zdrojové kódy

automatically define a capability for each plugin feature

Lucas Nogueira 2 rokov pred
rodič
commit
13a774e0ca

+ 19 - 1
core/tauri-build/src/plugin.rs

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

+ 1 - 3
core/tauri/build.rs

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

+ 0 - 16
core/tauri/capabilities/event.json

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

+ 1 - 1
examples/api/src-tauri/tauri.conf.json

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

+ 26 - 26
examples/api/src-tauri/tauri.namespace.lock

@@ -10,40 +10,20 @@
       "capabilities": [
         "allow-all-api-commands",
         "allow-ping",
-        "allow-event-emit",
-        "allow-event-listen"
+        "allow-emit",
+        "allow-listen"
       ]
     }
   ],
   "plugins": {
-    "__app__": {
-      "default_capability": null,
-      "capabilities": [
-        {
-          "id": "allow-all-api-commands",
-          "component": null,
-          "description": "Allows all application defined commands",
-          "features": [
-            "log_operation",
-            "perform_request"
-          ],
-          "scope": {
-            "allowed": [],
-            "blocked": []
-          }
-        }
-      ],
-      "features": [],
-      "scope_type": []
-    },
     "event": {
       "plugin": "event",
       "default_capability": null,
       "capabilities": [
         {
-          "id": "allow-event-emit",
+          "id": "allow-emit",
           "component": null,
-          "description": "Allows the event's emit function",
+          "description": "Allows the emit functionality",
           "features": [
             "emit"
           ],
@@ -53,9 +33,9 @@
           }
         },
         {
-          "id": "allow-event-listen",
+          "id": "allow-listen",
           "component": null,
-          "description": "Allows the event's listen function",
+          "description": "Allows the listen functionality",
           "features": [
             "listen"
           ],
@@ -71,6 +51,26 @@
       ],
       "scope_type": []
     },
+    "__app__": {
+      "default_capability": null,
+      "capabilities": [
+        {
+          "id": "allow-all-api-commands",
+          "component": null,
+          "description": "Allows all application defined commands",
+          "features": [
+            "log_operation",
+            "perform_request"
+          ],
+          "scope": {
+            "allowed": [],
+            "blocked": []
+          }
+        }
+      ],
+      "features": [],
+      "scope_type": []
+    },
     "path": {
       "plugin": "path",
       "default_capability": null,