use crate::config::{CampaignDeclaration, RuntimeSettings};
use std::{
io,
path::{Path, PathBuf},
};
#[derive(Clone, Debug)]
pub struct CampaignPlan {
pub(crate) root: PathBuf,
pub(crate) declaration: CampaignDeclaration,
pub(crate) settings: RuntimeSettings,
}
impl CampaignPlan {
pub fn load(input: &Path) -> Result<Self, crate::WorkflowError> {
let root = input.canonicalize()?;
let (declaration, settings) =
crate::config::load_campaign(&root).map_err(crate::study::StudyError::from)?;
Ok(Self {
root,
declaration,
settings,
})
}
pub fn input(&self) -> &Path {
&self.root
}
pub fn studies(&self) -> impl ExactSizeIterator<Item = &str> {
self.declaration.studies.iter().map(|m| m.id.as_str())
}
pub fn continue_on_failure(&self) -> bool {
self.declaration.continue_on_failure
}
pub fn check(&self) -> io::Result<()> {
for member in &self.declaration.studies {
let runner = if member.runner.is_absolute() {
member.runner.clone()
} else {
self.root.join(&member.runner)
};
let result = std::process::Command::new(runner)
.arg("--workflow-check")
.arg(self.root.join(&member.input))
.output()?;
if !result.status.success() {
return Err(io::Error::other(format!(
"campaign member {} failed check: {}",
member.id,
String::from_utf8_lossy(&result.stderr)
)));
}
}
Ok(())
}
}