bricks/build/
include.rs

1use std::{
2    fs,
3    path::{Component, Path, PathBuf},
4};
5
6use anyhow::Result;
7
8pub fn src_to_include_path(path: impl AsRef<Path>, name: &str) -> PathBuf {
9    let mut new_path = PathBuf::new();
10    for component in path.as_ref().components() {
11        match component {
12            Component::Normal(part) if part == "src" => {
13                new_path.push(format!("build/include/{}", name))
14            }
15            Component::Normal(part) => new_path.push(part),
16            _ => new_path.push(component),
17        }
18    }
19
20    new_path
21}
22
23pub fn copy_headers(src_path: impl AsRef<Path>, name: &str) -> Result<()> {
24    fs::create_dir_all(src_to_include_path(&src_path, name))?;
25
26    for entry in walkdir::WalkDir::new(&src_path).follow_links(true) {
27        let entry = entry?;
28
29        let path = entry.path();
30        let path_str = path.to_string_lossy();
31        if !path_str.ends_with(".h") && !path_str.ends_with(".hpp") {
32            continue;
33        }
34
35        fs::copy(path, src_to_include_path(path, name))?;
36    }
37    Ok(())
38}