Browse Source

feat(build): save ACL metadata (#8678)

* feat(build): save ACL metadata

metadata will be read by the CLI later to show the user what has been enabled and what can be done

* lint

* fix cli
Lucas Fernandes Nogueira 1 year ago
parent
commit
d70470f868

+ 2 - 0
.github/workflows/test-cli-rs.yml

@@ -12,6 +12,8 @@ on:
   pull_request:
     paths:
       - '.github/workflows/test-cli-rs.yml'
+      - 'core/tauri-utils/**'
+      - 'tooling/bundler/**'
       - 'tooling/cli/**'
 
 env:

+ 26 - 3
core/tauri-build/src/acl.rs

@@ -4,7 +4,7 @@
 
 use std::{
   collections::BTreeMap,
-  fs::{copy, create_dir_all, File},
+  fs::{copy, create_dir_all, read_to_string, File},
   io::{BufWriter, Write},
   path::PathBuf,
 };
@@ -20,7 +20,10 @@ use tauri_utils::{
 };
 
 const CAPABILITIES_SCHEMA_FILE_NAME: &str = "schema.json";
-const CAPABILITIES_SCHEMA_FOLDER_NAME: &str = "schemas";
+/// Path of the folder where schemas are saved.
+const CAPABILITIES_SCHEMA_FOLDER_PATH: &str = "capabilities/schemas";
+const CAPABILITIES_FILE_NAME: &str = "capabilities.json";
+const PLUGIN_MANIFESTS_FILE_NAME: &str = "plugin-manifests.json";
 
 fn capabilities_schema(plugin_manifests: &BTreeMap<String, Manifest>) -> RootSchema {
   let mut schema = schema_for!(CapabilityFile);
@@ -87,7 +90,7 @@ pub fn generate_schema(
 ) -> Result<()> {
   let schema = capabilities_schema(plugin_manifests);
   let schema_str = serde_json::to_string_pretty(&schema).unwrap();
-  let out_dir = PathBuf::from("capabilities").join(CAPABILITIES_SCHEMA_FOLDER_NAME);
+  let out_dir = PathBuf::from(CAPABILITIES_SCHEMA_FOLDER_PATH);
   create_dir_all(&out_dir).context("unable to create schema output directory")?;
 
   let schema_path = out_dir.join(format!("{target}-{CAPABILITIES_SCHEMA_FILE_NAME}"));
@@ -109,6 +112,26 @@ pub fn generate_schema(
   Ok(())
 }
 
+pub fn save_capabilities(capabilities: &BTreeMap<String, Capability>) -> Result<PathBuf> {
+  let capabilities_path =
+    PathBuf::from(CAPABILITIES_SCHEMA_FOLDER_PATH).join(CAPABILITIES_FILE_NAME);
+  let capabilities_json = serde_json::to_string(&capabilities)?;
+  if capabilities_json != read_to_string(&capabilities_path).unwrap_or_default() {
+    std::fs::write(&capabilities_path, capabilities_json)?;
+  }
+  Ok(capabilities_path)
+}
+
+pub fn save_plugin_manifests(plugin_manifests: &BTreeMap<String, Manifest>) -> Result<PathBuf> {
+  let plugin_manifests_path =
+    PathBuf::from(CAPABILITIES_SCHEMA_FOLDER_PATH).join(PLUGIN_MANIFESTS_FILE_NAME);
+  let plugin_manifests_json = serde_json::to_string(&plugin_manifests)?;
+  if plugin_manifests_json != read_to_string(&plugin_manifests_path).unwrap_or_default() {
+    std::fs::write(&plugin_manifests_path, plugin_manifests_json)?;
+  }
+  Ok(plugin_manifests_path)
+}
+
 pub fn get_plugin_manifests() -> Result<BTreeMap<String, Manifest>> {
   let permission_map =
     tauri_utils::acl::build::read_permissions().context("failed to read plugin permissions")?;

+ 5 - 7
core/tauri-build/src/lib.rs

@@ -25,7 +25,7 @@ use tauri_utils::{
 
 use std::{
   env::var_os,
-  fs::read_to_string,
+  fs::copy,
   path::{Path, PathBuf},
 };
 
@@ -482,14 +482,12 @@ pub fn try_build(attributes: Attributes) -> Result<()> {
     parse_capabilities("./capabilities/**/*")?
   };
   acl::generate_schema(&plugin_manifests, target)?;
-
   acl::validate_capabilities(&plugin_manifests, &capabilities)?;
 
-  let capabilities_path = out_dir.join(CAPABILITIES_FILE_NAME);
-  let capabilities_json = serde_json::to_string(&capabilities)?;
-  if capabilities_json != read_to_string(&capabilities_path).unwrap_or_default() {
-    std::fs::write(capabilities_path, capabilities_json)?;
-  }
+  let capabilities_path = acl::save_capabilities(&capabilities)?;
+  copy(capabilities_path, out_dir.join(CAPABILITIES_FILE_NAME))?;
+
+  acl::save_plugin_manifests(&plugin_manifests)?;
 
   println!("cargo:rustc-env=TAURI_ENV_TARGET_TRIPLE={target_triple}");
 

+ 2 - 2
tooling/cli/src/migrate/config.rs

@@ -6,7 +6,7 @@ use crate::Result;
 
 use serde_json::{Map, Value};
 use tauri_utils::{
-  acl::capability::{Capability, CapabilityContext},
+  acl::capability::{Capability, CapabilityContext, PermissionEntry},
   platform::Target,
 };
 
@@ -57,7 +57,7 @@ pub fn migrate(tauri_dir: &Path) -> Result<()> {
         windows: vec!["main".into()],
         permissions: permissions
           .into_iter()
-          .map(|p| p.to_string().try_into().unwrap())
+          .map(|p| PermissionEntry::PermissionRef(p.to_string().try_into().unwrap()))
           .collect(),
         platforms: vec![
           Target::Linux,