use crate::config::Config;
use crate::errors::{OrandaError, Result};
use camino::Utf8PathBuf;
#[derive(Debug, Clone)]
pub struct WorkspaceData {
pub root_path: Utf8PathBuf,
pub slug: String,
pub path: Utf8PathBuf,
pub config: Config,
}
pub fn from_config(
workspace_config: &Config,
root_path: &Utf8PathBuf,
workspace_config_path: &Utf8PathBuf,
) -> Result<Vec<WorkspaceData>> {
let mut vec = Vec::new();
for member in workspace_config.workspace.members.clone() {
if !member.path.exists() {
return Err(OrandaError::FileNotFound {
filedesc: "workspace member".to_string(),
path: member.path.display().to_string(),
});
}
let path = Utf8PathBuf::from(member.path.display().to_string()).canonicalize_utf8()?;
let mut config_path = path.clone();
config_path.push("oranda.json");
let mut config = Config::build_workspace_member(
&config_path,
workspace_config_path,
&path,
&member,
Some(member.slug.clone()),
)?;
config.build.path_prefix =
if let Some(path_prefix) = workspace_config.build.path_prefix.as_ref() {
Some(format!("{}/{}", path_prefix, &member.slug))
} else {
Some(member.slug.to_string())
};
config.build.dist_dir = format!(
"{}/{}/{}",
root_path, workspace_config.build.dist_dir, &member.slug
);
vec.push(WorkspaceData {
root_path: root_path.clone(),
slug: member.slug.clone(),
path,
config,
});
}
Ok(vec)
}