From 0236e3077227c2fbc01f0f63e1b6dc1f689f224d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kuba=20P=C5=82askonka?= Date: Tue, 30 Jan 2024 12:33:05 +0100 Subject: [PATCH 1/2] Discover other than default target dirs --- src/paths.rs | 37 +++++++++++++++++++++++++++++++++++-- 1 file changed, 35 insertions(+), 2 deletions(-) diff --git a/src/paths.rs b/src/paths.rs index 37e7f81..4769a95 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,41 @@ 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 cmd = "cargo config get build.target-dir -Z unstable-options"; + let output = Command::new("sh").arg("-c").arg(cmd).output(); + + if output.is_err() { + return PathBuf::from("target"); + } + + // convert output to string + let target_dir = String::from_utf8(output.unwrap().stdout).unwrap(); + + // output is in format build.target-dir = "../target" + // convert it to PathBuf + let target_dir = target_dir.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)) From f4ecba46504816f48f109cf6b7df7d8f91be0239 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kuba=20P=C5=82askonka?= Date: Tue, 30 Jan 2024 14:32:19 +0100 Subject: [PATCH 2/2] PR fixes --- src/paths.rs | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/src/paths.rs b/src/paths.rs index 4769a95..64afa2b 100644 --- a/src/paths.rs +++ b/src/paths.rs @@ -25,19 +25,29 @@ pub fn wasm_path_in_target(contract_name: &str, project_root: PathBuf) -> PathBu } fn get_build_target_dir() -> PathBuf { - let cmd = "cargo config get build.target-dir -Z unstable-options"; - let output = Command::new("sh").arg("-c").arg(cmd).output(); + 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 target_dir = String::from_utf8(output.unwrap().stdout).unwrap(); + 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 = target_dir.split('=').collect::>(); + let target_dir = output_string.split('=').collect::>(); let target_dir = target_dir.get(1); if target_dir.is_none() {