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
This commit is contained in:
Lucas Fernandes Nogueira 2024-02-04 11:42:13 -03:00 committed by GitHub
parent 0f2789cd67
commit 4e101f8016
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
15 changed files with 47 additions and 24 deletions

View File

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

View File

@ -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.

View File

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

View File

@ -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.

View File

@ -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";

View File

@ -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 {

View File

@ -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)?;

View File

@ -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")?;

View File

@ -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(())
}

View File

@ -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-*"],

View File

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

View File

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

View File

@ -1 +0,0 @@
schemas/

View File

@ -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"],

View File

@ -1 +0,0 @@
schemas/