diff --git a/src/paths.rs b/src/paths.rs index 37e7f81..64afa2b 100644 --- a/src/paths.rs +++ b/src/paths.rs @@ -1,6 +1,9 @@ //! Paths utils. -use std::path::{Path, PathBuf}; +use std::{ + path::{Path, PathBuf}, + process::Command, +}; use convert_case::{Boundary, Case, Casing}; @@ -11,11 +14,51 @@ pub fn wasm_file_name(contract_name: &str) -> PathBuf { /// Returns *.wasm file path in target directory. pub fn wasm_path_in_target(contract_name: &str, project_root: PathBuf) -> PathBuf { + // extract target dir + let target_dir = get_build_target_dir(); project_root - .join("target/wasm32-unknown-unknown/release") + .join(format!( + "{}/wasm32-unknown-unknown/release", + target_dir.display() + )) .join(wasm_file_name(contract_name)) } +fn get_build_target_dir() -> PathBuf { + let args: Vec = "config get build.target-dir -Z unstable-options" + .to_string() + .split(' ') + .map(|s| s.to_string()) + .collect(); + let output = Command::new("cargo").args(args).output(); + + if output.is_err() { + return PathBuf::from("target"); + } + + // convert output to string + let output_string = String::from_utf8(output.unwrap().stdout); + + if output_string.is_err() { + return PathBuf::from("target"); + } + + let output_string = output_string.unwrap(); + + // output is in format build.target-dir = "../target" + // convert it to PathBuf + let target_dir = output_string.split('=').collect::>(); + let target_dir = target_dir.get(1); + + if target_dir.is_none() { + return PathBuf::from("target"); + } + + let target = target_dir.unwrap().trim().to_string().replace('\"', ""); + + PathBuf::from(target) +} + /// Returns *.wasm file path in wasm directory. pub fn wasm_path_in_wasm_dir(contract_name: &str, project_root: &Path) -> PathBuf { wasm_dir(project_root).join(wasm_file_name(contract_name))