Skip to content

Commit 4e101f8

Browse files
authored
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
1 parent 0f2789c commit 4e101f8

File tree

15 files changed

+47
-24
lines changed

15 files changed

+47
-24
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"tauri-build": patch:bug
3+
---
4+
5+
Do not trigger build script to rerun if the frontendDist directory does not exist.
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
---
2+
"@tauri-apps/cli": patch:enhance
3+
"tauri-cli": patch:enhance
4+
"tauri-build": patch:enhance
5+
"tauri-utils": patch:enhance
6+
---
7+
8+
Moved the capability JSON schema to the `src-tauri/gen` folder so it's easier to track changes on the `capabilities` folder.
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"tauri-plugin": patch:bug
3+
"tauri-utils": patch:bug
4+
---
5+
6+
Rerun build script when a new permission is added.

.changes/update-plugin-template.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"@tauri-apps/cli": patch:enhance
3+
"tauri-cli": patch:enhance
4+
---
5+
6+
Update app and plugin templates following generated files change from tauri-build and tauri-plugin.

core/tauri-build/src/acl.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ use tauri_utils::{
2323

2424
const CAPABILITIES_SCHEMA_FILE_NAME: &str = "schema.json";
2525
/// Path of the folder where schemas are saved.
26-
const CAPABILITIES_SCHEMA_FOLDER_PATH: &str = "capabilities/schemas";
26+
const CAPABILITIES_SCHEMA_FOLDER_PATH: &str = "gen/schemas";
2727
const CAPABILITIES_FILE_NAME: &str = "capabilities.json";
2828
const PLUGIN_MANIFESTS_FILE_NAME: &str = "plugin-manifests.json";
2929

core/tauri-build/src/codegen/context.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,10 @@ impl CodegenContext {
8484
// rerun if changed
8585
match &config.build.frontend_dist {
8686
Some(FrontendDist::Directory(p)) => {
87-
println!("cargo:rerun-if-changed={}", config_parent.join(p).display());
87+
let dist_path = config_parent.join(p);
88+
if dist_path.exists() {
89+
println!("cargo:rerun-if-changed={}", dist_path.display());
90+
}
8891
}
8992
Some(FrontendDist::Files(files)) => {
9093
for path in files {

core/tauri-build/src/lib.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -355,6 +355,10 @@ impl Attributes {
355355
}
356356

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

core/tauri-plugin/src/build/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ impl<'a> Builder<'a> {
9898
acl::build::autogenerate_command_permissions(&commands_dir, self.commands, "");
9999
}
100100

101+
println!("cargo:rerun-if-changed=permissions");
101102
let permissions = acl::build::define_permissions("./permissions/**/*.*", &name, &out_dir)?;
102103

103104
acl::build::generate_schema(&permissions, "./permissions")?;

core/tauri-utils/src/acl/build.rs

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@
77
use std::{
88
collections::{BTreeMap, HashMap},
99
env::{current_dir, vars_os},
10-
fs::{create_dir_all, read_to_string, write, File},
11-
io::{BufWriter, Write},
10+
fs::{create_dir_all, read_to_string, write},
1211
path::{Path, PathBuf},
1312
};
1413

@@ -80,15 +79,6 @@ pub fn define_permissions(
8079
.filter(|p| p.parent().unwrap().file_name().unwrap() != PERMISSION_SCHEMAS_FOLDER_NAME)
8180
.collect::<Vec<PathBuf>>();
8281

83-
for path in &permission_files {
84-
if !path
85-
.components()
86-
.any(|c| c.as_os_str() == AUTOGENERATED_FOLDER_NAME)
87-
{
88-
println!("cargo:rerun-if-changed={}", path.display());
89-
}
90-
}
91-
9282
let permission_files_path = out_dir.join(format!("{}-permission-files", pkg_name));
9383
std::fs::write(
9484
&permission_files_path,
@@ -147,10 +137,9 @@ pub fn parse_capabilities(
147137
.unwrap_or_default()
148138
})
149139
// filter schema files
140+
// TODO: remove this before stable
150141
.filter(|p| p.parent().unwrap().file_name().unwrap() != CAPABILITIES_SCHEMA_FOLDER_NAME)
151142
{
152-
println!("cargo:rerun-if-changed={}", path.display());
153-
154143
let capability_file = std::fs::read_to_string(&path).map_err(Error::ReadFile)?;
155144
let ext = path.extension().unwrap().to_string_lossy().to_string();
156145
let capability: CapabilityFile = match ext.as_str() {
@@ -252,10 +241,11 @@ pub fn generate_schema<P: AsRef<Path>>(
252241
let out_dir = out_dir.as_ref().join(PERMISSION_SCHEMAS_FOLDER_NAME);
253242
create_dir_all(&out_dir).expect("unable to create schema output directory");
254243

255-
let mut schema_file = BufWriter::new(
256-
File::create(out_dir.join(PERMISSION_SCHEMA_FILE_NAME)).map_err(Error::CreateFile)?,
257-
);
258-
write!(schema_file, "{schema_str}").map_err(Error::WriteFile)?;
244+
let schema_path = out_dir.join(PERMISSION_SCHEMA_FILE_NAME);
245+
if schema_str != read_to_string(&schema_path).unwrap_or_default() {
246+
write(schema_path, schema_str).map_err(Error::WriteFile)?;
247+
}
248+
259249
Ok(())
260250
}
261251

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"$schema": "./schemas/desktop-schema.json",
2+
"$schema": "../gen/schemas/desktop-schema.json",
33
"identifier": "run-app",
44
"description": "permissions to run the app",
55
"windows": ["main", "main-*"],

0 commit comments

Comments
 (0)