Skip to content

Commit

Permalink
fix(codegen): write output file when contents change (#4889)
Browse files Browse the repository at this point in the history
  • Loading branch information
brian14708 committed Aug 8, 2022
1 parent 5109c27 commit f957cbb
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 36 deletions.
6 changes: 6 additions & 0 deletions .changes/write-only-changed.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"tauri-codegen": patch
"tauri-build": patch
---

Only rewrite temporary icon files when the content change, avoid needless rebuilds.
4 changes: 4 additions & 0 deletions core/tauri-build/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,10 @@ pub fn try_build(attributes: Attributes) -> Result<()> {
)?;
}

for icon in &config.tauri.bundle.icon {
println!("cargo:rerun-if-changed={}", icon);
}

#[allow(unused_mut, clippy::redundant_clone)]
let mut resources = config.tauri.bundle.resources.clone().unwrap_or_default();
#[cfg(windows)]
Expand Down
56 changes: 20 additions & 36 deletions core/tauri-codegen/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -415,9 +415,6 @@ fn ico_icon<P: AsRef<Path>>(
out_dir: &Path,
path: P,
) -> Result<TokenStream, EmbeddedAssetsError> {
use std::fs::File;
use std::io::Write;

let path = path.as_ref();
let bytes = std::fs::read(&path)
.unwrap_or_else(|e| panic!("failed to read icon {}: {}", path.display(), e))
Expand All @@ -434,18 +431,11 @@ fn ico_icon<P: AsRef<Path>>(
let height = entry.height();

let out_path = out_dir.join(path.file_name().unwrap());
let mut out_file = File::create(&out_path).map_err(|error| EmbeddedAssetsError::AssetWrite {
path: out_path.clone(),
write_if_changed(&out_path, &rgba).map_err(|error| EmbeddedAssetsError::AssetWrite {
path: path.to_owned(),
error,
})?;

out_file
.write_all(&rgba)
.map_err(|error| EmbeddedAssetsError::AssetWrite {
path: path.to_owned(),
error,
})?;

let out_path = out_path.display().to_string();

let icon = quote!(Some(#root::Icon::Rgba { rgba: include_bytes!(#out_path).to_vec(), width: #width, height: #height }));
Expand All @@ -454,27 +444,17 @@ fn ico_icon<P: AsRef<Path>>(

#[cfg(target_os = "macos")]
fn raw_icon<P: AsRef<Path>>(out_dir: &Path, path: P) -> Result<TokenStream, EmbeddedAssetsError> {
use std::fs::File;
use std::io::Write;

let path = path.as_ref();
let bytes = std::fs::read(&path)
.unwrap_or_else(|e| panic!("failed to read icon {}: {}", path.display(), e))
.to_vec();

let out_path = out_dir.join(path.file_name().unwrap());
let mut out_file = File::create(&out_path).map_err(|error| EmbeddedAssetsError::AssetWrite {
path: out_path.clone(),
write_if_changed(&out_path, &bytes).map_err(|error| EmbeddedAssetsError::AssetWrite {
path: path.to_owned(),
error,
})?;

out_file
.write_all(&bytes)
.map_err(|error| EmbeddedAssetsError::AssetWrite {
path: path.to_owned(),
error,
})?;

let out_path = out_path.display().to_string();

let icon = quote!(Some(include_bytes!(#out_path).to_vec()));
Expand All @@ -486,9 +466,6 @@ fn png_icon<P: AsRef<Path>>(
out_dir: &Path,
path: P,
) -> Result<TokenStream, EmbeddedAssetsError> {
use std::fs::File;
use std::io::Write;

let path = path.as_ref();
let bytes = std::fs::read(&path)
.unwrap_or_else(|e| panic!("failed to read icon {}: {}", path.display(), e))
Expand All @@ -505,24 +482,31 @@ fn png_icon<P: AsRef<Path>>(
let height = reader.info().height;

let out_path = out_dir.join(path.file_name().unwrap());
let mut out_file = File::create(&out_path).map_err(|error| EmbeddedAssetsError::AssetWrite {
path: out_path.clone(),
write_if_changed(&out_path, &buffer).map_err(|error| EmbeddedAssetsError::AssetWrite {
path: path.to_owned(),
error,
})?;

out_file
.write_all(&buffer)
.map_err(|error| EmbeddedAssetsError::AssetWrite {
path: path.to_owned(),
error,
})?;

let out_path = out_path.display().to_string();

let icon = quote!(Some(#root::Icon::Rgba { rgba: include_bytes!(#out_path).to_vec(), width: #width, height: #height }));
Ok(icon)
}

fn write_if_changed(out_path: &Path, data: &[u8]) -> std::io::Result<()> {
use std::fs::File;
use std::io::Write;

if let Ok(curr) = std::fs::read(&out_path) {
if curr == data {
return Ok(());
}
}

let mut out_file = File::create(&out_path)?;
out_file.write_all(data)
}

#[cfg(any(windows, target_os = "macos", target_os = "linux"))]
fn find_icon<F: Fn(&&String) -> bool>(
config: &Config,
Expand Down

0 comments on commit f957cbb

Please sign in to comment.