use crate::workspace_discovery::{discover_icp_root_from, discover_workspace_root_from};
use std::{
io,
path::{Path, PathBuf},
};
use super::super::{CANISTERS_ROOT_RELATIVE, ROOT_CONFIG_FILE, WORKSPACE_MANIFEST_RELATIVE};
pub fn workspace_root() -> Result<PathBuf, Box<dyn std::error::Error>> {
if let Some(root) = discover_workspace_root_from(&std::env::current_dir()?) {
return Ok(root);
}
Ok(std::env::current_dir()?.canonicalize()?)
}
pub fn workspace_root_from(path: &Path) -> Result<PathBuf, Box<dyn std::error::Error>> {
if let Some(root) = discover_workspace_root_from(path) {
return Ok(root);
}
workspace_root()
}
pub fn icp_root() -> io::Result<PathBuf> {
let current_dir = std::env::current_dir()?.canonicalize()?;
if let Some(root) = discover_icp_root_from(¤t_dir) {
return Ok(root);
}
Ok(current_dir)
}
#[must_use]
pub fn config_path(workspace_root: &Path) -> PathBuf {
canisters_root(workspace_root).join(ROOT_CONFIG_FILE)
}
#[must_use]
pub fn canisters_root(workspace_root: &Path) -> PathBuf {
workspace_root.join(CANISTERS_ROOT_RELATIVE)
}
#[must_use]
pub fn workspace_manifest_path(workspace_root: &Path) -> PathBuf {
workspace_root.join(WORKSPACE_MANIFEST_RELATIVE)
}
#[must_use]
pub fn display_workspace_path(workspace_root: &Path, path: &Path) -> String {
path.strip_prefix(workspace_root)
.unwrap_or(path)
.display()
.to_string()
}