@@ -4,10 +4,60 @@ use foundry_compilers::{
44 Project , ProjectPathsConfig ,
55} ;
66use semver:: Version ;
7- use std:: path:: Path ;
7+ use std:: { env, fs, path:: Path , process:: Command } ;
8+
9+ fn build_contracts ( ) {
10+ println ! ( "cargo:rerun-if-changed=../../../host-contracts" ) ;
11+ // Step 1: Copy ../../contracts/.env.example to ../../contracts/.env
12+ let env_example = Path :: new ( "../../../host-contracts/.env.example" ) ;
13+ let env_dest = Path :: new ( "../../../host-contracts/.env" ) ;
14+ if !env_dest. exists ( ) && env_example. exists ( ) {
15+ fs:: copy ( env_example, env_dest) . expect ( "Failed to copy .env.example to .env" ) ;
16+ println ! ( "Copied .env.example to .env" ) ;
17+ } else {
18+ panic ! ( "Error: .env.example not found in contracts directory" ) ;
19+ }
20+
21+ // Change to the contracts directory for npm commands.
22+ let contracts_dir = Path :: new ( "../../../host-contracts" ) ;
23+ if !contracts_dir. exists ( ) {
24+ panic ! ( "Error: contracts directory not found" ) ;
25+ }
26+ env:: set_current_dir ( contracts_dir) . expect ( "Failed to change to contracts directory" ) ;
27+
28+ // Step 2: Run `npm ci run `npm ci --include=optional` in ../../contracts
29+ let npm_ci_status = Command :: new ( "npm" )
30+ . args ( [ "ci" , "--include=optional" ] )
31+ . status ( )
32+ . expect ( "Failed to run npm ci" ) ;
33+ if !npm_ci_status. success ( ) {
34+ panic ! ( "Error: npm ci failed" ) ;
35+ }
36+ println ! ( "Ran npm ci successfully" ) ;
37+
38+ // Step 3: Run `npm install && npx hardhat compile` in ../../contracts
39+ let npm_install_status = Command :: new ( "npm" )
40+ . arg ( "install" )
41+ . status ( )
42+ . expect ( "Failed to run npm install" ) ;
43+ if !npm_install_status. success ( ) {
44+ panic ! ( "Error: npm install failed" ) ;
45+ }
46+ println ! ( "Ran npm install successfully" ) ;
47+
48+ let hardhat_compile_status = Command :: new ( "npx" )
49+ . args ( [ "hardhat" , "compile" ] )
50+ . status ( )
51+ . expect ( "Failed to run npx hardhat compile" ) ;
52+ if !hardhat_compile_status. success ( ) {
53+ panic ! ( "Error: npx hardhat compile failed" ) ;
54+ }
55+ println ! ( "Ran npx hardhat compile successfully" ) ;
56+ }
857
958fn main ( ) {
1059 println ! ( "cargo::warning=build.rs run ..." ) ;
60+ build_contracts ( ) ;
1161 // build tests contracts
1262 let paths =
1363 ProjectPathsConfig :: hardhat ( Path :: new ( env ! ( "CARGO_MANIFEST_DIR" ) ) )
0 commit comments