Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Let the get_files function skip excluded directories #251

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 18 additions & 5 deletions utils/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,21 @@ pub struct FileEntry {

#[cfg_attr(all(debug_assertions, not(feature = "debug-embed")), allow(unused))]
pub fn get_files(folder_path: String, matcher: PathMatcher) -> impl Iterator<Item = FileEntry> {
let temp_folder_path = folder_path.clone();

walkdir::WalkDir::new(&folder_path)
.follow_links(true)
.sort_by_file_name()
.into_iter()
.filter_entry(move |e| {
let rel_path = path_to_str(e.path().strip_prefix(&temp_folder_path).unwrap());
if e.file_type().is_dir() {
// dir must be explicitly excluded
!matcher.is_path_excluded(&rel_path)
} else {
matcher.is_path_included(&rel_path)
}
})
.filter_map(|e| e.ok())
.filter(|e| e.file_type().is_file())
.filter_map(move |e| {
Expand All @@ -29,11 +40,7 @@ pub fn get_files(folder_path: String, matcher: PathMatcher) -> impl Iterator<Ite
} else {
rel_path
};
if matcher.is_path_included(&rel_path) {
Some(FileEntry { rel_path, full_canonical_path })
} else {
None
}
Some(FileEntry { rel_path, full_canonical_path })
})
}

Expand Down Expand Up @@ -172,6 +179,9 @@ impl PathMatcher {
pub fn is_path_included(&self, path: &str) -> bool {
!self.exclude_matcher.is_match(path) && (self.include_matcher.is_empty() || self.include_matcher.is_match(path))
}
pub fn is_path_excluded(&self, path: &str) -> bool {
self.exclude_matcher.is_match(path)
}
}

#[cfg(not(feature = "include-exclude"))]
Expand All @@ -182,4 +192,7 @@ impl PathMatcher {
pub fn is_path_included(&self, _path: &str) -> bool {
true
}
pub fn is_path_excluded(&self, _path: &str) -> bool {
false
}
}
Loading