Skip to content

Commit f957cbb

Browse files
authored
fix(codegen): write output file when contents change (#4889)
1 parent 5109c27 commit f957cbb

3 files changed

Lines changed: 30 additions & 36 deletions

File tree

.changes/write-only-changed.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"tauri-codegen": patch
3+
"tauri-build": patch
4+
---
5+
6+
Only rewrite temporary icon files when the content change, avoid needless rebuilds.

core/tauri-build/src/lib.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,10 @@ pub fn try_build(attributes: Attributes) -> Result<()> {
293293
)?;
294294
}
295295

296+
for icon in &config.tauri.bundle.icon {
297+
println!("cargo:rerun-if-changed={}", icon);
298+
}
299+
296300
#[allow(unused_mut, clippy::redundant_clone)]
297301
let mut resources = config.tauri.bundle.resources.clone().unwrap_or_default();
298302
#[cfg(windows)]

core/tauri-codegen/src/context.rs

Lines changed: 20 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -415,9 +415,6 @@ fn ico_icon<P: AsRef<Path>>(
415415
out_dir: &Path,
416416
path: P,
417417
) -> Result<TokenStream, EmbeddedAssetsError> {
418-
use std::fs::File;
419-
use std::io::Write;
420-
421418
let path = path.as_ref();
422419
let bytes = std::fs::read(&path)
423420
.unwrap_or_else(|e| panic!("failed to read icon {}: {}", path.display(), e))
@@ -434,18 +431,11 @@ fn ico_icon<P: AsRef<Path>>(
434431
let height = entry.height();
435432

436433
let out_path = out_dir.join(path.file_name().unwrap());
437-
let mut out_file = File::create(&out_path).map_err(|error| EmbeddedAssetsError::AssetWrite {
438-
path: out_path.clone(),
434+
write_if_changed(&out_path, &rgba).map_err(|error| EmbeddedAssetsError::AssetWrite {
435+
path: path.to_owned(),
439436
error,
440437
})?;
441438

442-
out_file
443-
.write_all(&rgba)
444-
.map_err(|error| EmbeddedAssetsError::AssetWrite {
445-
path: path.to_owned(),
446-
error,
447-
})?;
448-
449439
let out_path = out_path.display().to_string();
450440

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

455445
#[cfg(target_os = "macos")]
456446
fn raw_icon<P: AsRef<Path>>(out_dir: &Path, path: P) -> Result<TokenStream, EmbeddedAssetsError> {
457-
use std::fs::File;
458-
use std::io::Write;
459-
460447
let path = path.as_ref();
461448
let bytes = std::fs::read(&path)
462449
.unwrap_or_else(|e| panic!("failed to read icon {}: {}", path.display(), e))
463450
.to_vec();
464451

465452
let out_path = out_dir.join(path.file_name().unwrap());
466-
let mut out_file = File::create(&out_path).map_err(|error| EmbeddedAssetsError::AssetWrite {
467-
path: out_path.clone(),
453+
write_if_changed(&out_path, &bytes).map_err(|error| EmbeddedAssetsError::AssetWrite {
454+
path: path.to_owned(),
468455
error,
469456
})?;
470457

471-
out_file
472-
.write_all(&bytes)
473-
.map_err(|error| EmbeddedAssetsError::AssetWrite {
474-
path: path.to_owned(),
475-
error,
476-
})?;
477-
478458
let out_path = out_path.display().to_string();
479459

480460
let icon = quote!(Some(include_bytes!(#out_path).to_vec()));
@@ -486,9 +466,6 @@ fn png_icon<P: AsRef<Path>>(
486466
out_dir: &Path,
487467
path: P,
488468
) -> Result<TokenStream, EmbeddedAssetsError> {
489-
use std::fs::File;
490-
use std::io::Write;
491-
492469
let path = path.as_ref();
493470
let bytes = std::fs::read(&path)
494471
.unwrap_or_else(|e| panic!("failed to read icon {}: {}", path.display(), e))
@@ -505,24 +482,31 @@ fn png_icon<P: AsRef<Path>>(
505482
let height = reader.info().height;
506483

507484
let out_path = out_dir.join(path.file_name().unwrap());
508-
let mut out_file = File::create(&out_path).map_err(|error| EmbeddedAssetsError::AssetWrite {
509-
path: out_path.clone(),
485+
write_if_changed(&out_path, &buffer).map_err(|error| EmbeddedAssetsError::AssetWrite {
486+
path: path.to_owned(),
510487
error,
511488
})?;
512489

513-
out_file
514-
.write_all(&buffer)
515-
.map_err(|error| EmbeddedAssetsError::AssetWrite {
516-
path: path.to_owned(),
517-
error,
518-
})?;
519-
520490
let out_path = out_path.display().to_string();
521491

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

496+
fn write_if_changed(out_path: &Path, data: &[u8]) -> std::io::Result<()> {
497+
use std::fs::File;
498+
use std::io::Write;
499+
500+
if let Ok(curr) = std::fs::read(&out_path) {
501+
if curr == data {
502+
return Ok(());
503+
}
504+
}
505+
506+
let mut out_file = File::create(&out_path)?;
507+
out_file.write_all(data)
508+
}
509+
526510
#[cfg(any(windows, target_os = "macos", target_os = "linux"))]
527511
fn find_icon<F: Fn(&&String) -> bool>(
528512
config: &Config,

0 commit comments

Comments
 (0)