Skip to content

Commit

Permalink
Let the get_files function skip excluded directories
Browse files Browse the repository at this point in the history
  • Loading branch information
jianse committed Sep 13, 2024
1 parent edeab18 commit 63b633e
Showing 1 changed file with 18 additions and 5 deletions.
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
}
}

0 comments on commit 63b633e

Please sign in to comment.