watchexec/src/main.rs

211 lines
5.1 KiB
Rust
Raw Normal View History

#[macro_use]
extern crate clap;
extern crate globset;
2016-10-13 23:59:46 +02:00
extern crate env_logger;
extern crate libc;
#[macro_use]
extern crate log;
#[macro_use]
extern crate lazy_static;
2016-09-18 22:42:11 +02:00
extern crate notify;
2016-09-14 15:30:59 +02:00
#[cfg(unix)]
extern crate nix;
#[cfg(windows)]
extern crate winapi;
#[cfg(windows)]
extern crate kernel32;
2016-10-22 21:37:03 +02:00
2016-10-25 13:05:53 +02:00
#[cfg(test)]
extern crate mktemp;
mod cli;
2016-10-12 04:43:53 +02:00
mod gitignore;
2016-11-09 15:44:00 +01:00
mod interrupt;
2016-09-23 21:52:50 +02:00
mod notification_filter;
mod process;
mod watcher;
2016-09-21 23:02:20 +02:00
use std::collections::HashMap;
use std::env;
use std::path::Path;
2016-11-09 15:44:00 +01:00
use std::sync::{Arc, RwLock};
use std::sync::mpsc::{channel, Receiver};
use std::time::Duration;
use std::path::{PathBuf};
2016-09-18 22:42:11 +02:00
2016-09-23 21:52:50 +02:00
use notification_filter::NotificationFilter;
2016-11-09 23:25:52 +01:00
use process::Process;
use watcher::{Event, Watcher};
2016-09-14 15:30:59 +02:00
fn find_gitignore(path: &Path) -> Option<PathBuf> {
let mut p = path.to_owned();
loop {
let gitignore_path = p.join(".gitignore");
if gitignore_path.exists() {
return Some(gitignore_path);
}
// Stop if we see a .git directory
if let Ok(metadata) = p.join(".git").metadata() {
if metadata.is_dir() {
break;
}
}
if p.parent().is_none() {
break;
}
p.pop();
}
None
}
fn init_logger(debug: bool) {
2016-10-13 23:59:46 +02:00
let mut log_builder = env_logger::LogBuilder::new();
2016-10-18 15:39:40 +02:00
let level = if debug {
log::LogLevelFilter::Debug
} else {
log::LogLevelFilter::Warn
2016-10-18 15:39:40 +02:00
};
2016-10-13 23:59:46 +02:00
log_builder.format(|r| format!("*** {}", r.args()))
2016-10-13 23:59:46 +02:00
.filter(None, level);
log_builder.init().expect("unable to initialize logger");
}
2016-10-12 15:43:22 +02:00
fn main() {
2016-11-09 15:44:00 +01:00
let child_process: Arc<RwLock<Option<Process>>> = Arc::new(RwLock::new(None));
let weak_child = Arc::downgrade(&child_process);
2016-11-09 15:44:00 +01:00
interrupt::install_handler(move || {
if let Some(lock) = weak_child.upgrade() {
let strong = lock.read().unwrap();
if let Some(ref child) = *strong {
child.kill();
child.wait();
}
}
});
let args = cli::get_args();
2016-10-13 23:59:46 +02:00
init_logger(args.debug);
2016-09-21 23:02:20 +02:00
2016-10-12 15:43:22 +02:00
let cwd = env::current_dir()
.expect("unable to get cwd")
.canonicalize()
.expect("unable to canonicalize cwd");
2016-10-12 04:43:53 +02:00
let mut gitignore_file = None;
if !args.no_vcs_ignore {
if let Some(gitignore_path) = find_gitignore(&cwd) {
2016-10-14 01:47:04 +02:00
debug!("Found .gitignore file: {:?}", gitignore_path);
2016-10-12 04:43:53 +02:00
2016-10-12 15:43:22 +02:00
gitignore_file = gitignore::parse(&gitignore_path).ok();
}
2016-10-12 04:43:53 +02:00
}
let filter = NotificationFilter::new(args.filters, args.ignores, gitignore_file)
.expect("unable to create notification filter");
2016-09-21 23:02:20 +02:00
2016-09-14 15:30:59 +02:00
let (tx, rx) = channel();
let mut watcher = Watcher::new(tx, args.poll, args.poll_interval)
.expect("unable to create watcher");
2016-10-20 02:19:01 +02:00
if watcher.is_polling() {
warn!("Polling for changes every {} ms", args.poll_interval);
}
2016-09-18 22:42:11 +02:00
watcher.watch(cwd).expect("unable to watch cwd");
2016-09-18 22:42:11 +02:00
2016-11-09 15:44:00 +01:00
// Start child process initially, if necessary
if args.run_initially {
2016-11-09 23:25:52 +01:00
if args.clear_screen {
cli::clear_screen();
}
2016-11-09 15:44:00 +01:00
let mut guard = child_process.write().unwrap();
*guard = Process::new(&args.cmd, vec![]).ok();
}
2016-11-09 15:44:00 +01:00
loop {
let paths = wait(&rx, &filter);
if let Some(path) = paths.get(0) {
debug!("Path updated: {:?}", path);
}
// Wait for current child process to exit
2016-11-09 15:44:00 +01:00
{
let guard = child_process.read().unwrap();
2016-11-09 15:44:00 +01:00
if let Some(ref child) = *guard {
if args.restart {
debug!("Killing child process");
child.kill();
}
debug!("Waiting for process to exit...");
2016-11-09 15:44:00 +01:00
child.wait();
}
2016-11-09 15:44:00 +01:00
}
2016-11-09 15:44:00 +01:00
// Launch child process
if args.clear_screen {
cli::clear_screen();
}
2016-11-09 15:44:00 +01:00
{
let mut guard = child_process.write().unwrap();
*guard = Process::new(&args.cmd, paths).ok();
}
2016-09-14 15:30:59 +02:00
}
}
2016-10-12 15:43:22 +02:00
2016-11-09 15:44:00 +01:00
fn wait(rx: &Receiver<Event>, filter: &NotificationFilter) -> Vec<PathBuf> {
let mut paths = vec![];
let mut cache = HashMap::new();
2016-10-12 15:43:22 +02:00
loop {
2016-11-09 15:44:00 +01:00
let e = rx.recv().expect("error when reading event");
if let Some(ref path) = e.path {
// Ignore cache for the initial file. Otherwise, in
// debug mode it's hard to track what's going on
let excluded = filter.is_excluded(path);
if !cache.contains_key(path) {
cache.insert(path.to_owned(), excluded);
}
if !excluded {
paths.push(path.to_owned());
break;
2016-10-12 15:43:22 +02:00
}
2016-11-09 15:44:00 +01:00
}
}
2016-10-12 15:43:22 +02:00
// Wait for filesystem activity to cool off
let timeout = Duration::from_millis(500);
while let Ok(e) = rx.recv_timeout(timeout) {
if let Some(ref path) = e.path {
if cache.contains_key(path) {
continue;
}
let excluded = filter.is_excluded(path);
let p = path.to_owned();
cache.insert(p.clone(), excluded);
if !excluded {
paths.push(p);
}
}
2016-10-12 15:43:22 +02:00
}
2016-11-09 15:44:00 +01:00
paths
2016-10-12 15:43:22 +02:00
}