1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
use yarte_config::{read_config_file, Config};

use std::fs;
use yarte_config::config_file_path;

pub fn when_changed() {
    println!(
        "cargo:rerun-if-changed={}",
        config_file_path().to_str().unwrap()
    );

    let file = read_config_file();
    let config = Config::new(&file);

    // rerun when dir change for add files
    println!(
        "cargo:rerun-if-changed={}",
        config.get_dir().to_str().unwrap()
    );
    let mut stack = vec![config.get_dir().clone()];
    loop {
        if let Some(dir) = stack.pop() {
            for entry in fs::read_dir(dir).expect("valid directory") {
                let path = entry.expect("valid directory entry").path();
                if path.is_dir() {
                    stack.push(path);
                } else {
                    // rerun when file change
                    println!("cargo:rerun-if-changed={}", path.to_str().unwrap());
                }
            }
        } else {
            break;
        }
    }
}