use crate::config::{
rt::{BuildOptions, RtcBuild, RtcBuilder},
Configuration, Watch,
};
use anyhow::anyhow;
use std::{ops::Deref, path::PathBuf, sync::Arc, time::Duration};
#[derive(Clone, Debug)]
pub struct RtcWatch {
pub build: Arc<RtcBuild>,
pub paths: Vec<PathBuf>,
pub ignored_paths: Vec<PathBuf>,
pub poll: Option<Duration>,
pub enable_cooldown: bool,
pub clear_screen: bool,
pub no_error_reporting: bool,
}
impl Deref for RtcWatch {
type Target = RtcBuild;
fn deref(&self) -> &Self::Target {
&self.build
}
}
#[derive(Clone, Debug)]
pub struct WatchOptions {
pub build: BuildOptions,
pub poll: Option<Duration>,
pub enable_cooldown: bool,
pub clear_screen: bool,
pub no_error_reporting: bool,
}
impl RtcWatch {
pub(crate) fn new(config: Configuration, opts: WatchOptions) -> anyhow::Result<Self> {
let WatchOptions {
build: build_opts,
poll,
enable_cooldown,
clear_screen,
no_error_reporting,
} = opts;
let Watch { watch, ignore } = config.watch.clone();
let build = RtcBuild::new(config, build_opts)?;
tracing::debug!("Disable error reporting: {no_error_reporting}");
let mut paths = vec![];
for path in watch {
let path = build.working_directory.join(path);
let canon_path = path.canonicalize().map_err(|_| {
anyhow!(
"error taking the canonical path to the watch path: {:?}",
path
)
})?;
paths.push(canon_path);
}
if paths.is_empty() {
paths.push(build.target_parent.clone());
}
let mut ignored_paths = ignore
.into_iter()
.map(|path| {
let path = build.working_directory.join(path);
path.canonicalize().map_err(|_| {
anyhow!(
"error taking the canonical path to the watch ignore path: {:?}",
path
)
})
})
.collect::<Result<Vec<_>, _>>()?;
ignored_paths.push(build.final_dist.clone());
Ok(Self {
build: Arc::new(build),
paths,
ignored_paths,
poll,
enable_cooldown,
clear_screen,
no_error_reporting,
})
}
}
impl RtcBuilder for RtcWatch {
type Options = WatchOptions;
async fn build(configuration: Configuration, options: Self::Options) -> anyhow::Result<Self> {
Self::new(configuration, options)
}
}