feat(tauri-plugin): generate permissions reference markdown file for plugin (#8729)

* feat(acl): generate reference markdown file

* lint

* generate plugin docs instead
This commit is contained in:
Lucas Fernandes Nogueira 2024-02-01 17:14:23 -03:00 committed by GitHub
parent 7315189e76
commit 3e5c28ff4f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 81 additions and 1 deletions

View File

@ -52,9 +52,11 @@ impl<'a> Builder<'a> {
// requirement: links MUST be set and MUST match the name
let _links = build_var("CARGO_MANIFEST_LINKS")?;
let autogenerated = Path::new("permissions/autogenerated/");
let autogenerated = Path::new("permissions/autogenerated");
let commands_dir = &autogenerated.join("commands");
std::fs::create_dir_all(&autogenerated).expect("unable to create permissions dir");
if !self.commands.is_empty() {
acl::build::autogenerate_command_permissions(commands_dir, self.commands, "");
}
@ -62,6 +64,7 @@ impl<'a> Builder<'a> {
let permissions = acl::build::define_permissions("./permissions/**/*.*", &name, &out_dir)?;
acl::build::generate_schema(&permissions, "./permissions")?;
generate_docs(&permissions, &autogenerated)?;
if let Some(global_scope_schema) = self.global_scope_schema {
acl::build::define_global_scope_schema(global_scope_schema, &name, &out_dir)?;
@ -74,6 +77,42 @@ impl<'a> Builder<'a> {
}
}
fn generate_docs(permissions: &[acl::plugin::PermissionFile], out_dir: &Path) -> Result<(), Error> {
let mut docs = format!("# Permissions\n\n");
fn docs_from(id: &str, description: Option<&str>) -> String {
let mut docs = format!("## {id}");
if let Some(d) = description {
docs.push_str(&format!("\n\n{d}"));
}
docs
}
for permission in permissions {
for set in &permission.set {
docs.push_str(&docs_from(&set.identifier, Some(&set.description)));
docs.push_str("\n\n");
}
if let Some(default) = &permission.default {
docs.push_str(&docs_from("default", default.description.as_deref()));
docs.push_str("\n\n");
}
for permission in &permission.permission {
docs.push_str(&docs_from(
&permission.identifier,
permission.description.as_deref(),
));
docs.push_str("\n\n");
}
}
std::fs::write(out_dir.join("reference.md"), docs).map_err(Error::WriteFile)?;
Ok(())
}
/// Grab an env var that is expected to be set inside of build scripts.
fn build_var(key: &'static str) -> Result<String, Error> {
std::env::var(key).map_err(|_| Error::BuildVar(key))

View File

@ -0,0 +1,18 @@
# Permissions
## allow-ping
Enables the ping command without any pre-configured scope.
## deny-ping
Denies the ping command without any pre-configured scope.
## global-scope
Sets a global scope.
## allow-ping-scoped
Enables the ping command with a test scope.

View File

@ -2120,6 +2120,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "45a28f4c49489add4ce10783f7911893516f15afe45d015608d41faca6bc4d29"
dependencies = [
"dyn-clone",
"indexmap 1.9.3",
"schemars_derive",
"serde",
"serde_json",

View File

@ -0,0 +1,22 @@
# Permissions
## deny-home-dir-config
Denies read access to the complete $HOME folder.
## allow-home-dir
Allows read access to the complete $HOME folder.
## allow-full-homefolder-access
Allows read and write access to the complete $HOME folder.
## deny-homefolder-config-access
Denies access to the $HOME/.config folder.
## default
Default permissions granted