Skip to content

Commit 0ce687b

Browse files
committed
both jwalk and ignore
1 parent 7c11d01 commit 0ce687b

File tree

1 file changed

+111
-4
lines changed

1 file changed

+111
-4
lines changed

src/project_builder.rs

Lines changed: 111 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use std::{
66

77
use error_stack::{Result, ResultExt};
88
use fast_glob::glob_match;
9+
use ignore::WalkBuilder;
910
use jwalk::{DirEntry, WalkDir};
1011
use rayon::iter::{IntoParallelIterator, ParallelIterator};
1112
use regex::Regex;
@@ -53,23 +54,51 @@ impl<'a> ProjectBuilder<'a> {
5354

5455
#[instrument(level = "debug", skip_all)]
5556
pub fn build(&mut self) -> Result<Project, Error> {
57+
self.build_ignore()
58+
//self.build_jwalk()
59+
}
60+
61+
#[instrument(level = "debug", skip_all)]
62+
pub fn build_ignore(&mut self) -> Result<Project, Error> {
63+
let mut entry_types = Vec::new();
64+
65+
let mut builder = WalkBuilder::new(&self.base_path);
66+
builder.hidden(false);
67+
let walkdir = builder.build();
68+
69+
for entry in walkdir {
70+
let entry = entry.change_context(Error::Io)?;
71+
entry_types.push(self.build_entry_type_ignore(entry)?);
72+
}
73+
self.build_project_from_entry_types(entry_types)
74+
//self.build_project_from_entry_types_rayon(entry_types)
75+
}
76+
77+
fn build_entry_type_ignore(&mut self, entry: ignore::DirEntry) -> Result<EntryType, Error> {
78+
let absolute_path = entry.path();
79+
80+
let is_dir = entry.file_type().unwrap().is_dir();
81+
self.build_entry_type(absolute_path.to_path_buf(), is_dir)
82+
}
83+
84+
#[instrument(level = "debug", skip_all)]
85+
pub fn build_jwalk(&mut self) -> Result<Project, Error> {
5686
let mut entry_types = Vec::new();
5787

5888
for entry in WalkDir::new(&self.base_path).follow_links(true).skip_hidden(false).into_iter() {
5989
let entry = match entry.change_context(Error::Io) {
6090
Ok(entry) => entry,
6191
Err(_) => continue,
6292
};
63-
entry_types.push(self.build_entry_type(entry)?);
93+
entry_types.push(self.build_entry_type_jwalk(entry)?);
6494
}
6595
self.build_project_from_entry_types(entry_types)
6696
}
6797

68-
fn build_entry_type(&mut self, entry: DirEntry<((), ())>) -> Result<EntryType, Error> {
69-
let absolute_path = entry.path();
98+
fn build_entry_type(&mut self, absolute_path: PathBuf, is_dir: bool) -> Result<EntryType, Error> {
7099
let relative_path = absolute_path.strip_prefix(&self.base_path).change_context(Error::Io)?.to_owned();
71100

72-
if entry.file_type().is_dir() {
101+
if is_dir {
73102
Ok(EntryType::Directory(absolute_path.to_owned(), relative_path.to_owned()))
74103
} else {
75104
let file_name = relative_path.file_name().expect("expected a file_name");
@@ -95,7 +124,85 @@ impl<'a> ProjectBuilder<'a> {
95124
}
96125
}
97126

127+
fn build_entry_type_jwalk(&mut self, entry: DirEntry<((), ())>) -> Result<EntryType, Error> {
128+
let absolute_path = entry.path();
129+
let is_dir = entry.file_type().is_dir();
130+
self.build_entry_type(absolute_path, is_dir)
131+
}
132+
98133
fn build_project_from_entry_types(&mut self, entry_types: Vec<EntryType>) -> Result<Project, Error> {
134+
let mut owned_files_vec = Vec::new();
135+
for entry_type in entry_types {
136+
match entry_type {
137+
EntryType::PossiblyOwnedFile(absolute_path, relative_path) => {
138+
if matches_globs(&relative_path, &self.config.owned_globs) && !matches_globs(&relative_path, &self.config.unowned_globs)
139+
{
140+
owned_files_vec.push(absolute_path);
141+
}
142+
}
143+
EntryType::Directory(absolute_path, relative_path) => {
144+
if relative_path.parent() == Some(Path::new(&self.config.vendored_gems_path)) {
145+
let file_name = relative_path.file_name().expect("expected a file_name");
146+
self.vendored_gems.push(VendoredGem {
147+
path: absolute_path,
148+
name: file_name.to_string_lossy().to_string(),
149+
});
150+
}
151+
}
152+
EntryType::RubyPackage(absolute_path, relative_path) => {
153+
if let Some(owner) = ruby_package_owner(&absolute_path).unwrap() {
154+
self.packages.push(Package {
155+
path: relative_path.clone(),
156+
owner,
157+
package_type: PackageType::Ruby,
158+
});
159+
}
160+
}
161+
EntryType::JavascriptPackage(absolute_path, relative_path) => {
162+
if let Some(owner) = javascript_package_owner(&absolute_path).unwrap() {
163+
self.packages.push(Package {
164+
path: relative_path.clone(),
165+
owner,
166+
package_type: PackageType::Javascript,
167+
});
168+
}
169+
}
170+
EntryType::CodeownerFile(absolute_path, relative_path) => {
171+
let owner = std::fs::read_to_string(absolute_path).unwrap();
172+
let owner = owner.trim().to_owned();
173+
self.directory_codeowner_files.push(DirectoryCodeownersFile {
174+
path: relative_path.clone(),
175+
owner,
176+
});
177+
}
178+
EntryType::TeamFile(absolute_path, _relative_path) => {
179+
let file = File::open(&absolute_path).unwrap();
180+
let deserializer: deserializers::Team = serde_yaml::from_reader(file).unwrap();
181+
self.teams.push(Team {
182+
path: absolute_path.to_owned(),
183+
name: deserializer.name,
184+
github_team: deserializer.github.team,
185+
owned_globs: deserializer.owned_globs,
186+
owned_gems: deserializer.ruby.map(|ruby| ruby.owned_gems).unwrap_or_default(),
187+
avoid_ownership: deserializer.github.do_not_add_to_codeowners_file,
188+
});
189+
}
190+
}
191+
}
192+
let owned_files = owned_files(owned_files_vec);
193+
194+
Ok(Project {
195+
base_path: self.base_path.to_owned(),
196+
files: owned_files,
197+
vendored_gems: self.vendored_gems.clone(),
198+
teams: self.teams.clone(),
199+
packages: self.packages.clone(),
200+
codeowners_file_path: self.codeowners_file_path.to_path_buf(),
201+
directory_codeowner_files: self.directory_codeowner_files.clone(),
202+
})
203+
}
204+
205+
fn build_project_from_entry_types_rayon(&mut self, entry_types: Vec<EntryType>) -> Result<Project, Error> {
99206
// Process entry types in parallel and collect results
100207
let (owned_files_vec, packages, vendored_gems, directory_codeowners, teams): (Vec<_>, Vec<_>, Vec<_>, Vec<_>, Vec<_>) = entry_types
101208
.into_par_iter()

0 commit comments

Comments
 (0)