feat: add cargo change detection support (#599)

* feat: add cargo change detection support

* chore: allow clippy main

* chore: remove doctest main
This commit is contained in:
Matthias Seitz 2021-11-20 15:47:36 +01:00 committed by GitHub
parent ffb1a8bf1d
commit cd2c9f6dd1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 30 additions and 2 deletions

View File

@ -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 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 ```toml
[build-dependencies] [build-dependencies]
@ -21,6 +23,8 @@ fn main() {
.build() .build()
.unwrap(); .unwrap();
let output = project.compile().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();
} }
``` ```

View File

@ -115,6 +115,30 @@ impl<Artifacts: ArtifactOutput> Project<Artifacts> {
Source::read_all_from(self.paths.sources.as_path()) 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" /// Attempts to read all unique libraries that are used as imports like "hardhat/console.sol"
fn resolved_libraries( fn resolved_libraries(
&self, &self,