1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
use std::{ffi::OsStr, path::PathBuf};

use anyhow::bail;
use serde::{Deserialize, Serialize};

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct WorkspaceSettings {
    /// must be an absolute path
    pub(crate) workspace_root: PathBuf,
    /// absolute or relative to workspace root
    pub(crate) config_path: PathBuf,
    /// absolute or relative to workspace root
    pub(crate) target_dir: PathBuf,
    /// absolute or relative to workspace root
    pub(crate) deployment_dir: PathBuf,
    /// absolute or relative to workspace root
    pub(crate) artifacts_dir: PathBuf,
}

impl WorkspaceSettings {
    pub fn new<T: ?Sized + AsRef<OsStr>>(workspace_root: &T) -> anyhow::Result<Self> {
        let workspace_root = PathBuf::from(workspace_root);
        if workspace_root.is_relative() {
            bail!("workspace root must be an absolute path")
        } else if workspace_root.is_file() {
            bail!("workspace root must be a directory")
        }
        Ok(Self {
            workspace_root: workspace_root.clone(),
            config_path: workspace_root.join(".wasm-deploy/config.json"),
            target_dir: workspace_root.join("target"),
            deployment_dir: workspace_root.join("deployment"),
            artifacts_dir: workspace_root.join("artifacts"),
        })
    }

    /// Default path is `.wasm-deploy/config.json`
    pub fn set_config_path<T: ?Sized + AsRef<OsStr>>(
        mut self,
        config_path: &T,
    ) -> anyhow::Result<Self> {
        let mut config_path = PathBuf::from(config_path);
        if config_path.is_dir() {
            config_path = config_path.join("config.json");
        }
        self.config_path = config_path;
        Ok(self)
    }

    /// Default path is `target`
    pub fn set_build_dir<T: ?Sized + AsRef<OsStr>>(
        mut self,
        target_dir: &T,
    ) -> anyhow::Result<Self> {
        let target_dir = PathBuf::from(target_dir);
        if !target_dir.is_dir() {
            bail!("target dir must be a directory")
        }
        self.target_dir = target_dir;
        Ok(self)
    }

    /// Default path is `deployment`
    pub fn set_deployment_dir<T: ?Sized + AsRef<OsStr>>(
        mut self,
        deployment_dir: &T,
    ) -> anyhow::Result<Self> {
        let deployment_dir = PathBuf::from(deployment_dir);
        if !deployment_dir.is_dir() {
            bail!("deployment dir must be a directory")
        }
        self.deployment_dir = deployment_dir;
        Ok(self)
    }

    /// Default path is `artifacts`
    pub fn set_artifacts_dir<T: ?Sized + AsRef<OsStr>>(
        mut self,
        artifacts_dir: &T,
    ) -> anyhow::Result<Self> {
        let artifacts_dir = PathBuf::from(artifacts_dir);
        if !artifacts_dir.is_dir() {
            bail!("artifacts dir must be a directory")
        }
        self.artifacts_dir = artifacts_dir;
        Ok(self)
    }
}