canic_host/release_set/
paths.rs1use crate::workspace_discovery::{
2 discover_canister_manifest_from_metadata, discover_icp_root_from, discover_workspace_root_from,
3 normalize_workspace_path,
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
16pub 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
44pub 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(¤t_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#[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#[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 if let Some(manifest_path) = discover_canister_manifest_from_metadata(workspace_root, "root")
91 && let Some(parent) = manifest_path.parent().and_then(Path::parent)
92 {
93 return parent.to_path_buf();
94 }
95
96 workspace_root.join(CANISTERS_ROOT_RELATIVE)
97}
98
99#[must_use]
101pub fn root_manifest_path(workspace_root: &Path) -> PathBuf {
102 std::env::var_os("CANIC_ROOT_MANIFEST_PATH").map_or_else(
103 || {
104 discover_canister_manifest_from_metadata(workspace_root, "root").unwrap_or_else(|| {
105 canisters_root(workspace_root)
106 .join("root")
107 .join("Cargo.toml")
108 })
109 },
110 |path| normalize_workspace_path(workspace_root, PathBuf::from(path)),
111 )
112}
113
114#[must_use]
116pub fn canister_manifest_path(workspace_root: &Path, canister_name: &str) -> PathBuf {
117 discover_canister_manifest_from_metadata(workspace_root, canister_name).unwrap_or_else(|| {
118 canisters_root(workspace_root)
119 .join(canister_name)
120 .join("Cargo.toml")
121 })
122}
123
124#[must_use]
126pub fn workspace_manifest_path(workspace_root: &Path) -> PathBuf {
127 std::env::var_os("CANIC_WORKSPACE_MANIFEST_PATH").map_or_else(
128 || workspace_root.join(WORKSPACE_MANIFEST_RELATIVE),
129 |path| normalize_workspace_path(workspace_root, PathBuf::from(path)),
130 )
131}
132
133pub fn resolve_artifact_root(
135 icp_root: &Path,
136 network: &str,
137) -> Result<PathBuf, Box<dyn std::error::Error>> {
138 let preferred = icp_root.join(".icp").join(network).join("canisters");
139 if preferred.is_dir() {
140 return Ok(preferred);
141 }
142
143 let local_artifact_root = icp_root.join(".icp/local/canisters");
144 if local_artifact_root.is_dir() {
145 return Ok(local_artifact_root);
146 }
147
148 Err(format!(
149 "missing built ICP artifacts under {} or {}",
150 preferred.display(),
151 local_artifact_root.display()
152 )
153 .into())
154}
155
156pub fn root_release_set_manifest_path(
158 artifact_root: &Path,
159) -> Result<PathBuf, Box<dyn std::error::Error>> {
160 let manifest_path = artifact_root
161 .join("root")
162 .join(ROOT_RELEASE_SET_MANIFEST_FILE);
163
164 if let Some(parent) = manifest_path.parent() {
165 fs::create_dir_all(parent)?;
166 }
167
168 Ok(manifest_path)
169}
170
171pub fn load_root_package_version(
173 root_manifest_path: &Path,
174 workspace_manifest_path: &Path,
175) -> Result<String, Box<dyn std::error::Error>> {
176 let manifest_source = fs::read_to_string(root_manifest_path)?;
177 let manifest = toml::from_str::<TomlValue>(&manifest_source)?;
178 let version_value = manifest
179 .get("package")
180 .and_then(TomlValue::as_table)
181 .and_then(|package| package.get("version"))
182 .ok_or_else(|| {
183 format!(
184 "missing package.version in {}",
185 root_manifest_path.display()
186 )
187 })?;
188
189 if let Some(version) = version_value.as_str() {
190 return Ok(version.to_string());
191 }
192
193 if version_value
194 .as_table()
195 .and_then(|value| value.get("workspace"))
196 .and_then(TomlValue::as_bool)
197 == Some(true)
198 {
199 return load_workspace_package_version(workspace_manifest_path);
200 }
201
202 Err(format!(
203 "unsupported package.version format in {}",
204 root_manifest_path.display()
205 )
206 .into())
207}
208
209pub fn load_workspace_package_version(
211 workspace_manifest_path: &Path,
212) -> Result<String, Box<dyn std::error::Error>> {
213 let manifest_source = fs::read_to_string(workspace_manifest_path)?;
214 let manifest = toml::from_str::<TomlValue>(&manifest_source)?;
215 let version = manifest
216 .get("workspace")
217 .and_then(TomlValue::as_table)
218 .and_then(|workspace| workspace.get("package"))
219 .and_then(TomlValue::as_table)
220 .and_then(|package| package.get("version"))
221 .and_then(TomlValue::as_str)
222 .ok_or_else(|| {
223 format!(
224 "missing workspace.package.version in {}",
225 workspace_manifest_path.display()
226 )
227 })?;
228
229 Ok(version.to_string())
230}