Parcourir la source

resolve conflicts on capability ids

Lucas Nogueira il y a 2 ans
Parent
commit
cb78c12db8
2 fichiers modifiés avec 44 ajouts et 14 suppressions
  1. 38 10
      core/tauri-build/src/lib.rs
  2. 6 4
      core/tauri-utils/src/plugin.rs

+ 38 - 10
core/tauri-build/src/lib.rs

@@ -521,19 +521,47 @@ pub fn try_build(attributes: Attributes) -> Result<()> {
           });
 
       for capability in &namespace.capabilities {
-        let (plugin, capability) = manifests.find_capability(capability).unwrap_or_else(|| {
+        let (target_plugin, capability_id) = capability
+          .split_once(':')
+          .map(|(plugin, id)| (Some(plugin), id))
+          .unwrap_or_else(|| (None, capability.as_str()));
+        let capabilities = manifests.find_capability(capability_id);
+
+        if capabilities.is_empty() {
           panic!("could not find capability specification matching id {capability}")
-        });
-        if plugin == APP_MANIFEST_KEY {
-          member_resolution.commands.extend(capability.features);
         } else {
-          member_resolution.commands.extend(
-            capability
-              .features
+          let (plugin, capability) = if let Some(target) = target_plugin {
+            capabilities
               .into_iter()
-              .map(|f| format!("plugin:{plugin}|{f}"))
-              .collect::<Vec<_>>(),
-          );
+              .find(|(p, _)| p == target)
+              .unwrap_or_else(|| {
+                panic!("failed to find capability matching id {capability_id} for plugin {target}")
+              })
+          } else if capabilities.len() > 1 {
+            panic!(
+              "found a conflict on capability id {capability}, please use one of the [{}] prefixes",
+              capabilities
+                .iter()
+                .map(|(p, _)| format!("'{p}:'"))
+                .collect::<Vec<String>>()
+                .join(", ")
+            );
+          } else {
+            // already checked that the capabilities aren't empty
+            capabilities.into_iter().next().unwrap()
+          };
+
+          if plugin == APP_MANIFEST_KEY {
+            member_resolution.commands.extend(capability.features);
+          } else {
+            member_resolution.commands.extend(
+              capability
+                .features
+                .into_iter()
+                .map(|f| format!("plugin:{plugin}|{f}"))
+                .collect::<Vec<_>>(),
+            );
+          }
         }
       }
     }

+ 6 - 4
core/tauri-utils/src/plugin.rs

@@ -176,21 +176,23 @@ impl From<HashMap<String, Manifest>> for ManifestMap {
 
 impl ManifestMap {
   /// Finds the capability with the given identifier.
-  pub fn find_capability(&self, id: &str) -> Option<(String, Capability)> {
+  pub fn find_capability(&self, id: &str) -> Vec<(String, Capability)> {
+    let mut capabilities = Vec::new();
+
     for (plugin, manifest) in &self.0 {
       if id == format!("{DEFAULT_CAPABILITY_ID}-{plugin}") {
-        return Some((
+        capabilities.push((
           plugin.clone(),
           manifest.default_capability.clone().unwrap_or_default(),
         ));
       }
       for capability in &manifest.capabilities {
         if capability.id == id {
-          return Some((plugin.clone(), capability.clone()));
+          capabilities.push((plugin.clone(), capability.clone()));
         }
       }
     }
 
-    None
+    capabilities
   }
 }