From cd2c9f6dd11d12ddb8db64b98531c10852befe0f Mon Sep 17 00:00:00 2001 From: Matthias Seitz Date: Sat, 20 Nov 2021 15:47:36 +0100 Subject: [PATCH] feat: add cargo change detection support (#599) * feat: add cargo change detection support * chore: allow clippy main * chore: remove doctest main --- ethers-solc/README.md | 8 ++++++-- ethers-solc/src/lib.rs | 24 ++++++++++++++++++++++++ 2 files changed, 30 insertions(+), 2 deletions(-) diff --git a/ethers-solc/README.md b/ethers-solc/README.md index fee2a724..2ee3f438 100644 --- a/ethers-solc/README.md +++ b/ethers-solc/README.md @@ -4,7 +4,9 @@ Utilities for working with native `solc` and compiling projects. To also compile contracts during `cargo build` (so that ethers `abigen!` can pull in updated abi automatically) you can configure a `ethers_solc::Project` in your `build.rs` file -First add `ethers-solc` to your cargo build-dependencies +First add `ethers-solc` to your cargo build-dependencies. + +Once you compiled the project, you can configure cargo change detection with `rerun_if_sources_changed`, so that cargo will execute the `build.rs` file if a contract in the sources directory has changed ```toml [build-dependencies] @@ -21,6 +23,8 @@ fn main() { .build() .unwrap(); let output = project.compile().unwrap(); - println!("{}", output); + + // Tell Cargo that if a source file changes, to rerun this build script. + project.rerun_if_sources_changed(); } ``` \ No newline at end of file diff --git a/ethers-solc/src/lib.rs b/ethers-solc/src/lib.rs index 0bdd7bbe..a2b7033a 100644 --- a/ethers-solc/src/lib.rs +++ b/ethers-solc/src/lib.rs @@ -115,6 +115,30 @@ impl Project { Source::read_all_from(self.paths.sources.as_path()) } + /// This emits the cargo [`rerun-if-changed`](https://doc.rust-lang.org/cargo/reference/build-scripts.html#cargorerun-if-changedpath) instruction. + /// Which tells Cargo to re-run the build script if a file inside the project's sources + /// directory has changed. + /// + /// Use this if you compile a project in a `build.rs` file. + /// + /// # Example `build.rs` file + /// + /// + /// ```no_run + /// use ethers_solc::{Project, ProjectPathsConfig}; + /// // configure the project with all its paths, solc, cache etc. + /// let project = Project::builder() + /// .paths(ProjectPathsConfig::hardhat(env!("CARGO_MANIFEST_DIR")).unwrap()) + /// .build() + /// .unwrap(); + /// let output = project.compile().unwrap(); + /// // Tell Cargo that if a source file changes, to rerun this build script. + /// project.rerun_if_sources_changed(); + /// ``` + pub fn rerun_if_sources_changed(&self) { + println!("cargo:rerun-if-changed={}", self.paths.sources.display()) + } + /// Attempts to read all unique libraries that are used as imports like "hardhat/console.sol" fn resolved_libraries( &self,