diff --git a/ethers-solc/src/config.rs b/ethers-solc/src/config.rs index cc4d98f0..75dc4d9a 100644 --- a/ethers-solc/src/config.rs +++ b/ethers-solc/src/config.rs @@ -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 + '_ { + 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 { - 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` diff --git a/ethers-solc/src/utils.rs b/ethers-solc/src/utils.rs index eedea95f..487055cf 100644 --- a/ethers-solc/src/utils.rs +++ b/ethers-solc/src/utils.rs @@ -72,6 +72,22 @@ pub fn find_version_pragma(contract: &str) -> Option { 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) -> impl Iterator { + 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. /// @@ -86,16 +102,7 @@ pub fn find_version_pragma(contract: &str) -> Option { /// let sources = utils::source_files("./contracts"); /// ``` pub fn source_files(root: impl AsRef) -> Vec { - 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