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

This commit is contained in:
Matthias Seitz 2022-07-14 19:57:10 +02:00 committed by GitHub
parent d509f7a7c6
commit cca3fcec93
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 35 additions and 16 deletions

View File

@ -119,13 +119,25 @@ impl ProjectPathsConfig {
Ok(Source::read_all_from(&self.scripts)?) 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> { pub fn input_files(&self) -> Vec<PathBuf> {
utils::source_files(&self.sources) self.input_files_iter().collect()
.into_iter()
.chain(utils::source_files(&self.tests))
.chain(utils::source_files(&self.scripts))
.collect()
} }
/// Returns the combined set of `Self::read_sources` + `Self::read_tests` + `Self::read_scripts` /// Returns the combined set of `Self::read_sources` + `Self::read_tests` + `Self::read_scripts`

View File

@ -72,6 +72,22 @@ pub fn find_version_pragma(contract: &str) -> Option<Match> {
RE_SOL_PRAGMA_VERSION.captures(contract)?.name("version") 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, /// 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. /// if the path is a solidity file.
/// ///
@ -86,16 +102,7 @@ pub fn find_version_pragma(contract: &str) -> Option<Match> {
/// let sources = utils::source_files("./contracts"); /// let sources = utils::source_files("./contracts");
/// ``` /// ```
pub fn source_files(root: impl AsRef<Path>) -> Vec<PathBuf> { pub fn source_files(root: impl AsRef<Path>) -> Vec<PathBuf> {
WalkDir::new(root) source_files_iter(root).collect()
.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()
} }
/// Returns a list of _unique_ paths to all folders under `root` that contain at least one solidity /// Returns a list of _unique_ paths to all folders under `root` that contain at least one solidity