|
@@ -24,6 +24,7 @@ use tauri_utils::{
|
|
|
};
|
|
|
|
|
|
use std::{
|
|
|
+ collections::HashMap,
|
|
|
env::var_os,
|
|
|
fs::copy,
|
|
|
path::{Path, PathBuf},
|
|
@@ -331,6 +332,41 @@ impl WindowsAttributes {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+/// Definition of a plugin that is part of the Tauri application instead of having its own crate.
|
|
|
+///
|
|
|
+/// By default it generates a plugin manifest that parses permissions from the `permissions/$plugin-name` directory.
|
|
|
+/// To change the glob pattern that is used to find permissions, use [`Self::permissions_path_pattern`].
|
|
|
+///
|
|
|
+/// To autogenerate permissions for each of the plugin commands, see [`Self::commands`].
|
|
|
+#[derive(Debug, Default)]
|
|
|
+pub struct InlinedPlugin {
|
|
|
+ commands: &'static [&'static str],
|
|
|
+ permissions_path_pattern: Option<&'static str>,
|
|
|
+}
|
|
|
+
|
|
|
+impl InlinedPlugin {
|
|
|
+ pub fn new() -> Self {
|
|
|
+ Self::default()
|
|
|
+ }
|
|
|
+
|
|
|
+ /// Define a list of commands that gets permissions autogenerated in the format of `allow-$command` and `deny-$command`
|
|
|
+ /// where $command is the command in kebab-case.
|
|
|
+ pub fn commands(mut self, commands: &'static [&'static str]) -> Self {
|
|
|
+ self.commands = commands;
|
|
|
+ self
|
|
|
+ }
|
|
|
+
|
|
|
+ /// Sets a glob pattern that is used to find the permissions of this inlined plugin.
|
|
|
+ ///
|
|
|
+ /// **Note:** You must emit [rerun-if-changed] instructions for the plugin permissions directory.
|
|
|
+ ///
|
|
|
+ /// By default it is `./permissions/$plugin-name/**/*`
|
|
|
+ pub fn permissions_path_pattern(mut self, pattern: &'static str) -> Self {
|
|
|
+ self.permissions_path_pattern.replace(pattern);
|
|
|
+ self
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
/// The attributes used on the build.
|
|
|
#[derive(Debug, Default)]
|
|
|
pub struct Attributes {
|
|
@@ -339,6 +375,7 @@ pub struct Attributes {
|
|
|
capabilities_path_pattern: Option<&'static str>,
|
|
|
#[cfg(feature = "codegen")]
|
|
|
codegen: Option<codegen::context::CodegenContext>,
|
|
|
+ inlined_plugins: HashMap<&'static str, InlinedPlugin>,
|
|
|
}
|
|
|
|
|
|
impl Attributes {
|
|
@@ -365,6 +402,14 @@ impl Attributes {
|
|
|
self
|
|
|
}
|
|
|
|
|
|
+ /// Adds the given plugin to the list of inlined plugins (a plugin that is part of your application).
|
|
|
+ ///
|
|
|
+ /// See [`InlinedPlugin`] for more information.
|
|
|
+ pub fn plugin(mut self, name: &'static str, plugin: InlinedPlugin) -> Self {
|
|
|
+ self.inlined_plugins.insert(name, plugin);
|
|
|
+ self
|
|
|
+ }
|
|
|
+
|
|
|
#[cfg(feature = "codegen")]
|
|
|
#[cfg_attr(docsrs, doc(cfg(feature = "codegen")))]
|
|
|
#[must_use]
|
|
@@ -473,7 +518,51 @@ pub fn try_build(attributes: Attributes) -> Result<()> {
|
|
|
let out_dir = PathBuf::from(std::env::var("OUT_DIR").unwrap());
|
|
|
|
|
|
manifest::check(&config, &mut manifest)?;
|
|
|
- let plugin_manifests = acl::get_plugin_manifests()?;
|
|
|
+ let mut plugin_manifests = acl::get_plugin_manifests()?;
|
|
|
+ for (name, plugin) in attributes.inlined_plugins {
|
|
|
+ let plugin_out_dir = out_dir.join("plugins").join(name);
|
|
|
+
|
|
|
+ let mut permission_files = if plugin.commands.is_empty() {
|
|
|
+ Vec::new()
|
|
|
+ } else {
|
|
|
+ tauri_utils::acl::build::autogenerate_command_permissions(
|
|
|
+ &plugin_out_dir,
|
|
|
+ plugin.commands,
|
|
|
+ "",
|
|
|
+ );
|
|
|
+ tauri_utils::acl::build::define_permissions(
|
|
|
+ &plugin_out_dir.join("*").to_string_lossy(),
|
|
|
+ name,
|
|
|
+ &plugin_out_dir,
|
|
|
+ )?
|
|
|
+ };
|
|
|
+
|
|
|
+ if let Some(pattern) = plugin.permissions_path_pattern {
|
|
|
+ permission_files.extend(tauri_utils::acl::build::define_permissions(
|
|
|
+ pattern,
|
|
|
+ name,
|
|
|
+ &plugin_out_dir,
|
|
|
+ )?);
|
|
|
+ } else {
|
|
|
+ let default_permissions_path = Path::new("permissions").join(name);
|
|
|
+ println!(
|
|
|
+ "cargo:rerun-if-changed={}",
|
|
|
+ default_permissions_path.display()
|
|
|
+ );
|
|
|
+ permission_files.extend(tauri_utils::acl::build::define_permissions(
|
|
|
+ &default_permissions_path
|
|
|
+ .join("**")
|
|
|
+ .join("*")
|
|
|
+ .to_string_lossy(),
|
|
|
+ name,
|
|
|
+ &plugin_out_dir,
|
|
|
+ )?);
|
|
|
+ }
|
|
|
+
|
|
|
+ let manifest = tauri_utils::acl::plugin::Manifest::new(permission_files, None);
|
|
|
+ plugin_manifests.insert(name.into(), manifest);
|
|
|
+ }
|
|
|
+
|
|
|
std::fs::write(
|
|
|
out_dir.join(PLUGIN_MANIFESTS_FILE_NAME),
|
|
|
serde_json::to_string(&plugin_manifests)?,
|