Skip to content

Commit b69687d

Browse files
Pollepsjoepio
authored andcommitted
Fix server rebuilding js even when ther are no changes
1 parent cb37705 commit b69687d

File tree

1 file changed

+43
-31
lines changed

1 file changed

+43
-31
lines changed

server/build.rs

Lines changed: 43 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
use std::{path::PathBuf, time::SystemTime};
1+
use std::{
2+
fs::{self, Metadata},
3+
path::PathBuf,
4+
time::SystemTime,
5+
};
26

37
macro_rules! p {
48
($($tokens: tt)*) => {
@@ -27,6 +31,7 @@ fn main() -> std::io::Result<()> {
2731

2832
if should_build(&dirs) {
2933
build_js(&dirs);
34+
let _ = fs::remove_dir_all(&dirs.js_dist_tmp);
3035
dircpy::copy_dir(&dirs.js_dist_source, &dirs.js_dist_tmp)?;
3136
} else if dirs.js_dist_tmp.exists() {
3237
p!("Found {}, skipping copy", dirs.js_dist_tmp.display());
@@ -61,38 +66,19 @@ fn should_build(dirs: &Dirs) -> bool {
6166
if let Ok(tmp_dist_index_html) =
6267
std::fs::metadata(format!("{}/index.html", dirs.js_dist_tmp.display()))
6368
{
64-
let dist_time = tmp_dist_index_html
65-
.modified()
66-
.unwrap()
67-
.duration_since(SystemTime::UNIX_EPOCH)
68-
.unwrap();
69-
for entry in walkdir::WalkDir::new(&dirs.src_browser)
69+
let has_changes = walkdir::WalkDir::new(&dirs.src_browser)
7070
.into_iter()
71-
.filter_map(|e| {
72-
// ignore ds store
73-
if let Ok(e) = e {
74-
if e.path().to_str().unwrap().contains(".DS_Store") {
75-
return None;
76-
}
77-
Some(e)
78-
} else {
79-
None
80-
}
71+
.filter_entry(|entry| {
72+
entry
73+
.file_name()
74+
.to_str()
75+
.map(|s| !s.starts_with(".DS_Store"))
76+
.unwrap_or(false)
8177
})
82-
{
83-
if entry.path().is_file() {
84-
let src_time = entry
85-
.metadata()
86-
.unwrap()
87-
.modified()
88-
.unwrap()
89-
.duration_since(SystemTime::UNIX_EPOCH)
90-
.unwrap();
91-
if src_time >= dist_time {
92-
p!("Source file modified: {:?}, rebuilding...", entry.path());
93-
return true;
94-
}
95-
}
78+
.any(|entry| is_older_than(&entry.unwrap(), &tmp_dist_index_html));
79+
80+
if has_changes {
81+
return true;
9682
}
9783

9884
p!("No changes in JS source files, skipping JS build.");
@@ -145,3 +131,29 @@ fn build_js(dirs: &Dirs) {
145131
);
146132
}
147133
}
134+
135+
fn is_older_than(dir_entry: &walkdir::DirEntry, dist_meta: &Metadata) -> bool {
136+
let dist_time = dist_meta
137+
.modified()
138+
.unwrap()
139+
.duration_since(SystemTime::UNIX_EPOCH)
140+
.unwrap();
141+
142+
if dir_entry.path().is_file() {
143+
let src_time = dir_entry
144+
.metadata()
145+
.unwrap()
146+
.modified()
147+
.unwrap()
148+
.duration_since(SystemTime::UNIX_EPOCH)
149+
.unwrap();
150+
if src_time >= dist_time {
151+
p!(
152+
"Source file modified: {:?}, rebuilding...",
153+
dir_entry.path()
154+
);
155+
return true;
156+
}
157+
}
158+
false
159+
}

0 commit comments

Comments
 (0)