Skip to content

Commit 8da7373

Browse files
Soubhik-10mattsse
andauthored
Added options to forge bind to define crate description and license (#10203)
* desc and license bind args * license mapping * helper fn * clippy * removed dup logic * touchup * touchup --------- Co-authored-by: Matthias Seitz <matthias.seitz@outlook.de>
1 parent c3c880f commit 8da7373

File tree

2 files changed

+47
-3
lines changed

2 files changed

+47
-3
lines changed

crates/forge/src/cmd/bind.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,18 @@ pub struct BindArgs {
5252
#[arg(long, default_value = DEFAULT_CRATE_VERSION, value_name = "VERSION")]
5353
crate_version: String,
5454

55+
/// The description of the Rust crate to generate.
56+
///
57+
/// This will be added to the package.description field in Cargo.toml.
58+
#[arg(long, default_value = "", value_name = "DESCRIPTION")]
59+
crate_description: String,
60+
61+
/// The license of the Rust crate to generate.
62+
///
63+
/// This will be added to the package.license field in Cargo.toml.
64+
#[arg(long, value_name = "LICENSE", default_value = "")]
65+
crate_license: String,
66+
5567
/// Generate the bindings as a module instead of a crate.
5668
#[arg(long)]
5769
module: bool,
@@ -232,6 +244,8 @@ impl BindArgs {
232244
solmacrogen.write_to_crate(
233245
&self.crate_name,
234246
&self.crate_version,
247+
&self.crate_description,
248+
&self.crate_license,
235249
bindings_root,
236250
self.single_file,
237251
self.alloy_version.clone(),

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

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,8 @@ impl MultiSolMacroGen {
106106
&mut self,
107107
name: &str,
108108
version: &str,
109+
description: &str,
110+
license: &str,
109111
bindings_path: &Path,
110112
single_file: bool,
111113
alloy_version: Option<String>,
@@ -115,7 +117,6 @@ impl MultiSolMacroGen {
115117
self.generate_bindings(all_derives)?;
116118

117119
let src = bindings_path.join("src");
118-
119120
let _ = fs::create_dir_all(&src);
120121

121122
// Write Cargo.toml
@@ -125,11 +126,23 @@ impl MultiSolMacroGen {
125126
name = "{name}"
126127
version = "{version}"
127128
edition = "2021"
128-
129-
[dependencies]
130129
"#
131130
);
132131

132+
if !description.is_empty() {
133+
toml_contents.push_str(&format!("description = \"{description}\"\n"));
134+
}
135+
136+
if !license.is_empty() {
137+
let formatted_licenses: Vec<String> =
138+
license.split(',').map(Self::parse_license_alias).collect();
139+
140+
let formatted_license = formatted_licenses.join(" OR ");
141+
toml_contents.push_str(&format!("license = \"{formatted_license}\"\n"));
142+
}
143+
144+
toml_contents.push_str("\n[dependencies]\n");
145+
133146
let alloy_dep = Self::get_alloy_dep(alloy_version, alloy_rev);
134147
write!(toml_contents, "{alloy_dep}")?;
135148

@@ -174,6 +187,23 @@ edition = "2021"
174187
Ok(())
175188
}
176189

190+
/// Attempts to detect the appropriate license.
191+
pub fn parse_license_alias(license: &str) -> String {
192+
match license.trim().to_lowercase().as_str() {
193+
"mit" => "MIT".to_string(),
194+
"apache" | "apache2" | "apache20" | "apache2.0" => "Apache-2.0".to_string(),
195+
"gpl" | "gpl3" => "GPL-3.0".to_string(),
196+
"lgpl" | "lgpl3" => "LGPL-3.0".to_string(),
197+
"agpl" | "agpl3" => "AGPL-3.0".to_string(),
198+
"bsd" | "bsd3" => "BSD-3-Clause".to_string(),
199+
"bsd2" => "BSD-2-Clause".to_string(),
200+
"mpl" | "mpl2" => "MPL-2.0".to_string(),
201+
"isc" => "ISC".to_string(),
202+
"unlicense" => "Unlicense".to_string(),
203+
_ => license.trim().to_string(),
204+
}
205+
}
206+
177207
pub fn write_to_module(
178208
&mut self,
179209
bindings_path: &Path,

0 commit comments

Comments
 (0)