Skip to content

Commit

Permalink
Refactor PackageStore.collect(...)
Browse files Browse the repository at this point in the history
Remove the flag filtering for vendored files from the method. Since
there is only one caller needing the filter, this is moved to the
caller. Provide a convenience method to get a list of all package files
under the vendor directory.
  • Loading branch information
Johannes Hutter committed Nov 26, 2023
1 parent acfa7f9 commit 66e321e
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 20 deletions.
2 changes: 1 addition & 1 deletion src/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,7 @@ pub async fn list() -> miette::Result<()> {

store.populate(&manifest).await?;

let protos = store.collect(&store.proto_vendor_path(), true).await;
let protos = store.collect(&store.proto_vendor_path()).await;

let cwd = {
let cwd = std::env::current_dir()
Expand Down
8 changes: 2 additions & 6 deletions src/generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,9 @@ impl Generator {
let store = PackageStore::current().await?;
let manifest = Manifest::read().await?;

let vendored_path = store
.proto_vendor_path()
.join(manifest.package.name.to_string());

store.populate(&manifest).await?;

let protos = store.collect(&vendored_path, true).await;
let protos = store.populated_files(&manifest).await;
let includes = &[store.proto_vendor_path()];

match self {
Expand Down Expand Up @@ -136,7 +132,7 @@ impl Generator {
store.populate(&manifest).await?;

// Collect non-vendored protos
let protos = store.collect(&store.proto_path(), false).await;
let protos = store.populated_files(&manifest).await;

info!(":: initializing code generator");

Expand Down
32 changes: 19 additions & 13 deletions src/package/store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,12 @@ impl PackageStore {
self.root.join(Self::PROTO_VENDOR_PATH)
}

/// Path to where the package contents are populated.
pub fn populated_path(&self, manifest: &Manifest) -> PathBuf {
self.proto_vendor_path()
.join(manifest.package.name.to_string())
}

/// Creates the expected directory structure for `buffrs`
pub async fn create(path: PathBuf) -> miette::Result<Self> {
let store = PackageStore::new(path);
Expand Down Expand Up @@ -146,9 +152,7 @@ impl PackageStore {
manifest: &Manifest,
) -> miette::Result<crate::validation::Violations> {
let root_path = self.proto_vendor_path();
let source_files = self
.collect(&root_path.join(manifest.package.name.to_string()), true)
.await;
let source_files = self.populated_files(manifest).await;

let mut parser = crate::validation::Validator::new(&root_path, &manifest.package.name);

Expand Down Expand Up @@ -184,7 +188,7 @@ impl PackageStore {
let pkg_path = self.proto_path();
let mut entries = BTreeMap::new();

for entry in self.collect(&pkg_path, false).await {
for entry in self.populated_files(&manifest).await {
let path = entry.strip_prefix(&pkg_path).into_diagnostic()?;
let contents = tokio::fs::read(&entry).await.unwrap();
entries.insert(path.into(), contents.into());
Expand All @@ -207,18 +211,11 @@ impl PackageStore {
}

/// Collect .proto files in a given path
pub async fn collect(&self, path: &Path, vendored: bool) -> Vec<PathBuf> {
pub async fn collect(&self, path: &Path) -> Vec<PathBuf> {
let mut paths: Vec<_> = WalkDir::new(path)
.into_iter()
.filter_map(Result::ok)
.map(|entry| entry.into_path())
.filter(|path| {
if vendored {
return true;
}

!path.starts_with(self.proto_vendor_path())
})
.filter(|path| {
let ext = path.extension().map(|s| s.to_str());

Expand Down Expand Up @@ -256,7 +253,11 @@ impl PackageStore {
))?;
}

for entry in self.collect(&source_path, false).await {
for entry in self.collect(&source_path).await {
if entry.starts_with(self.proto_vendor_path()) {
continue;
}

let file_name = entry.strip_prefix(&source_path).into_diagnostic()?;
let target_path = target_dir.join(file_name);
tokio::fs::create_dir_all(target_path.parent().unwrap())
Expand All @@ -274,6 +275,11 @@ impl PackageStore {

Ok(())
}

/// Get the paths of all files under management after population
pub async fn populated_files(&self, manifest: &Manifest) -> Vec<PathBuf> {
self.collect(&self.populated_path(manifest)).await
}
}

#[test]
Expand Down

0 comments on commit 66e321e

Please sign in to comment.