@@ -4,10 +4,67 @@ 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+ let artefacts = Path :: new ( "../../../host-contracts/artefacts" ) ;
15+ if env_example. exists ( ) {
16+ // CI build
17+ if !env_dest. exists ( ) {
18+ fs:: copy ( env_example, env_dest) . expect ( "Failed to copy .env.example to .env" ) ;
19+ println ! ( "Copied .env.example to .env" ) ;
20+ }
21+ } else if artefacts. exists ( ) {
22+ // Docker build
23+ println ! ( "Assuming artefacts are up to date." ) ;
24+ } else {
25+ panic ! ( "Error: .env.example not found in contracts directory" ) ;
26+ }
27+
28+ // Change to the contracts directory for npm commands.
29+ let contracts_dir = Path :: new ( "../../../host-contracts" ) ;
30+ if !contracts_dir. exists ( ) {
31+ panic ! ( "Error: contracts directory not found" ) ;
32+ }
33+ env:: set_current_dir ( contracts_dir) . expect ( "Failed to change to contracts directory" ) ;
34+
35+ // Step 2: Run `npm ci run `npm ci --include=optional` in ../../contracts
36+ let npm_ci_status = Command :: new ( "npm" )
37+ . args ( [ "ci" , "--include=optional" ] )
38+ . status ( )
39+ . expect ( "Failed to run npm ci" ) ;
40+ if !npm_ci_status. success ( ) {
41+ panic ! ( "Error: npm ci failed" ) ;
42+ }
43+ println ! ( "Ran npm ci successfully" ) ;
44+
45+ // Step 3: Run `npm install && npx hardhat compile` in ../../contracts
46+ let npm_install_status = Command :: new ( "npm" )
47+ . arg ( "install" )
48+ . status ( )
49+ . expect ( "Failed to run npm install" ) ;
50+ if !npm_install_status. success ( ) {
51+ panic ! ( "Error: npm install failed" ) ;
52+ }
53+ println ! ( "Ran npm install successfully" ) ;
54+
55+ let hardhat_compile_status = Command :: new ( "npx" )
56+ . args ( [ "hardhat" , "compile" ] )
57+ . status ( )
58+ . expect ( "Failed to run npx hardhat compile" ) ;
59+ if !hardhat_compile_status. success ( ) {
60+ panic ! ( "Error: npx hardhat compile failed" ) ;
61+ }
62+ println ! ( "Ran npx hardhat compile successfully" ) ;
63+ }
864
965fn main ( ) {
1066 println ! ( "cargo::warning=build.rs run ..." ) ;
67+ build_contracts ( ) ;
1168 // build tests contracts
1269 let paths =
1370 ProjectPathsConfig :: hardhat ( Path :: new ( env ! ( "CARGO_MANIFEST_DIR" ) ) )
0 commit comments