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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
//! This library lets you rerun a build script if files in a directory have changed, excluding specific ones that are listed

#![warn(clippy::pedantic)]
use std::path::Path;

/// Return cargo rerun-if-changed lines to rerun build script if files in folder are changed, excluding listed
///
/// Takes the directory to check, and a slice of files under that directory to exclude. Returns a string in the format:
/// ```text
/// cargo:rerun-if-changed=path/to/folder/test.c
/// cargo:rerun-if-changed=path/to/folder/test.h
/// ```
///
/// # Errors
/// Errors if the directory cannot be read. Any invalid files will be skipped.
///
/// # Example
/// ```ignore
/// # use rerun_in_except::rerun_in_except;
/// println!("{}", rerun_in_except("./frontend", &["./frontend/node_modules", "./frontend/artifacts"]).unwrap())
/// ```
pub fn rerun_in_except(
    run_in: impl AsRef<Path>,
    except: &[impl AsRef<Path>],
) -> std::io::Result<String> {
    let mut string = String::new();
    let paths = except.iter().map(std::convert::AsRef::as_ref);
    string.extend(
        std::fs::read_dir(run_in)?
            .filter_map(Result::ok)
            .filter(|x| !paths.clone().any(|y| y == x.path()))
            .filter_map(|x| x.path().into_os_string().into_string().ok())
            .map(|x| format!("cargo:rerun-if-changed={}\n", x)),
    );
    Ok(string)
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn rerun_in_except_test() {
        assert_eq!(
            rerun_in_except(
                env!("CARGO_MANIFEST_DIR"),
                &[
                    concat!(env!("CARGO_MANIFEST_DIR"), "/Cargo.toml"),
                    concat!(env!("CARGO_MANIFEST_DIR"), "/Cargo.lock"),
                    concat!(env!("CARGO_MANIFEST_DIR"), "/.git"),
                    concat!(env!("CARGO_MANIFEST_DIR"), "/LICENSE_APACHE"),
                    concat!(env!("CARGO_MANIFEST_DIR"), "/LICENSE_MIT"),
                ],
            )
            .unwrap(),
            concat!(
                "cargo:rerun-if-changed=",
                env!("CARGO_MANIFEST_DIR"),
                "/target\n",
                "cargo:rerun-if-changed=",
                env!("CARGO_MANIFEST_DIR"),
                "/README.md\n",
                "cargo:rerun-if-changed=",
                env!("CARGO_MANIFEST_DIR"),
                "/.gitignore\n",
                "cargo:rerun-if-changed=",
                env!("CARGO_MANIFEST_DIR"),
                "/src\n",
            )
        );
    }
}