-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
64 additions
and
5 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,82 @@ | ||
use std::time::SystemTime; | ||
|
||
use static_files::resource_dir; | ||
|
||
const JS_DIST: &str = "../browser/data-browser/dist"; | ||
const SRC_BROWSER: &str = "../browser/data-browser/src"; | ||
|
||
macro_rules! p { | ||
($($tokens: tt)*) => { | ||
println!("cargo:warning={}", format!($($tokens)*)) | ||
} | ||
} | ||
|
||
/// Check if any JS files were modified since the last build | ||
fn should_build() -> bool { | ||
if let Ok(dist) = std::fs::metadata(format!("{}/index.html", JS_DIST)) { | ||
let dist_time = dist | ||
.modified() | ||
.unwrap() | ||
.duration_since(SystemTime::UNIX_EPOCH) | ||
.unwrap(); | ||
for entry in walkdir::WalkDir::new(SRC_BROWSER) | ||
.into_iter() | ||
.filter_map(|e| { | ||
// ignore ds store | ||
if let Ok(e) = e { | ||
if e.path().to_str().unwrap().contains(".DS_Store") { | ||
return None; | ||
} | ||
Some(e) | ||
} else { | ||
None | ||
} | ||
}) | ||
{ | ||
if entry.path().is_file() { | ||
let src_time = entry | ||
.metadata() | ||
.unwrap() | ||
.modified() | ||
.unwrap() | ||
.duration_since(SystemTime::UNIX_EPOCH) | ||
.unwrap(); | ||
if src_time >= dist_time { | ||
p!("Source file modified: {:?}, rebuilding...", entry.path()); | ||
return true; | ||
} | ||
} | ||
} | ||
|
||
p!("No changes in JS, skipping build."); | ||
false | ||
} else { | ||
p!("No dist folder found, building..."); | ||
true | ||
} | ||
} | ||
|
||
fn main() -> std::io::Result<()> { | ||
let js_build_path = "../browser/data-browser/dist"; | ||
println!("cargo:rerun-if-changed=../browser"); | ||
|
||
if std::fs::read_dir(js_build_path).is_err() { | ||
println!("Running `pnpm run build` in the data-browser folder..."); | ||
if should_build() { | ||
p!("pnpm install..."); | ||
std::process::Command::new("pnpm") | ||
.current_dir("../browser") | ||
.args(["install"]) | ||
.output() | ||
.expect("failed to install deps"); | ||
p!("pnpm build..."); | ||
std::process::Command::new("pnpm") | ||
.current_dir("../browser") | ||
.args(["run", "build"]) | ||
.output() | ||
.expect("failed to build js bundle"); | ||
} | ||
|
||
resource_dir(js_build_path) | ||
resource_dir(JS_DIST) | ||
.build() | ||
.unwrap_or_else(|_| panic!("failed to open data browser assets from {}", js_build_path)); | ||
.unwrap_or_else(|_| panic!("failed to open data browser assets from {}", JS_DIST)); | ||
|
||
Ok(()) | ||
} |