use std::path::PathBuf;
use std::sync::Arc;
use anyhow::Result;
use async_std::fs;
use futures::channel::mpsc::Sender;
use indicatif::ProgressBar;
use crate::common::{BUILDING, ERROR, SUCCESS};
use crate::config::RtcBuild;
use crate::pipelines::HtmlPipeline;
pub struct BuildSystem {
cfg: Arc<RtcBuild>,
html_pipeline: Arc<HtmlPipeline>,
progress: ProgressBar,
}
impl BuildSystem {
pub async fn new(cfg: Arc<RtcBuild>, progress: ProgressBar, ignore_chan: Option<Sender<PathBuf>>) -> Result<Self> {
let html_pipeline = Arc::new(HtmlPipeline::new(cfg.clone(), progress.clone(), ignore_chan)?);
Ok(Self {
cfg,
html_pipeline,
progress,
})
}
pub async fn build(&mut self) -> Result<()> {
self.progress.reset();
self.progress.enable_steady_tick(100);
self.progress.set_prefix(&format!("{}", BUILDING));
self.progress.set_message("starting build");
let res = self.do_build().await;
self.progress.disable_steady_tick();
self.progress.set_position(0);
match res {
Ok(_) => {
self.progress.set_prefix(&format!("{}", SUCCESS));
self.progress.finish_with_message("success");
Ok(())
}
Err(err) => {
self.progress.set_prefix(&format!("{}", ERROR));
self.progress.finish_with_message("error");
Err(err)
}
}
}
async fn do_build(&mut self) -> Result<()> {
fs::create_dir_all(self.cfg.dist.as_path()).await?;
self.html_pipeline.clone().spawn().await?;
Ok(())
}
}