yarte_helpers/
recompile.rs

1use std::fs;
2
3use crate::config::{config_file_path, read_config_file, Config};
4
5/// Recompile when changed. Put me on your `build.rs`
6pub fn when_changed() {
7    // rerun when config file change
8    println!(
9        "cargo:rerun-if-changed={}",
10        config_file_path().to_str().unwrap()
11    );
12
13    let file = read_config_file();
14    let config = Config::new(&file);
15
16    let mut stack = vec![config.get_dir().clone()];
17    while let Some(dir) = stack.pop() {
18        // rerun when dir change
19        println!("cargo:rerun-if-changed={}", dir.to_str().unwrap());
20        for entry in fs::read_dir(dir).expect("valid directory") {
21            let path = entry.expect("valid directory entry").path();
22            if path.is_dir() {
23                stack.push(path);
24            } else {
25                // rerun when file change
26                println!("cargo:rerun-if-changed={}", path.to_str().unwrap());
27            }
28        }
29    }
30}