Jelajahi Sumber

refactor(core): enhance app rerun-if-changed for capabilities and frontend dist (#8756)

* refactor(core): enhance app rerun-if-changed for capabilities and frontend dist

* always rerun-if-changed=capabilities

* fix todo

* rerun if plugin permissions change

* add change files
Lucas Fernandes Nogueira 1 tahun lalu
induk
melakukan
4e101f8016

+ 5 - 0
.changes/fix-codegen-rerun-if-changed.md

@@ -0,0 +1,5 @@
+---
+"tauri-build": patch:bug
+---
+
+Do not trigger build script to rerun if the frontendDist directory does not exist.

+ 8 - 0
.changes/refactor-capabilities-schema.md

@@ -0,0 +1,8 @@
+---
+"@tauri-apps/cli": patch:enhance
+"tauri-cli": patch:enhance
+"tauri-build": patch:enhance
+"tauri-utils": patch:enhance
+---
+
+Moved the capability JSON schema to the `src-tauri/gen` folder so it's easier to track changes on the `capabilities` folder.

+ 6 - 0
.changes/rerun-if-permission-created.md

@@ -0,0 +1,6 @@
+---
+"tauri-plugin": patch:bug
+"tauri-utils": patch:bug
+---
+
+Rerun build script when a new permission is added.

+ 6 - 0
.changes/update-plugin-template.md

@@ -0,0 +1,6 @@
+---
+"@tauri-apps/cli": patch:enhance
+"tauri-cli": patch:enhance
+---
+
+Update app and plugin templates following generated files change from tauri-build and tauri-plugin.

+ 1 - 1
core/tauri-build/src/acl.rs

@@ -23,7 +23,7 @@ use tauri_utils::{
 
 const CAPABILITIES_SCHEMA_FILE_NAME: &str = "schema.json";
 /// Path of the folder where schemas are saved.
-const CAPABILITIES_SCHEMA_FOLDER_PATH: &str = "capabilities/schemas";
+const CAPABILITIES_SCHEMA_FOLDER_PATH: &str = "gen/schemas";
 const CAPABILITIES_FILE_NAME: &str = "capabilities.json";
 const PLUGIN_MANIFESTS_FILE_NAME: &str = "plugin-manifests.json";
 

+ 4 - 1
core/tauri-build/src/codegen/context.rs

@@ -84,7 +84,10 @@ impl CodegenContext {
     // rerun if changed
     match &config.build.frontend_dist {
       Some(FrontendDist::Directory(p)) => {
-        println!("cargo:rerun-if-changed={}", config_parent.join(p).display());
+        let dist_path = config_parent.join(p);
+        if dist_path.exists() {
+          println!("cargo:rerun-if-changed={}", dist_path.display());
+        }
       }
       Some(FrontendDist::Files(files)) => {
         for path in files {

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

@@ -355,6 +355,10 @@ impl Attributes {
   }
 
   /// Set the glob pattern to be used to find the capabilities.
+  ///
+  /// **Note:** You must emit [rerun-if-changed] instructions for your capabilities directory.
+  ///
+  /// [rerun-if-changed]: https://doc.rust-lang.org/cargo/reference/build-scripts.html#rerun-if-changed
   #[must_use]
   pub fn capabilities_path_pattern(mut self, pattern: &'static str) -> Self {
     self.capabilities_path_pattern.replace(pattern);
@@ -477,6 +481,7 @@ pub fn try_build(attributes: Attributes) -> Result<()> {
   let capabilities = if let Some(pattern) = attributes.capabilities_path_pattern {
     parse_capabilities(pattern)?
   } else {
+    println!("cargo:rerun-if-changed=capabilities");
     parse_capabilities("./capabilities/**/*")?
   };
   acl::generate_schema(&plugin_manifests, target)?;

+ 1 - 0
core/tauri-plugin/src/build/mod.rs

@@ -98,6 +98,7 @@ impl<'a> Builder<'a> {
       acl::build::autogenerate_command_permissions(&commands_dir, self.commands, "");
     }
 
+    println!("cargo:rerun-if-changed=permissions");
     let permissions = acl::build::define_permissions("./permissions/**/*.*", &name, &out_dir)?;
 
     acl::build::generate_schema(&permissions, "./permissions")?;

+ 7 - 17
core/tauri-utils/src/acl/build.rs

@@ -7,8 +7,7 @@
 use std::{
   collections::{BTreeMap, HashMap},
   env::{current_dir, vars_os},
-  fs::{create_dir_all, read_to_string, write, File},
-  io::{BufWriter, Write},
+  fs::{create_dir_all, read_to_string, write},
   path::{Path, PathBuf},
 };
 
@@ -80,15 +79,6 @@ pub fn define_permissions(
     .filter(|p| p.parent().unwrap().file_name().unwrap() != PERMISSION_SCHEMAS_FOLDER_NAME)
     .collect::<Vec<PathBuf>>();
 
-  for path in &permission_files {
-    if !path
-      .components()
-      .any(|c| c.as_os_str() == AUTOGENERATED_FOLDER_NAME)
-    {
-      println!("cargo:rerun-if-changed={}", path.display());
-    }
-  }
-
   let permission_files_path = out_dir.join(format!("{}-permission-files", pkg_name));
   std::fs::write(
     &permission_files_path,
@@ -147,10 +137,9 @@ pub fn parse_capabilities(
         .unwrap_or_default()
     })
     // filter schema files
+    // TODO: remove this before stable
     .filter(|p| p.parent().unwrap().file_name().unwrap() != CAPABILITIES_SCHEMA_FOLDER_NAME)
   {
-    println!("cargo:rerun-if-changed={}", path.display());
-
     let capability_file = std::fs::read_to_string(&path).map_err(Error::ReadFile)?;
     let ext = path.extension().unwrap().to_string_lossy().to_string();
     let capability: CapabilityFile = match ext.as_str() {
@@ -252,10 +241,11 @@ pub fn generate_schema<P: AsRef<Path>>(
   let out_dir = out_dir.as_ref().join(PERMISSION_SCHEMAS_FOLDER_NAME);
   create_dir_all(&out_dir).expect("unable to create schema output directory");
 
-  let mut schema_file = BufWriter::new(
-    File::create(out_dir.join(PERMISSION_SCHEMA_FILE_NAME)).map_err(Error::CreateFile)?,
-  );
-  write!(schema_file, "{schema_str}").map_err(Error::WriteFile)?;
+  let schema_path = out_dir.join(PERMISSION_SCHEMA_FILE_NAME);
+  if schema_str != read_to_string(&schema_path).unwrap_or_default() {
+    write(schema_path, schema_str).map_err(Error::WriteFile)?;
+  }
+
   Ok(())
 }
 

+ 1 - 1
examples/api/src-tauri/capabilities/run-app.json

@@ -1,5 +1,5 @@
 {
-  "$schema": "./schemas/desktop-schema.json",
+  "$schema": "../gen/schemas/desktop-schema.json",
   "identifier": "run-app",
   "description": "permissions to run the app",
   "windows": ["main", "main-*"],

+ 1 - 1
examples/resources/src-tauri/capabilities/app.json

@@ -1,5 +1,5 @@
 {
-  "$schema": "./schemas/desktop-schema.json",
+  "$schema": "../gen/schemas/desktop-schema.json",
   "identifier": "app",
   "permissions": ["event:default", "window:default"],
   "windows": ["main"]

+ 1 - 0
tooling/cli/templates/app/src-tauri/.gitignore

@@ -1,3 +1,4 @@
 # Generated by Cargo
 # will have compiled files and executables
 /target/
+/gen/schemas

+ 0 - 1
tooling/cli/templates/app/src-tauri/capabilities/.gitignore

@@ -1 +0,0 @@
-schemas/

+ 1 - 1
tooling/cli/templates/app/src-tauri/capabilities/default.json

@@ -1,5 +1,5 @@
 {
-  "$schema": "./schemas/desktop-schema.json",
+  "$schema": "../gen/schemas/desktop-schema.json",
   "identifier": "default-plugins",
   "description": "enables the default permissions",
   "windows": ["main"],

+ 0 - 1
tooling/cli/templates/plugin/permissions/.gitignore

@@ -1 +0,0 @@
-schemas/