use crate::workspace_discovery::{
WorkspaceDiscoveryError, discover_icp_root_from, discover_workspace_root_from,
};
use std::path::{Path, PathBuf};
use super::super::{CANISTERS_ROOT_RELATIVE, ROOT_CONFIG_FILE, WORKSPACE_MANIFEST_RELATIVE};
pub fn workspace_root() -> Result<PathBuf, WorkspaceDiscoveryError> {
let current_dir = std::env::current_dir().map_err(WorkspaceDiscoveryError::CurrentDirectory)?;
if let Some(root) = discover_workspace_root_from(¤t_dir)? {
return Ok(root);
}
current_dir
.canonicalize()
.map_err(|source| WorkspaceDiscoveryError::Canonicalize {
path: current_dir,
source,
})
}
pub fn icp_root() -> Result<PathBuf, WorkspaceDiscoveryError> {
let current_dir = std::env::current_dir().map_err(WorkspaceDiscoveryError::CurrentDirectory)?;
if let Some(root) = discover_icp_root_from(¤t_dir)? {
return Ok(root);
}
current_dir
.canonicalize()
.map_err(|source| WorkspaceDiscoveryError::Canonicalize {
path: current_dir,
source,
})
}
#[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()
}