@@ -24,6 +24,7 @@ use tauri_utils::{
2424} ;
2525
2626use std:: {
27+ collections:: HashMap ,
2728 env:: var_os,
2829 fs:: copy,
2930 path:: { Path , PathBuf } ,
@@ -331,6 +332,41 @@ impl WindowsAttributes {
331332 }
332333}
333334
335+ /// Definition of a plugin that is part of the Tauri application instead of having its own crate.
336+ ///
337+ /// By default it generates a plugin manifest that parses permissions from the `permissions/$plugin-name` directory.
338+ /// To change the glob pattern that is used to find permissions, use [`Self::permissions_path_pattern`].
339+ ///
340+ /// To autogenerate permissions for each of the plugin commands, see [`Self::commands`].
341+ #[ derive( Debug , Default ) ]
342+ pub struct InlinedPlugin {
343+ commands : & ' static [ & ' static str ] ,
344+ permissions_path_pattern : Option < & ' static str > ,
345+ }
346+
347+ impl InlinedPlugin {
348+ pub fn new ( ) -> Self {
349+ Self :: default ( )
350+ }
351+
352+ /// Define a list of commands that gets permissions autogenerated in the format of `allow-$command` and `deny-$command`
353+ /// where $command is the command in kebab-case.
354+ pub fn commands ( mut self , commands : & ' static [ & ' static str ] ) -> Self {
355+ self . commands = commands;
356+ self
357+ }
358+
359+ /// Sets a glob pattern that is used to find the permissions of this inlined plugin.
360+ ///
361+ /// **Note:** You must emit [rerun-if-changed] instructions for the plugin permissions directory.
362+ ///
363+ /// By default it is `./permissions/$plugin-name/**/*`
364+ pub fn permissions_path_pattern ( mut self , pattern : & ' static str ) -> Self {
365+ self . permissions_path_pattern . replace ( pattern) ;
366+ self
367+ }
368+ }
369+
334370/// The attributes used on the build.
335371#[ derive( Debug , Default ) ]
336372pub struct Attributes {
@@ -339,6 +375,7 @@ pub struct Attributes {
339375 capabilities_path_pattern : Option < & ' static str > ,
340376 #[ cfg( feature = "codegen" ) ]
341377 codegen : Option < codegen:: context:: CodegenContext > ,
378+ inlined_plugins : HashMap < & ' static str , InlinedPlugin > ,
342379}
343380
344381impl Attributes {
@@ -365,6 +402,14 @@ impl Attributes {
365402 self
366403 }
367404
405+ /// Adds the given plugin to the list of inlined plugins (a plugin that is part of your application).
406+ ///
407+ /// See [`InlinedPlugin`] for more information.
408+ pub fn plugin ( mut self , name : & ' static str , plugin : InlinedPlugin ) -> Self {
409+ self . inlined_plugins . insert ( name, plugin) ;
410+ self
411+ }
412+
368413 #[ cfg( feature = "codegen" ) ]
369414 #[ cfg_attr( docsrs, doc( cfg( feature = "codegen" ) ) ) ]
370415 #[ must_use]
@@ -473,7 +518,51 @@ pub fn try_build(attributes: Attributes) -> Result<()> {
473518 let out_dir = PathBuf :: from ( std:: env:: var ( "OUT_DIR" ) . unwrap ( ) ) ;
474519
475520 manifest:: check ( & config, & mut manifest) ?;
476- let plugin_manifests = acl:: get_plugin_manifests ( ) ?;
521+ let mut plugin_manifests = acl:: get_plugin_manifests ( ) ?;
522+ for ( name, plugin) in attributes. inlined_plugins {
523+ let plugin_out_dir = out_dir. join ( "plugins" ) . join ( name) ;
524+
525+ let mut permission_files = if plugin. commands . is_empty ( ) {
526+ Vec :: new ( )
527+ } else {
528+ tauri_utils:: acl:: build:: autogenerate_command_permissions (
529+ & plugin_out_dir,
530+ plugin. commands ,
531+ "" ,
532+ ) ;
533+ tauri_utils:: acl:: build:: define_permissions (
534+ & plugin_out_dir. join ( "*" ) . to_string_lossy ( ) ,
535+ name,
536+ & plugin_out_dir,
537+ ) ?
538+ } ;
539+
540+ if let Some ( pattern) = plugin. permissions_path_pattern {
541+ permission_files. extend ( tauri_utils:: acl:: build:: define_permissions (
542+ pattern,
543+ name,
544+ & plugin_out_dir,
545+ ) ?) ;
546+ } else {
547+ let default_permissions_path = Path :: new ( "permissions" ) . join ( name) ;
548+ println ! (
549+ "cargo:rerun-if-changed={}" ,
550+ default_permissions_path. display( )
551+ ) ;
552+ permission_files. extend ( tauri_utils:: acl:: build:: define_permissions (
553+ & default_permissions_path
554+ . join ( "**" )
555+ . join ( "*" )
556+ . to_string_lossy ( ) ,
557+ name,
558+ & plugin_out_dir,
559+ ) ?) ;
560+ }
561+
562+ let manifest = tauri_utils:: acl:: plugin:: Manifest :: new ( permission_files, None ) ;
563+ plugin_manifests. insert ( name. into ( ) , manifest) ;
564+ }
565+
477566 std:: fs:: write (
478567 out_dir. join ( PLUGIN_MANIFESTS_FILE_NAME ) ,
479568 serde_json:: to_string ( & plugin_manifests) ?,
0 commit comments