xtask/
util.rs

1use std::path::{Path, PathBuf};
2
3use xshell::{Shell, cmd};
4
5pub(crate) fn list_rust_files(dir: &Path) -> Vec<PathBuf> {
6    let mut res = list_files(dir);
7    res.retain(|it| {
8        it.file_name().unwrap_or_default().to_str().unwrap_or_default().ends_with(".rs")
9    });
10    res
11}
12
13pub(crate) fn list_files(dir: &Path) -> Vec<PathBuf> {
14    let mut res = Vec::new();
15    let mut work = vec![dir.to_path_buf()];
16    while let Some(dir) = work.pop() {
17        for entry in dir.read_dir().unwrap() {
18            let entry = entry.unwrap();
19            let file_type = entry.file_type().unwrap();
20            let path = entry.path();
21            let is_hidden =
22                path.file_name().unwrap_or_default().to_str().unwrap_or_default().starts_with('.');
23            if !is_hidden {
24                if file_type.is_dir() {
25                    work.push(path);
26                } else if file_type.is_file() {
27                    res.push(path);
28                }
29            }
30        }
31    }
32    res
33}
34
35pub(crate) fn detect_target(sh: &Shell) -> String {
36    match std::env::var("RA_TARGET") {
37        Ok(target) => target,
38        _ => match cmd!(sh, "rustc --print=host-tuple").read() {
39            Ok(target) => target,
40            Err(e) => panic!("Failed to detect target: {e}\nPlease set RA_TARGET explicitly"),
41        },
42    }
43}