1use std::path::{Path, PathBuf};
2
3use loam_build::get_target_dir;
4
5#[derive(thiserror::Error, Debug)]
6pub enum Error {
7 #[error(transparent)]
8 LoamBuild(#[from] loam_build::Error),
9 #[error("Missing contract_id for {0}")]
10 MissingContractId(String),
11}
12
13pub fn wasm_location(name: &str, out_dir: Option<&Path>) -> Result<PathBuf, Error> {
14 let out_dir = if let Some(out_dir) = out_dir {
15 out_dir.to_path_buf()
16 } else {
17 target_dir()?
18 };
19 let mut out_file = out_dir.join(name).join("index");
20 out_file.set_extension("wasm");
21 Ok(out_file)
22}
23
24pub fn contract_id(name: &str, out_dir: Option<&Path>) -> Result<String, Error> {
25 let wasm = wasm_location(name, out_dir)?;
26 let parent = wasm
27 .parent()
28 .ok_or_else(|| Error::MissingContractId(name.to_owned()))?;
29 let id_file = parent.join("contract_id.txt");
30 std::fs::read_to_string(id_file).map_err(|_| Error::MissingContractId(name.to_owned()))
31}
32
33fn manifest() -> PathBuf {
34 std::path::PathBuf::from(std::env::var("CARGO_MANIFEST_DIR").unwrap_or_else(|_| ".".to_owned()))
35 .join("Cargo.toml")
36}
37
38pub fn target_dir() -> Result<PathBuf, Error> {
39 let mut target_dir = get_target_dir(&manifest()).map_err(loam_build::Error::Metadata)?;
40 target_dir.pop();
41 Ok(target_dir.join("smartdeploy"))
42}