diff --git a/src/command.rs b/src/command.rs index 062074d8..bf7f319a 100644 --- a/src/command.rs +++ b/src/command.rs @@ -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() diff --git a/src/generator.rs b/src/generator.rs index d308e3fe..4bfee12c 100644 --- a/src/generator.rs +++ b/src/generator.rs @@ -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 { @@ -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"); diff --git a/src/package/store.rs b/src/package/store.rs index 19fee6e7..ea931ec6 100644 --- a/src/package/store.rs +++ b/src/package/store.rs @@ -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 { let store = PackageStore::new(path); @@ -146,9 +152,7 @@ impl PackageStore { manifest: &Manifest, ) -> miette::Result { 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); @@ -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()); @@ -207,18 +211,11 @@ impl PackageStore { } /// Collect .proto files in a given path - pub async fn collect(&self, path: &Path, vendored: bool) -> Vec { + pub async fn collect(&self, path: &Path) -> Vec { 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()); @@ -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()) @@ -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 { + self.collect(&self.populated_path(manifest)).await + } } #[test]