Skip to content

Commit 1bcd17c

Browse files
authored
feat: add --alloy-rev and fix forge bind re-run on built project (#9861)
* add --alloy-rev, marked as conflicting with --alloy-version, updated --alloy-version to use crates.io whereas rev uses git * fix forge bind upon re-run after crate was built * clarify github / crates target * minor doc fixes * dedupe dep check * fix fmt
1 parent 55785b7 commit 1bcd17c

File tree

2 files changed

+36
-17
lines changed

2 files changed

+36
-17
lines changed

crates/forge/bin/cmd/bind.rs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,11 +83,15 @@ pub struct BindArgs {
8383
#[arg(long, hide = true)]
8484
alloy: bool,
8585

86-
/// Specify the alloy version.
86+
/// Specify the `alloy` version on Crates.
8787
#[arg(long)]
8888
alloy_version: Option<String>,
8989

90-
/// Generate bindings for the `ethers` library, instead of `alloy` (removed).
90+
/// Specify the `alloy` revision on GitHub.
91+
#[arg(long, conflicts_with = "alloy_version")]
92+
alloy_rev: Option<String>,
93+
94+
/// Generate bindings for the `ethers` library (removed), instead of `alloy`.
9195
#[arg(long, hide = true)]
9296
ethers: bool,
9397

@@ -159,6 +163,11 @@ impl BindArgs {
159163
return None;
160164
}
161165

166+
// Ignore the `target` directory in case the user has built the project.
167+
if path.iter().any(|comp| comp == "target") {
168+
return None;
169+
}
170+
162171
// We don't want `.metadata.json` files.
163172
let stem = path.file_stem()?.to_str()?;
164173
if stem.ends_with(".metadata") {
@@ -207,6 +216,7 @@ impl BindArgs {
207216
!self.skip_cargo_toml,
208217
self.module,
209218
self.alloy_version.clone(),
219+
self.alloy_rev.clone(),
210220
)?;
211221
sh_println!("OK.")?;
212222
Ok(())
@@ -225,6 +235,7 @@ impl BindArgs {
225235
bindings_root,
226236
self.single_file,
227237
self.alloy_version.clone(),
238+
self.alloy_rev.clone(),
228239
)?;
229240
} else {
230241
trace!(single_file = self.single_file, "generating module");

crates/sol-macro-gen/src/sol_macro_gen.rs

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ impl MultiSolMacroGen {
108108
bindings_path: &Path,
109109
single_file: bool,
110110
alloy_version: Option<String>,
111+
alloy_rev: Option<String>,
111112
) -> Result<()> {
112113
self.generate_bindings()?;
113114

@@ -127,13 +128,7 @@ edition = "2021"
127128
"#
128129
);
129130

130-
let alloy_dep = if let Some(alloy_version) = alloy_version {
131-
format!(
132-
r#"alloy = {{ git = "https://github.com/alloy-rs/alloy", rev = "{alloy_version}", features = ["sol-types", "contract"] }}"#
133-
)
134-
} else {
135-
r#"alloy = { git = "https://github.com/alloy-rs/alloy", features = ["sol-types", "contract"] }"#.to_string()
136-
};
131+
let alloy_dep = Self::get_alloy_dep(alloy_version, alloy_rev);
137132
write!(toml_contents, "{alloy_dep}")?;
138133

139134
fs::write(cargo_toml_path, toml_contents).wrap_err("Failed to write Cargo.toml")?;
@@ -235,9 +230,10 @@ edition = "2021"
235230
check_cargo_toml: bool,
236231
is_mod: bool,
237232
alloy_version: Option<String>,
233+
alloy_rev: Option<String>,
238234
) -> Result<()> {
239235
if check_cargo_toml {
240-
self.check_cargo_toml(name, version, crate_path, alloy_version)?;
236+
self.check_cargo_toml(name, version, crate_path, alloy_version, alloy_rev)?;
241237
}
242238

243239
let mut super_contents = String::new();
@@ -304,6 +300,7 @@ edition = "2021"
304300
version: &str,
305301
crate_path: &Path,
306302
alloy_version: Option<String>,
303+
alloy_rev: Option<String>,
307304
) -> Result<()> {
308305
eyre::ensure!(crate_path.is_dir(), "Crate path must be a directory");
309306

@@ -315,13 +312,7 @@ edition = "2021"
315312

316313
let name_check = format!("name = \"{name}\"");
317314
let version_check = format!("version = \"{version}\"");
318-
let alloy_dep_check = if let Some(version) = alloy_version {
319-
format!(
320-
r#"alloy = {{ git = "https://github.com/alloy-rs/alloy", rev = "{version}", features = ["sol-types", "contract"] }}"#,
321-
)
322-
} else {
323-
r#"alloy = { git = "https://github.com/alloy-rs/alloy", features = ["sol-types", "contract"] }"#.to_string()
324-
};
315+
let alloy_dep_check = Self::get_alloy_dep(alloy_version, alloy_rev);
325316
let toml_consistent = cargo_toml_contents.contains(&name_check) &&
326317
cargo_toml_contents.contains(&version_check) &&
327318
cargo_toml_contents.contains(&alloy_dep_check);
@@ -333,6 +324,23 @@ edition = "2021"
333324

334325
Ok(())
335326
}
327+
328+
/// Returns the `alloy` dependency string for the Cargo.toml file.
329+
/// If `alloy_version` is provided, it will use that version from crates.io.
330+
/// If `alloy_rev` is provided, it will use that revision from the GitHub repository.
331+
fn get_alloy_dep(alloy_version: Option<String>, alloy_rev: Option<String>) -> String {
332+
if let Some(alloy_version) = alloy_version {
333+
format!(
334+
r#"alloy = {{ version = "{alloy_version}", features = ["sol-types", "contract"] }}"#,
335+
)
336+
} else if let Some(alloy_rev) = alloy_rev {
337+
format!(
338+
r#"alloy = {{ git = "https://github.com/alloy-rs/alloy", rev = "{alloy_rev}", features = ["sol-types", "contract"] }}"#,
339+
)
340+
} else {
341+
r#"alloy = { git = "https://github.com/alloy-rs/alloy", features = ["sol-types", "contract"] }"#.to_string()
342+
}
343+
}
336344
}
337345

338346
fn write_mod_name(contents: &mut String, name: &str) -> Result<()> {

0 commit comments

Comments
 (0)