run_make_support/
path_helpers.rs
1use std::path::{Path, PathBuf};
4
5use crate::env::env_var;
6use crate::rfs;
7
8#[must_use]
13pub fn cwd() -> PathBuf {
14 std::env::current_dir().unwrap()
15}
16
17pub fn path<P: AsRef<Path>>(p: P) -> PathBuf {
28 cwd().join(p.as_ref())
29}
30
31#[must_use]
33pub fn source_root() -> PathBuf {
34 env_var("SOURCE_ROOT").into()
35}
36
37#[must_use]
39pub fn build_root() -> PathBuf {
40 env_var("BUILD_ROOT").into()
41}
42
43#[track_caller]
46pub fn shallow_find_files<P: AsRef<Path>, F: Fn(&PathBuf) -> bool>(
47 path: P,
48 filter: F,
49) -> Vec<PathBuf> {
50 let mut matching_files = Vec::new();
51 for entry in rfs::read_dir(path) {
52 let entry = entry.expect("failed to read directory entry.");
53 let path = entry.path();
54
55 if path.is_file() && filter(&path) {
56 matching_files.push(path);
57 }
58 }
59 matching_files
60}
61
62#[track_caller]
65pub fn shallow_find_directories<P: AsRef<Path>, F: Fn(&PathBuf) -> bool>(
66 path: P,
67 filter: F,
68) -> Vec<PathBuf> {
69 let mut matching_files = Vec::new();
70 for entry in rfs::read_dir(path) {
71 let entry = entry.expect("failed to read directory entry.");
72 let path = entry.path();
73
74 if path.is_dir() && filter(&path) {
75 matching_files.push(path);
76 }
77 }
78 matching_files
79}
80
81pub fn not_contains<P: AsRef<Path>>(path: P, expected: &str) -> bool {
83 !path.as_ref().file_name().is_some_and(|name| name.to_str().unwrap().contains(expected))
84}
85
86pub fn filename_not_in_denylist<P: AsRef<Path>, V: AsRef<[String]>>(path: P, expected: V) -> bool {
88 let expected = expected.as_ref();
89 path.as_ref()
90 .file_name()
91 .is_some_and(|name| !expected.contains(&name.to_str().unwrap().to_owned()))
92}
93
94pub fn has_prefix<P: AsRef<Path>>(path: P, prefix: &str) -> bool {
96 path.as_ref().file_name().is_some_and(|name| name.to_str().unwrap().starts_with(prefix))
97}
98
99pub fn has_extension<P: AsRef<Path>>(path: P, extension: &str) -> bool {
101 path.as_ref().extension().is_some_and(|ext| ext == extension)
102}
103
104pub fn has_suffix<P: AsRef<Path>>(path: P, suffix: &str) -> bool {
106 path.as_ref().file_name().is_some_and(|name| name.to_str().unwrap().ends_with(suffix))
107}
108
109pub fn filename_contains<P: AsRef<Path>>(path: P, needle: &str) -> bool {
111 path.as_ref().file_name().is_some_and(|name| name.to_str().unwrap().contains(needle))
112}
113
114pub fn read_dir_entries_recursive<P: AsRef<Path>, F: FnMut(&Path)>(dir: P, mut callback: F) {
116 fn read_dir_entries_recursive_inner<P: AsRef<Path>, F: FnMut(&Path)>(dir: P, callback: &mut F) {
117 for entry in rfs::read_dir(dir) {
118 let path = entry.unwrap().path();
119 callback(&path);
120 if path.is_dir() {
121 read_dir_entries_recursive_inner(path, callback);
122 }
123 }
124 }
125
126 read_dir_entries_recursive_inner(dir, &mut callback);
127}