Skip to content

Commit

Permalink
Discover other than default target dirs (#69)
Browse files Browse the repository at this point in the history
* Discover other than default target dirs
  • Loading branch information
kubaplas authored Jan 30, 2024
1 parent 6a82c98 commit 51fe151
Showing 1 changed file with 45 additions and 2 deletions.
47 changes: 45 additions & 2 deletions src/paths.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
//! Paths utils.
use std::path::{Path, PathBuf};
use std::{
path::{Path, PathBuf},
process::Command,
};

use convert_case::{Boundary, Case, Casing};

Expand All @@ -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<String> = "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::<Vec<&str>>();
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))
Expand Down

0 comments on commit 51fe151

Please sign in to comment.