use anyhow::Result;
use notify::{Event, RecursiveMode, Watcher};
use std::path::{Path, PathBuf};
use std::sync::Arc;
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::mpsc;
use std::time::{Duration, Instant};
use crate::builder::Builder;
use crate::cli::BuildOptions;
use crate::color;
fn should_ignore(path: &Path, output_dir: &str) -> bool {
if path.components().any(|c| c.as_os_str() == ".rsconstruct") {
return true;
}
if path.components().any(|c| c.as_os_str() == output_dir) {
return true;
}
if let Some(name) = path.file_name().and_then(|n| n.to_str()) {
if name.starts_with('.') && name.ends_with(".swp") {
return true;
}
if name.ends_with('~') {
return true;
}
if name.starts_with('#') && name.ends_with('#') {
return true;
}
if name.ends_with(".tmp") {
return true;
}
}
false
}
fn register_watches(
watcher: &mut impl Watcher,
paths: &[PathBuf],
) {
for path in paths {
let mode = if path.is_dir() {
RecursiveMode::Recursive
} else {
RecursiveMode::NonRecursive
};
if let Err(e) = watcher.watch(path, mode) {
eprintln!("Warning: could not watch {}: {}", path.display(), e);
}
}
}
pub fn watch(ctx: &crate::build_context::BuildContext, opts: &BuildOptions, interrupted: Arc<AtomicBool>) -> Result<()> {
let mut builder = Builder::new_with_overrides(&opts.iset, &opts.pset)?;
let mut watch_paths = builder.watch_paths();
let mut output_dir = builder.output_dir().to_string();
let (tx, rx) = mpsc::channel::<notify::Result<Event>>();
let mut watcher = notify::recommended_watcher(tx)?;
register_watches(&mut watcher, &watch_paths);
println!("{}", color::bold("Running initial build..."));
if let Err(e) = builder.build(ctx, opts, Arc::clone(&interrupted), Vec::new()) {
println!("{}", color::red(&format!("Initial build error: {e}")));
}
drop(builder);
println!("{}", color::green("Watching for changes... (Ctrl+C to stop)"));
let debounce_duration = Duration::from_millis(200);
let poll_interval = Duration::from_millis(500);
loop {
let got_event = loop {
if interrupted.load(Ordering::SeqCst) {
return Ok(());
}
match rx.recv_timeout(poll_interval) {
Ok(Ok(event)) => {
let all_ignored = event.paths.iter().all(|p| should_ignore(p, &output_dir));
if all_ignored {
continue;
}
break true;
}
Ok(Err(e)) => {
println!("{}", color::red(&format!("Watch error: {e}")));
continue;
}
Err(mpsc::RecvTimeoutError::Timeout) => continue,
Err(mpsc::RecvTimeoutError::Disconnected) => break false,
}
};
if !got_event {
break;
}
let deadline = Instant::now() + debounce_duration;
loop {
let remaining = deadline.saturating_duration_since(Instant::now());
if remaining.is_zero() {
break;
}
match rx.recv_timeout(remaining) {
Ok(_) => {}
Err(mpsc::RecvTimeoutError::Timeout) => break,
Err(mpsc::RecvTimeoutError::Disconnected) => break,
}
}
println!();
println!("{}", color::bold("Change detected, rebuilding..."));
match Builder::new_with_overrides(&opts.iset, &opts.pset) {
Err(e) => {
println!("{}", color::red(&format!("Config error: {e}")));
}
Ok(mut builder) => {
let new_paths = builder.watch_paths();
output_dir = builder.output_dir().to_string();
if let Err(e) = builder.build(ctx, opts, Arc::clone(&interrupted), Vec::new()) {
println!("{}", color::red(&format!("Build error: {e}")));
}
for path in &new_paths {
if !watch_paths.contains(path) {
register_watches(&mut watcher, std::slice::from_ref(path));
}
}
for path in &watch_paths {
if !new_paths.contains(path) {
let _ = watcher.unwatch(path);
}
}
watch_paths = new_paths;
}
}
println!("{}", color::green("Watching for changes... (Ctrl+C to stop)"));
}
Ok(())
}