-
-
Notifications
You must be signed in to change notification settings - Fork 339
/
walk.rs
167 lines (143 loc) · 5.19 KB
/
walk.rs
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
use std::{
path::{Path, PathBuf},
sync::mpsc,
};
use ignore::{overrides::OverrideBuilder, DirEntry};
use oxc_span::VALID_EXTENSIONS;
use crate::IgnoreOptions;
#[derive(Clone)]
pub struct Extensions(pub Vec<&'static str>);
impl Default for Extensions {
fn default() -> Self {
Self(VALID_EXTENSIONS.to_vec())
}
}
pub struct Walk {
inner: ignore::WalkParallel,
/// The file extensions to include during the traversal.
extensions: Extensions,
}
struct WalkBuilder {
sender: mpsc::Sender<Vec<Box<Path>>>,
extensions: Extensions,
}
impl<'s> ignore::ParallelVisitorBuilder<'s> for WalkBuilder {
fn build(&mut self) -> Box<dyn ignore::ParallelVisitor + 's> {
Box::new(WalkCollector {
paths: vec![],
sender: self.sender.clone(),
extensions: self.extensions.clone(),
})
}
}
struct WalkCollector {
paths: Vec<Box<Path>>,
sender: mpsc::Sender<Vec<Box<Path>>>,
extensions: Extensions,
}
impl Drop for WalkCollector {
fn drop(&mut self) {
let paths = std::mem::take(&mut self.paths);
self.sender.send(paths).unwrap();
}
}
impl ignore::ParallelVisitor for WalkCollector {
fn visit(&mut self, entry: Result<ignore::DirEntry, ignore::Error>) -> ignore::WalkState {
match entry {
Ok(entry) => {
if entry.file_type().is_some_and(|ft| !ft.is_dir())
&& Walk::is_wanted_entry(&entry, &self.extensions)
{
self.paths.push(entry.path().to_path_buf().into_boxed_path());
}
ignore::WalkState::Continue
}
Err(_err) => ignore::WalkState::Skip,
}
}
}
impl Walk {
/// Will not canonicalize paths.
/// # Panics
pub fn new(paths: &[PathBuf], options: &IgnoreOptions) -> Self {
assert!(!paths.is_empty(), "At least one path must be provided to Walk::new");
let mut inner = ignore::WalkBuilder::new(
paths
.iter()
.next()
.expect("Expected paths parameter to Walk::new() to contain at least one path."),
);
if let Some(paths) = paths.get(1..) {
for path in paths {
inner.add(path);
}
}
if !options.no_ignore {
inner.add_custom_ignore_filename(&options.ignore_path);
if !options.ignore_pattern.is_empty() {
let mut override_builder = OverrideBuilder::new(Path::new("/"));
for pattern in &options.ignore_pattern {
// Meaning of ignore pattern is reversed
// <https://docs.rs/ignore/latest/ignore/overrides/struct.OverrideBuilder.html#method.add>
let pattern = format!("!{pattern}");
override_builder.add(&pattern).unwrap();
}
let overrides = override_builder.build().unwrap();
inner.overrides(overrides);
}
}
// Turning off `follow_links` because:
// * following symlinks is a really slow syscall
// * it is super rare to have symlinked source code
let inner = inner.ignore(false).git_global(false).follow_links(false).build_parallel();
Self { inner, extensions: Extensions::default() }
}
pub fn paths(self) -> Vec<Box<Path>> {
let (sender, receiver) = mpsc::channel::<Vec<Box<Path>>>();
let mut builder = WalkBuilder { sender, extensions: self.extensions };
self.inner.visit(&mut builder);
drop(builder);
receiver.into_iter().flatten().collect()
}
pub fn with_extensions(mut self, extensions: Extensions) -> Self {
self.extensions = extensions;
self
}
fn is_wanted_entry(dir_entry: &DirEntry, extensions: &Extensions) -> bool {
let Some(file_type) = dir_entry.file_type() else { return false };
if file_type.is_dir() {
return false;
}
let Some(file_name) = dir_entry.path().file_name() else { return false };
if [".min.", "-min.", "_min."].iter().any(|e| file_name.to_string_lossy().contains(e)) {
return false;
}
let Some(extension) = dir_entry.path().extension() else { return false };
let extension = extension.to_string_lossy();
extensions.0.contains(&extension.as_ref())
}
}
#[cfg(test)]
mod test {
use std::{env, ffi::OsString};
use crate::IgnoreOptions;
use super::{Extensions, Walk};
#[test]
fn test_walk_with_extensions() {
let fixture = env::current_dir().unwrap().join("fixtures/walk_dir");
let fixtures = vec![fixture.clone()];
let ignore_options = IgnoreOptions {
no_ignore: false,
ignore_path: OsString::from(".gitignore"),
ignore_pattern: vec![],
};
let mut paths = Walk::new(&fixtures, &ignore_options)
.with_extensions(Extensions(["js", "vue"].to_vec()))
.paths()
.into_iter()
.map(|path| path.strip_prefix(&fixture).unwrap().to_string_lossy().to_string())
.collect::<Vec<_>>();
paths.sort();
assert_eq!(paths, vec!["bar.vue", "foo.js"]);
}
}