build.rs: re-run after any file inside directories have changed
this is needed to regenerate the build timestamp properly
This commit is contained in:
parent
e4421fc70e
commit
f7d93bf799
1 changed files with 22 additions and 7 deletions
29
build.rs
29
build.rs
|
@ -1,28 +1,43 @@
|
|||
extern crate serde_json;
|
||||
|
||||
use std::fs;
|
||||
use std::io::prelude::*;
|
||||
use std::time::*;
|
||||
|
||||
fn main() {
|
||||
println!("cargo:rerun-if-changed=config.json");
|
||||
println!("cargo:rerun-if-changed=src");
|
||||
println!("cargo:rerun-if-changed=theme");
|
||||
println!("cargo:rerun-if-changed=Cargo.toml");
|
||||
rerun_if_dir_changed("src");
|
||||
rerun_if_dir_changed("theme");
|
||||
// Load theme name from config.json and output code to load the theme via include_dir!
|
||||
let config: serde_json::Value =
|
||||
serde_json::from_str(&std::fs::read_to_string("./config.json").unwrap()).unwrap();
|
||||
serde_json::from_str(&fs::read_to_string("./config.json").unwrap()).unwrap();
|
||||
|
||||
generate_build_timestamp();
|
||||
generate_theme_loader(&config);
|
||||
generate_hljs_loader(&config);
|
||||
}
|
||||
|
||||
fn rerun_if_dir_changed(dir: &str) {
|
||||
for f in fs::read_dir(dir).unwrap() {
|
||||
let f = f.unwrap();
|
||||
let t = f.file_type().unwrap();
|
||||
let path = f.path();
|
||||
let path = path.to_str().unwrap();
|
||||
println!("cargo:rerun-if-changed={}", path);
|
||||
|
||||
if t.is_dir() {
|
||||
rerun_if_dir_changed(path);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn generate_build_timestamp() {
|
||||
let build_time = format!(
|
||||
"pub const BUILD_TIMESTAMP: u64 = {};",
|
||||
SystemTime::now().duration_since(SystemTime::UNIX_EPOCH).unwrap().as_secs());
|
||||
let out_path = std::path::PathBuf::from(std::env::var("OUT_DIR").unwrap());
|
||||
let mut out_file = std::fs::File::create(out_path.join("build_timestamp.rs")).unwrap();
|
||||
let mut out_file = fs::File::create(out_path.join("build_timestamp.rs")).unwrap();
|
||||
out_file.write(build_time.as_bytes()).unwrap();
|
||||
out_file.sync_data().unwrap();
|
||||
}
|
||||
|
@ -34,7 +49,7 @@ fn generate_theme_loader(config: &serde_json::Value) {
|
|||
};
|
||||
let theme_load_code = format!("const THEME_DIR: Dir = include_dir!(\"theme/{}\");", theme_name.as_str().unwrap());
|
||||
let out_path = std::path::PathBuf::from(std::env::var("OUT_DIR").unwrap());
|
||||
let mut out_file = std::fs::File::create(out_path.join("load_theme.rs")).unwrap();
|
||||
let mut out_file = fs::File::create(out_path.join("load_theme.rs")).unwrap();
|
||||
out_file.write_all(theme_load_code.as_bytes()).unwrap();
|
||||
out_file.sync_data().unwrap();
|
||||
}
|
||||
|
@ -58,10 +73,10 @@ fn generate_hljs_loader(config: &serde_json::Value) {
|
|||
let js_code = format!(
|
||||
"const hljs = require(\\\"highlight.js/lib/highlight.js\\\");\n{}module.exports = hljs;",
|
||||
highlight_lang);
|
||||
let rs_code = std::fs::read_to_string("./src/hljs_tpl.rs").unwrap();
|
||||
let rs_code = fs::read_to_string("./src/hljs_tpl.rs").unwrap();
|
||||
let rs_code = format!("#[wasm_bindgen(inline_js = \"{}\")]\n{}", js_code, rs_code);
|
||||
let out_path = std::path::PathBuf::from(std::env::var("OUT_DIR").unwrap());
|
||||
let mut out_file = std::fs::File::create(out_path.join("load_hljs.rs")).unwrap();
|
||||
let mut out_file = fs::File::create(out_path.join("load_hljs.rs")).unwrap();
|
||||
out_file.write_all(rs_code.as_bytes()).unwrap();
|
||||
out_file.sync_data().unwrap();
|
||||
}
|
Loading…
Add table
Reference in a new issue