From 24040302f5c11f748d36aa5bea11eb39f7c9dd07 Mon Sep 17 00:00:00 2001 From: Jawad Tariq Date: Wed, 13 Mar 2024 18:00:49 -0400 Subject: [PATCH] feat: test build.rs Signed-off-by: Jawad Tariq --- crates/topos/build.rs | 53 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) diff --git a/crates/topos/build.rs b/crates/topos/build.rs index 980a55a55..081b824a3 100644 --- a/crates/topos/build.rs +++ b/crates/topos/build.rs @@ -1,5 +1,6 @@ use std::process::Command; +const CONTRACTS_PATH: &str = "./contracts"; const DEFAULT_VERSION: &str = "detached"; fn main() { @@ -20,4 +21,56 @@ fn main() { println!("cargo:rustc-env=TOPOS_VERSION={topos_version}"); } + + compile_contracts(); +} + +fn compile_contracts() { + if !CONTRACTS_PATH.is_empty() { + if let Err(err) = std::env::set_current_dir(CONTRACTS_PATH) { + eprintln!("Error changing to subdirectory: {}", err); + return; + } + } + + let install_result = Command::new("npm") + .arg("ci") + .status(); + + match install_result { + Ok(status) => { + if status.success() { + println!("npm dependencies installed successfully in {}!", CONTRACTS_PATH); + } else { + eprintln!("Error installing npm dependencies in {}: {:?}", CONTRACTS_PATH, status); + } + } + Err(err) => { + eprintln!("Error executing npm ci: {}", err); + } + } + + let compile_result = Command::new("npm") + .arg("run") + .arg("build") + .status(); + + match compile_result { + Ok(status) => { + if status.success() { + println!("Artifacts compiled successfully in {}!", CONTRACTS_PATH); + } else { + eprintln!("Error compiling artifacts in {}: {:?}", CONTRACTS_PATH, status); + } + } + Err(err) => { + eprintln!("Error executing npx hardhat compile: {}", err); + } + } + + if let Err(err) = std::env::set_current_dir("..") { + eprintln!("Error changing back to the original directory: {}", err); + } + + println!("cargo:rerun-if-changed={}", CONTRACTS_PATH); }