Skip to content

Commit

Permalink
fix(bind): make more ergonomic
Browse files Browse the repository at this point in the history
  • Loading branch information
charisma98 committed Feb 17, 2022
1 parent 2ebf70f commit 3300f41
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 31 deletions.
22 changes: 11 additions & 11 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

31 changes: 11 additions & 20 deletions cli/src/cmd/bind.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,20 +31,12 @@ pub struct BindArgs {

#[clap(
help = "Path to where the contract artifacts are stored",
long = "out",
long = "bindings-path",
short,
value_hint = ValueHint::DirPath
)]
#[serde(rename = "out", skip_serializing_if = "Option::is_none")]
pub out_path: Option<PathBuf>,

#[clap(
help = "A directory at which to write the generated rust crate",
long = "bindings-root",
value_hint = ValueHint::DirPath
)]
#[serde(skip)]
pub bindings_root: Option<PathBuf>,
pub bindings: Option<PathBuf>,

#[clap(
long = "crate-name",
Expand All @@ -62,6 +54,9 @@ pub struct BindArgs {
#[serde(skip)]
crate_version: String,

#[clap(long, help = "Generate the bindings as a module instead of a crate")]
module: bool,

#[clap(
long = "overwrite",
help = "Overwrite existing generated bindings. If set to false, the command will check that the bindings are correct, and then exit. If set to true, it will instead delete and overwrite the bindings."
Expand All @@ -83,12 +78,7 @@ impl BindArgs {

/// Get the path to the root of the autogenerated crate
fn bindings_root(&self) -> PathBuf {
self.bindings_root.clone().unwrap_or_else(|| self.artifacts().join("bindings"))
}

/// `true` if the arguments set `crate_version` or `crate_name`
fn gen_crate(&self) -> bool {
self.crate_name != DEFAULT_CRATE_NAME || self.crate_version != DEFAULT_CRATE_VERSION
self.bindings.clone().unwrap_or_else(|| self.artifacts().join("bindings"))
}

/// `true` if the bindings root already exists
Expand All @@ -103,7 +93,7 @@ impl BindArgs {
eyre::ensure!(
!multi.is_empty(),
r#"
No contract artifacts found. Hint: Have you built your contracts yet? `forge bind` does not currently invoke `forge build`, although this is planned for future versions.
No contract artifacts found. Hint: Have you built your contracts yet? `forge bind` does not currently invoke `forge build`, although this is planned for future versions.
"#
);
Ok(multi)
Expand All @@ -112,8 +102,8 @@ impl BindArgs {
/// Check that the existing bindings match the expected abigen output
fn check_existing_bindings(&self) -> eyre::Result<()> {
let bindings = self.get_multi()?.build()?;
println!("Checking bindings for {} contracts", bindings.len());
if self.gen_crate() {
println!("Checking bindings for {} contracts.", bindings.len());
if !self.module {
bindings.ensure_consistent_crate(
&self.crate_name,
&self.crate_version,
Expand All @@ -123,14 +113,15 @@ impl BindArgs {
} else {
bindings.ensure_consistent_module(self.bindings_root(), self.single_file)?;
}
println!("OK.");
Ok(())
}

/// Generate the bindings
fn generate_bindings(&self) -> eyre::Result<()> {
let bindings = self.get_multi()?.build()?;
println!("Generating bindings for {} contracts", bindings.len());
if self.gen_crate() {
if !self.module {
bindings.write_to_crate(
&self.crate_name,
&self.crate_version,
Expand Down

0 comments on commit 3300f41

Please sign in to comment.