Skip to content

Commit 66b849a

Browse files
committed
fix(coprocessor): fhevm-listener, build contracts in build.rs
1 parent d86f391 commit 66b849a

File tree

2 files changed

+58
-5
lines changed

2 files changed

+58
-5
lines changed

.github/workflows/coprocessor-cargo-tests.yml

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,10 +72,6 @@ jobs:
7272
with:
7373
node-version: 20.x
7474

75-
- run: cp ./host-contracts/.env.example ./host-contracts/.env
76-
- run: npm --prefix ./host-contracts ci --include=optional
77-
- run: "cd host-contracts && npm install && npx hardhat compile"
78-
7975
- name: Run tests
8076
run: |
8177
DATABASE_URL=postgresql://postgres:postgres@localhost:5432/coprocessor cargo test --release -- --test-threads=1

coprocessor/fhevm-engine/fhevm-listener/build.rs

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,67 @@ use foundry_compilers::{
44
Project, ProjectPathsConfig,
55
};
66
use 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/artifacts");
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

965
fn 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

Comments
 (0)