From 9c3fcca32eb52e9cba3a35b646902ccce7c7a0ba Mon Sep 17 00:00:00 2001 From: Serial <69764315+Serial-ATA@users.noreply.github.com> Date: Wed, 25 Dec 2024 12:02:40 -0500 Subject: [PATCH] feat: migrate blueprint-build-utils --- Cargo.lock | 8 ++ Cargo.toml | 1 + crates/blueprint/Cargo.toml | 4 +- crates/blueprint/build-utils/Cargo.toml | 16 ++++ crates/blueprint/build-utils/src/lib.rs | 106 ++++++++++++++++++++++++ 5 files changed, 134 insertions(+), 1 deletion(-) create mode 100644 crates/blueprint/build-utils/Cargo.toml create mode 100644 crates/blueprint/build-utils/src/lib.rs diff --git a/Cargo.lock b/Cargo.lock index 5f09b32..0bbbc5d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2328,6 +2328,13 @@ dependencies = [ "zeroize", ] +[[package]] +name = "blueprint-build-utils" +version = "0.1.0" +dependencies = [ + "gadget-std", +] + [[package]] name = "blueprint-metadata" version = "0.2.1" @@ -2346,6 +2353,7 @@ dependencies = [ name = "blueprint-util-meta" version = "0.1.0" dependencies = [ + "blueprint-build-utils", "blueprint-metadata", ] diff --git a/Cargo.toml b/Cargo.toml index 61e2fe6..21e3fd0 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -31,6 +31,7 @@ broken_intra_doc_links = "deny" # Blueprint utils blueprint-util-meta = { version = "0.1.0", path = "./crates/blueprint", default-features = false } blueprint-metadata = { version = "0.2.1", path = "./crates/blueprint/metadata", default-features = false } +blueprint-build-utils = { version = "0.1.0", path = "./crates/blueprint/build-utils", default-features = false } gadget-blueprint-serde = { version = "0.3.1", path = "./crates/blueprint/serde", default-features = false } # Crypto diff --git a/crates/blueprint/Cargo.toml b/crates/blueprint/Cargo.toml index fdbc9fc..ef09d1c 100644 --- a/crates/blueprint/Cargo.toml +++ b/crates/blueprint/Cargo.toml @@ -6,6 +6,8 @@ edition.workspace = true license.workspace = true homepage.workspace = true repository.workspace = true +publish = false [dependencies] -blueprint-metadata.workspace = true \ No newline at end of file +blueprint-metadata.workspace = true +blueprint-build-utils.workspace = true \ No newline at end of file diff --git a/crates/blueprint/build-utils/Cargo.toml b/crates/blueprint/build-utils/Cargo.toml new file mode 100644 index 0000000..f33ad4f --- /dev/null +++ b/crates/blueprint/build-utils/Cargo.toml @@ -0,0 +1,16 @@ +[package] +name = "blueprint-build-utils" +version = "0.1.0" +description = "Tangle Blueprint build utils" +authors.workspace = true +edition.workspace = true +license.workspace = true +homepage.workspace = true +repository.workspace = true +publish = false + +[dependencies] +gadget-std = { workspace = true, features = ["std"] } + +[lints] +workspace = true \ No newline at end of file diff --git a/crates/blueprint/build-utils/src/lib.rs b/crates/blueprint/build-utils/src/lib.rs new file mode 100644 index 0000000..7bb9888 --- /dev/null +++ b/crates/blueprint/build-utils/src/lib.rs @@ -0,0 +1,106 @@ +use gadget_std::env; +use gadget_std::path::PathBuf; +use gadget_std::process::Command; + +/// Build the Smart contracts at the specified directories. +/// +/// This function will automatically rerun the build if changes are detected in the `src` +/// directory within any of the directories specified. Due to this, it is recommended to +/// ensure that you only pass in directories that contain the `src` directory and won't be +/// modified by anything else in the build script (otherwise, the build will always rerun). +/// +/// # Panics +/// - If the Cargo Manifest directory is not found. +/// - If the `forge` executable is not found. +pub fn build_contracts(contract_dirs: Vec<&str>) { + for dir in contract_dirs.clone() { + let dir = format!("{}/src", dir); + println!("cargo::rerun-if-changed={dir}"); + } + + // Get the project root directory + let root = PathBuf::from(env::var("CARGO_MANIFEST_DIR").unwrap()); + + // Try to find the `forge` executable dynamically + let forge_executable = find_forge_executable(); + + for dir in contract_dirs { + let full_path = root.join(dir).canonicalize().unwrap_or_else(|_| { + println!( + "Directory not found or inaccessible: {}", + root.join(dir).display() + ); + root.join(dir) + }); + + if full_path.exists() { + let status = Command::new(&forge_executable) + .current_dir(&full_path) + .arg("build") + .status() + .expect("Failed to execute Forge build"); + + assert!( + status.success(), + "Forge build failed for directory: {}", + full_path.display() + ); + } else { + println!( + "Directory not found or does not exist: {}", + full_path.display() + ); + } + } +} + +/// Run soldeer's `update` command to populate the `dependencies` directory. +/// +/// # Panics +/// - If the Cargo Manifest directory is not found. +/// - If the `forge` executable is not found. +/// - If forge's `soldeer` is not installed. +pub fn soldeer_update() { + // Get the project root directory + let root = PathBuf::from(env::var("CARGO_MANIFEST_DIR").unwrap()); + + // Try to find the `forge` executable dynamically + let forge_executable = find_forge_executable(); + + println!("Populating dependencies directory"); + let status = Command::new(&forge_executable) + .current_dir(&root) + .args(["soldeer", "install"]) + .status() + .expect("Failed to execute 'forge soldeer update'"); + + assert!(status.success(), "'forge soldeer install' failed"); + + let status = Command::new(&forge_executable) + .current_dir(&root) + .args(["soldeer", "update", "-d"]) + .status() + .expect("Failed to execute 'forge soldeer update'"); + + assert!(status.success(), "'forge soldeer update' failed"); +} + +/// Returns a string with the path to the `forge` executable. +/// +/// # Panics +/// - If the `forge` executable is not found i.e., if Foundry is not installed. +#[must_use] +pub fn find_forge_executable() -> String { + // Try to find the `forge` executable dynamically + match Command::new("which").arg("forge").output() { + Ok(output) => { + let path = String::from_utf8_lossy(&output.stdout).trim().to_string(); + assert!( + !path.is_empty(), + "Forge executable not found. Make sure Foundry is installed." + ); + path + } + Err(e) => panic!("Failed to find `forge` executable: {e}"), + } +}