Skip to main content

canic_host/release_set/
paths.rs

1use crate::workspace_discovery::{
2    discover_icp_root_from, discover_workspace_root_from, normalize_workspace_path,
3    resolve_canister_manifest_from_metadata_under,
4};
5use std::{
6    fs,
7    path::{Path, PathBuf},
8};
9use toml::Value as TomlValue;
10
11use super::{
12    CANISTERS_ROOT_RELATIVE, ROOT_CONFIG_FILE, ROOT_RELEASE_SET_MANIFEST_FILE,
13    WORKSPACE_MANIFEST_RELATIVE,
14};
15
16// Resolve the downstream Cargo workspace root from the current directory,
17// config hints, or an explicit override.
18pub fn workspace_root() -> Result<PathBuf, Box<dyn std::error::Error>> {
19    if let Ok(path) = std::env::var("CANIC_WORKSPACE_ROOT") {
20        return Ok(PathBuf::from(path).canonicalize()?);
21    }
22
23    if let Some(root) = std::env::var_os("CANIC_WORKSPACE_MANIFEST_PATH")
24        .map(PathBuf::from)
25        .and_then(|path| discover_workspace_root_from(&path))
26    {
27        return Ok(root);
28    }
29
30    if let Some(root) = std::env::var_os("CANIC_CONFIG_PATH")
31        .map(PathBuf::from)
32        .and_then(|path| discover_workspace_root_from(&path))
33    {
34        return Ok(root);
35    }
36
37    if let Some(root) = discover_workspace_root_from(&std::env::current_dir()?) {
38        return Ok(root);
39    }
40
41    Ok(std::env::current_dir()?.canonicalize()?)
42}
43
44// Resolve the downstream ICP CLI/project root from the current directory or an
45// explicit override.
46pub fn icp_root() -> Result<PathBuf, Box<dyn std::error::Error>> {
47    if let Ok(path) = std::env::var("CANIC_ICP_ROOT") {
48        return Ok(PathBuf::from(path).canonicalize()?);
49    }
50
51    let current_dir = std::env::current_dir()?.canonicalize()?;
52    if let Some(root) = discover_icp_root_from(&current_dir) {
53        return Ok(root);
54    }
55
56    if let Ok(path) = std::env::var("CANIC_WORKSPACE_ROOT") {
57        let workspace_root = PathBuf::from(path).canonicalize()?;
58        if let Some(root) = discover_icp_root_from(&workspace_root) {
59            return Ok(root);
60        }
61        return Ok(workspace_root);
62    }
63
64    Ok(current_dir)
65}
66
67// Resolve the downstream Canic config path.
68#[must_use]
69pub fn config_path(workspace_root: &Path) -> PathBuf {
70    std::env::var_os("CANIC_CONFIG_PATH").map_or_else(
71        || canisters_root(workspace_root).join(ROOT_CONFIG_FILE),
72        |path| normalize_workspace_path(workspace_root, PathBuf::from(path)),
73    )
74}
75
76// Resolve the downstream canister-manifest root.
77#[must_use]
78pub fn canisters_root(workspace_root: &Path) -> PathBuf {
79    if let Some(path) = std::env::var_os("CANIC_CANISTERS_ROOT") {
80        return normalize_workspace_path(workspace_root, PathBuf::from(path));
81    }
82
83    if let Some(path) = std::env::var_os("CANIC_CONFIG_PATH") {
84        let config_path = normalize_workspace_path(workspace_root, PathBuf::from(path));
85        if let Some(parent) = config_path.parent() {
86            return parent.to_path_buf();
87        }
88    }
89
90    workspace_root.join(CANISTERS_ROOT_RELATIVE)
91}
92
93// Resolve the downstream root canister manifest path.
94pub fn root_manifest_path(workspace_root: &Path) -> Result<PathBuf, Box<dyn std::error::Error>> {
95    if let Some(path) = std::env::var_os("CANIC_ROOT_MANIFEST_PATH") {
96        return Ok(normalize_workspace_path(
97            workspace_root,
98            PathBuf::from(path),
99        ));
100    }
101
102    canister_manifest_path(workspace_root, "root")
103}
104
105// Resolve the downstream manifest path for one visible canister role.
106pub fn canister_manifest_path(
107    workspace_root: &Path,
108    canister_name: &str,
109) -> Result<PathBuf, Box<dyn std::error::Error>> {
110    let root = canisters_root(workspace_root);
111    resolve_canister_manifest_from_metadata_under(workspace_root, canister_name, &root)
112        .map_err(|err| {
113            format!(
114                "{err}; selected canister root is {}. Set CANIC_CANISTERS_ROOT or CANIC_CONFIG_PATH so it points at the fleet canister directory.",
115                root.display()
116            )
117            .into()
118        })
119}
120
121// Resolve the downstream workspace manifest path.
122#[must_use]
123pub fn workspace_manifest_path(workspace_root: &Path) -> PathBuf {
124    std::env::var_os("CANIC_WORKSPACE_MANIFEST_PATH").map_or_else(
125        || workspace_root.join(WORKSPACE_MANIFEST_RELATIVE),
126        |path| normalize_workspace_path(workspace_root, PathBuf::from(path)),
127    )
128}
129
130// Render a path relative to the workspace root when possible.
131#[must_use]
132pub fn display_workspace_path(workspace_root: &Path, path: &Path) -> String {
133    path.strip_prefix(workspace_root)
134        .unwrap_or(path)
135        .display()
136        .to_string()
137}
138
139// Resolve the built artifact directory for the selected ICP environment.
140pub fn resolve_artifact_root(
141    icp_root: &Path,
142    network: &str,
143) -> Result<PathBuf, Box<dyn std::error::Error>> {
144    let preferred = icp_root.join(".icp").join(network).join("canisters");
145    if preferred.is_dir() {
146        return Ok(preferred);
147    }
148
149    let local_artifact_root = icp_root.join(".icp/local/canisters");
150    if local_artifact_root.is_dir() {
151        return Ok(local_artifact_root);
152    }
153
154    Err(format!(
155        "missing built ICP artifacts under {} or {}",
156        preferred.display(),
157        local_artifact_root.display()
158    )
159    .into())
160}
161
162// Return the canonical manifest path for the staged root release set.
163pub fn root_release_set_manifest_path(
164    artifact_root: &Path,
165) -> Result<PathBuf, Box<dyn std::error::Error>> {
166    let manifest_path = artifact_root
167        .join("root")
168        .join(ROOT_RELEASE_SET_MANIFEST_FILE);
169
170    if let Some(parent) = manifest_path.parent() {
171        fs::create_dir_all(parent)?;
172    }
173
174    Ok(manifest_path)
175}
176
177// Read the reference root canister version so staged release versions match the install.
178pub fn load_root_package_version(
179    root_manifest_path: &Path,
180    workspace_manifest_path: &Path,
181) -> Result<String, Box<dyn std::error::Error>> {
182    let manifest_source = fs::read_to_string(root_manifest_path)?;
183    let manifest = toml::from_str::<TomlValue>(&manifest_source)?;
184    let version_value = manifest
185        .get("package")
186        .and_then(TomlValue::as_table)
187        .and_then(|package| package.get("version"))
188        .ok_or_else(|| {
189            format!(
190                "missing package.version in {}",
191                root_manifest_path.display()
192            )
193        })?;
194
195    if let Some(version) = version_value.as_str() {
196        return Ok(version.to_string());
197    }
198
199    if version_value
200        .as_table()
201        .and_then(|value| value.get("workspace"))
202        .and_then(TomlValue::as_bool)
203        == Some(true)
204    {
205        return load_workspace_package_version(workspace_manifest_path);
206    }
207
208    Err(format!(
209        "unsupported package.version format in {}",
210        root_manifest_path.display()
211    )
212    .into())
213}
214
215// Resolve the shared workspace package version used by reference canisters.
216pub fn load_workspace_package_version(
217    workspace_manifest_path: &Path,
218) -> Result<String, Box<dyn std::error::Error>> {
219    let manifest_source = fs::read_to_string(workspace_manifest_path)?;
220    let manifest = toml::from_str::<TomlValue>(&manifest_source)?;
221    let version = manifest
222        .get("workspace")
223        .and_then(TomlValue::as_table)
224        .and_then(|workspace| workspace.get("package"))
225        .and_then(TomlValue::as_table)
226        .and_then(|package| package.get("version"))
227        .and_then(TomlValue::as_str)
228        .ok_or_else(|| {
229            format!(
230                "missing workspace.package.version in {}",
231                workspace_manifest_path.display()
232            )
233        })?;
234
235    Ok(version.to_string())
236}