Skip to content

Commit

Permalink
refactor(core): enhance app rerun-if-changed for capabilities and fro…
Browse files Browse the repository at this point in the history
…ntend 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
  • Loading branch information
lucasfernog committed Feb 4, 2024
1 parent 0f2789c commit 4e101f8
Show file tree
Hide file tree
Showing 15 changed files with 47 additions and 24 deletions.
5 changes: 5 additions & 0 deletions .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 changes: 8 additions & 0 deletions .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 changes: 6 additions & 0 deletions .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 changes: 6 additions & 0 deletions .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.
2 changes: 1 addition & 1 deletion core/tauri-build/src/acl.rs
Expand Up @@ -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";

Expand Down
5 changes: 4 additions & 1 deletion core/tauri-build/src/codegen/context.rs
Expand Up @@ -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 {
Expand Down
5 changes: 5 additions & 0 deletions core/tauri-build/src/lib.rs
Expand Up @@ -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);
Expand Down Expand Up @@ -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)?;
Expand Down
1 change: 1 addition & 0 deletions core/tauri-plugin/src/build/mod.rs
Expand Up @@ -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")?;
Expand Down
24 changes: 7 additions & 17 deletions core/tauri-utils/src/acl/build.rs
Expand Up @@ -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},
};

Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -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(())
}

Expand Down
2 changes: 1 addition & 1 deletion 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-*"],
Expand Down
2 changes: 1 addition & 1 deletion 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"]
Expand Down
1 change: 1 addition & 0 deletions tooling/cli/templates/app/src-tauri/.gitignore
@@ -1,3 +1,4 @@
# Generated by Cargo
# will have compiled files and executables
/target/
/gen/schemas

This file was deleted.

@@ -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"],
Expand Down
1 change: 0 additions & 1 deletion tooling/cli/templates/plugin/permissions/.gitignore

This file was deleted.

0 comments on commit 4e101f8

Please sign in to comment.