浏览代码

allow the capability JSON to have a list of capabilities

Lucas Nogueira 2 年之前
父节点
当前提交
d894ad2896

+ 28 - 5
core/tauri-utils/src/plugin.rs

@@ -53,6 +53,13 @@ pub struct Capability {
   pub scope: CapabilityScope,
 }
 
+#[derive(Serialize, Deserialize)]
+#[serde(untagged)]
+enum CapabilityOrList {
+  Single(Capability),
+  List(Vec<Capability>),
+}
+
 /// Plugin manifest.
 #[derive(Debug, Serialize, Deserialize)]
 pub struct Manifest {
@@ -81,8 +88,8 @@ impl Manifest {
     }
   }
 
-  /// Sets the plugin's default capability set.
-  pub fn default_capability(mut self, default_capability: impl AsRef<str>) -> Self {
+  /// Sets the plugin's default capability set from a JSON string.
+  pub fn default_capability_json(mut self, default_capability: impl AsRef<str>) -> Self {
     let mut capability: Capability = serde_json::from_str(default_capability.as_ref())
       .expect("failed to deserialize default capability");
     assert!(
@@ -94,10 +101,18 @@ impl Manifest {
     self
   }
 
-  /// Appends a capability JSON. See [`Capability`].
-  pub fn capability(mut self, capability: impl AsRef<str>) -> Self {
-    let capability: Capability =
+  /// Appends a capability from a JSON string. The JSON can also include an array of capabilities instead of a single one. See [`Capability`].
+  pub fn capability_json(self, capability: impl AsRef<str>) -> Self {
+    let capability =
       serde_json::from_str(capability.as_ref()).expect("failed to deserialize default capability");
+    match capability {
+      CapabilityOrList::Single(cap) => self.capability(cap),
+      CapabilityOrList::List(l) => self.capabilities(l),
+    }
+  }
+
+  /// Appends a [`Capability`].
+  pub fn capability(mut self, capability: Capability) -> Self {
     assert!(
       !capability.id.is_empty(),
       "capability must have an identifier"
@@ -106,6 +121,14 @@ impl Manifest {
     self
   }
 
+  /// Appends the given list of capabilities. See [`Self::capability`].
+  pub fn capabilities<I: IntoIterator<Item = Capability>>(mut self, capabilities: I) -> Self {
+    for capability in capabilities {
+      self = self.capability(capability);
+    }
+    self
+  }
+
   /// Appends the given feature on the list of plugin's features.
   pub fn feature(mut self, feature: impl Into<String>) -> Self {
     self.features.push(feature.into());

+ 1 - 2
core/tauri/build.rs

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

+ 0 - 5
core/tauri/capabilities/allow-event-emit.json

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

+ 0 - 5
core/tauri/capabilities/allow-event-listen.json

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

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

@@ -0,0 +1,16 @@
+[
+  {
+    "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"
+    ]
+  }
+]

+ 4 - 0
examples/api/src-tauri/Cargo.lock

@@ -3272,6 +3272,8 @@ dependencies = [
 [[package]]
 name = "tao"
 version = "0.21.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "436fb014010f6c87561125b14add8a6091354681f190bb9ffeb42819af9218a4"
 dependencies = [
  "bitflags 1.3.2",
  "cairo-rs",
@@ -3321,6 +3323,8 @@ dependencies = [
 [[package]]
 name = "tao-macros"
 version = "0.1.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "3b27a4bcc5eb524658234589bdffc7e7bfb996dbae6ce9393bfd39cb4159b445"
 dependencies = [
  "proc-macro2",
  "quote",

+ 0 - 1
examples/api/src-tauri/Cargo.toml

@@ -27,7 +27,6 @@ tauri-plugin-cli = { git = "https://github.com/tauri-apps/plugins-workspace", br
 [patch.crates-io]
 tauri = { path = "../../../core/tauri" }
 tauri-build = { path = "../../../core/tauri-build" }
-tao = { path = "../../../../tao" }
 
 [patch.'https://github.com/tauri-apps/tauri']
 tauri = { path = "../../../core/tauri" }

+ 2 - 2
examples/api/src-tauri/tauri-plugin-sample/build.rs

@@ -20,8 +20,8 @@ fn main() {
 
   set_manifest(
     Manifest::new("sample")
-      .default_capability(include_str!("capabilities/default.json"))
-      .capability(include_str!("capabilities/ping.json"))
+      .default_capability_json(include_str!("capabilities/default.json"))
+      .capability_json(include_str!("capabilities/ping.json"))
       .feature("ping")
       .scope_type(ScopeType::String),
   );

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

@@ -16,6 +16,26 @@
     }
   ],
   "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,
@@ -51,26 +71,6 @@
       ],
       "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,