Skip to content
This repository has been archived by the owner on Oct 19, 2024. It is now read-only.

Commit

Permalink
feat(abigen): search json recursively (#733)
Browse files Browse the repository at this point in the history
  • Loading branch information
mattsse authored Dec 25, 2021
1 parent 4b4ebd7 commit 2a98b34
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 6 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions ethers-contract/ethers-contract-abigen/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ reqwest = { version = "0.11.3", default-features = false, features = ["blocking"
once_cell = "1.8.0"
cfg-if = "1.0.0"
dunce = "1.0.2"
walkdir = "2.3.2"

[target.'cfg(target_arch = "wasm32")'.dependencies]
# NOTE: this enables wasm compatibility for getrandom indirectly
Expand Down
9 changes: 3 additions & 6 deletions ethers-contract/ethers-contract-abigen/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -266,12 +266,9 @@ impl MultiAbigen {
/// ```
pub fn from_json_files(dir: impl AsRef<Path>) -> Result<Self> {
let mut abis = Vec::new();
for file in fs::read_dir(dir)?.into_iter().filter_map(std::io::Result::ok).filter(|p| {
p.path().is_file() && p.path().extension().and_then(|ext| ext.to_str()) == Some("json")
}) {
let file: fs::DirEntry = file;
if let Some(file_name) = file.path().file_stem().and_then(|s| s.to_str()) {
let content = fs::read_to_string(file.path())?;
for file in util::json_files(dir) {
if let Some(file_name) = file.file_stem().and_then(|s| s.to_str()) {
let content = fs::read_to_string(&file)?;
abis.push((file_name.to_string(), content));
}
}
Expand Down
11 changes: 11 additions & 0 deletions ethers-contract/ethers-contract-abigen/src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,17 @@ fn take_while(s: &str, mut predicate: impl FnMut(char) -> bool) -> (&str, &str)
s.split_at(index)
}

/// Returns a list of absolute paths to all the json files under the root
pub fn json_files(root: impl AsRef<std::path::Path>) -> Vec<PathBuf> {
walkdir::WalkDir::new(root)
.into_iter()
.filter_map(Result::ok)
.filter(|e| e.file_type().is_file())
.filter(|e| e.path().extension().map(|ext| ext == "json").unwrap_or_default())
.map(|e| e.path().into())
.collect()
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down

0 comments on commit 2a98b34

Please sign in to comment.