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

perf(solc): add iterator function for finding sol files #1480

Merged
merged 1 commit into from
Jul 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 18 additions & 6 deletions ethers-solc/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,13 +119,25 @@ impl ProjectPathsConfig {
Ok(Source::read_all_from(&self.scripts)?)
}

/// Returns the combined set solidity file paths for `Self::sources` and `Self::tests`
/// Returns true if the there is at least one solidity file in this config.
///
/// See also, `Self::input_files()`
pub fn has_input_files(&self) -> bool {
self.input_files_iter().next().is_some()
}

/// Returns an iterator that yields all solidity file paths for `Self::sources`, `Self::tests`
/// and `Self::scripts`
pub fn input_files_iter(&self) -> impl Iterator<Item = PathBuf> + '_ {
utils::source_files_iter(&self.sources)
.chain(utils::source_files_iter(&self.tests))
.chain(utils::source_files_iter(&self.scripts))
}

/// Returns the combined set solidity file paths for `Self::sources`, `Self::tests` and
/// `Self::scripts`
pub fn input_files(&self) -> Vec<PathBuf> {
utils::source_files(&self.sources)
.into_iter()
.chain(utils::source_files(&self.tests))
.chain(utils::source_files(&self.scripts))
.collect()
self.input_files_iter().collect()
}

/// Returns the combined set of `Self::read_sources` + `Self::read_tests` + `Self::read_scripts`
Expand Down
27 changes: 17 additions & 10 deletions ethers-solc/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,22 @@ pub fn find_version_pragma(contract: &str) -> Option<Match> {
RE_SOL_PRAGMA_VERSION.captures(contract)?.name("version")
}

/// Returns an iterator that yields all solidity/yul files funder under the given root path or the
/// `root` itself, if it is a sol/yul file
///
/// This also follows symlinks.
pub fn source_files_iter(root: impl AsRef<Path>) -> impl Iterator<Item = PathBuf> {
WalkDir::new(root)
.follow_links(true)
.into_iter()
.filter_map(Result::ok)
.filter(|e| e.file_type().is_file())
.filter(|e| {
e.path().extension().map(|ext| (ext == "sol") || (ext == "yul")).unwrap_or_default()
})
.map(|e| e.path().into())
}

/// Returns a list of absolute paths to all the solidity files under the root, or the file itself,
/// if the path is a solidity file.
///
Expand All @@ -86,16 +102,7 @@ pub fn find_version_pragma(contract: &str) -> Option<Match> {
/// let sources = utils::source_files("./contracts");
/// ```
pub fn source_files(root: impl AsRef<Path>) -> Vec<PathBuf> {
WalkDir::new(root)
.follow_links(true)
.into_iter()
.filter_map(Result::ok)
.filter(|e| e.file_type().is_file())
.filter(|e| {
e.path().extension().map(|ext| (ext == "sol") || (ext == "yul")).unwrap_or_default()
})
.map(|e| e.path().into())
.collect()
source_files_iter(root).collect()
}

/// Returns a list of _unique_ paths to all folders under `root` that contain at least one solidity
Expand Down