Skip to content

Commit 6ac33a8

Browse files
committed
feat(build): add target dir handling with cargo metadata
1 parent 1df8ff2 commit 6ac33a8

File tree

3 files changed

+42
-2
lines changed

3 files changed

+42
-2
lines changed

Cargo.lock

+7
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ clap = {version = "4.5.20", features = ["derive", "env", "wrap_help"]}
1717
dialoguer = "0.11.0"
1818
dotenvy = "0.15"
1919
ethers = "2.0.14"
20+
json = "0.12.4"
2021
serde = {version = "1.0.214", features = ["derive"]}
2122
serde_json = "1.0.132"
2223
tempfile = "3.14.0"

src/commands/rust/build.rs

+34-2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use std::{
88
process::{Command, Stdio},
99
time::Instant,
1010
};
11+
use std::str::from_utf8;
1112

1213
#[derive(Args)]
1314
pub struct BuildArgs {
@@ -82,11 +83,38 @@ fn build_project(path: &PathBuf, release: bool, verbose: bool, target_dir: Optio
8283
ensure_wasm_target()?;
8384

8485
println!("📦 Running cargo build...");
85-
run_cargo_build(path, release, verbose, target_dir)?;
86+
run_cargo_build(path, release, verbose, target_dir.clone())?;
87+
88+
let target_dir = if target_dir.is_none() {
89+
let result = Command::new("cargo")
90+
.arg("metadata")
91+
.arg("--no-deps")
92+
.output()
93+
.map_err(|e| {
94+
if verbose {
95+
println!("Failed to get target directory: {:?}", e);
96+
}
97+
Error::Build("Failed to get target directory".to_string())
98+
})?;
99+
let utf8_string = from_utf8(&result.stdout).map_err(|e| {
100+
if verbose {
101+
println!("Failed to decode UTF-8 output: {:?}", e);
102+
}
103+
Error::Build("Failed to get target directory".to_string())
104+
})?;
105+
let json_value = json::parse(utf8_string).unwrap();
106+
json_value["target_directory"].to_string()
107+
} else {
108+
target_dir.unwrap()
109+
};
110+
111+
if verbose {
112+
println!("Checking target dir: {}", target_dir)
113+
}
86114

87115
// Define the expected output location
88116
let build_mode = if release { "release" } else { "debug" };
89-
let target_dir = path.join("target/wasm32-unknown-unknown").join(build_mode);
117+
let target_dir = path.join(target_dir).join("wasm32-unknown-unknown").join(build_mode);
90118

91119
// Locate the generated .wasm file
92120
let wasm_file = target_dir
@@ -142,6 +170,10 @@ fn run_cargo_build(path: &PathBuf, release: bool, verbose: bool, target_dir: Opt
142170
build_args.push("--release".to_string());
143171
}
144172

173+
if verbose {
174+
println!("Running command: {}", build_args.join(" "));
175+
}
176+
145177
let mut cmd = Command::new("cargo")
146178
.args(&build_args)
147179
.env(

0 commit comments

Comments
 (0)