2016-09-18 22:42:11 +02:00
|
|
|
extern crate clap;
|
|
|
|
extern crate notify;
|
2016-09-14 15:30:59 +02:00
|
|
|
|
2016-10-12 04:43:53 +02:00
|
|
|
mod gitignore;
|
2016-09-23 21:52:50 +02:00
|
|
|
mod notification_filter;
|
2016-10-09 20:07:23 +02:00
|
|
|
mod runner;
|
2016-09-21 23:02:20 +02:00
|
|
|
|
2016-09-14 21:43:58 +02:00
|
|
|
use std::sync::mpsc::{channel, Receiver, RecvError};
|
2016-10-09 20:07:23 +02:00
|
|
|
use std::{env, thread, time};
|
2016-09-24 20:06:51 +02:00
|
|
|
use std::path::Path;
|
2016-09-18 22:42:11 +02:00
|
|
|
|
|
|
|
use clap::{App, Arg};
|
|
|
|
use notify::{Event, RecommendedWatcher, Watcher};
|
2016-09-14 21:43:58 +02:00
|
|
|
|
2016-09-23 21:52:50 +02:00
|
|
|
use notification_filter::NotificationFilter;
|
2016-10-09 20:07:23 +02:00
|
|
|
use runner::Runner;
|
2016-09-14 15:30:59 +02:00
|
|
|
|
2016-09-23 21:52:50 +02:00
|
|
|
fn wait(rx: &Receiver<Event>, filter: &NotificationFilter, verbose: bool) -> Result<Event, RecvError> {
|
2016-09-14 22:11:35 +02:00
|
|
|
loop {
|
2016-09-18 22:42:11 +02:00
|
|
|
// Block on initial notification
|
2016-09-14 22:11:35 +02:00
|
|
|
let e = try!(rx.recv());
|
2016-09-18 22:42:11 +02:00
|
|
|
if let Some(ref path) = e.path {
|
2016-09-21 23:02:20 +02:00
|
|
|
if filter.is_excluded(&path) {
|
2016-09-23 21:38:26 +02:00
|
|
|
if verbose {
|
2016-09-21 23:02:20 +02:00
|
|
|
println!("*** Ignoring {} due to filter", path.to_str().unwrap());
|
|
|
|
}
|
2016-09-18 22:42:11 +02:00
|
|
|
continue;
|
|
|
|
}
|
2016-09-14 22:11:35 +02:00
|
|
|
}
|
2016-09-14 21:43:58 +02:00
|
|
|
|
2016-09-18 22:42:11 +02:00
|
|
|
// Accumulate subsequent events
|
2016-09-14 22:11:35 +02:00
|
|
|
thread::sleep(time::Duration::from_millis(250));
|
2016-09-18 22:42:11 +02:00
|
|
|
|
|
|
|
// Drain rx buffer and drop them
|
2016-09-14 22:11:35 +02:00
|
|
|
loop {
|
|
|
|
match rx.try_recv() {
|
|
|
|
Ok(_) => continue,
|
|
|
|
Err(_) => break,
|
|
|
|
}
|
2016-09-14 21:43:58 +02:00
|
|
|
}
|
|
|
|
|
2016-09-14 22:11:35 +02:00
|
|
|
return Ok(e);
|
|
|
|
}
|
2016-09-14 21:43:58 +02:00
|
|
|
}
|
|
|
|
|
2016-09-14 15:30:59 +02:00
|
|
|
fn main() {
|
2016-09-18 22:42:11 +02:00
|
|
|
let args = App::new("watchexec")
|
2016-10-12 04:43:53 +02:00
|
|
|
.version("0.11.0")
|
2016-09-23 21:49:55 +02:00
|
|
|
.about("Execute commands when watched files change")
|
2016-09-18 22:42:11 +02:00
|
|
|
.arg(Arg::with_name("path")
|
2016-09-23 21:49:55 +02:00
|
|
|
.help("Path to watch")
|
2016-09-23 19:59:57 +02:00
|
|
|
.short("w")
|
|
|
|
.long("watch")
|
2016-09-23 21:38:26 +02:00
|
|
|
.number_of_values(1)
|
|
|
|
.multiple(true)
|
2016-09-23 19:59:57 +02:00
|
|
|
.takes_value(true)
|
|
|
|
.default_value("."))
|
2016-09-18 22:42:11 +02:00
|
|
|
.arg(Arg::with_name("command")
|
2016-09-23 21:49:55 +02:00
|
|
|
.help("Command to execute")
|
2016-09-21 23:02:20 +02:00
|
|
|
.multiple(true)
|
2016-09-18 22:42:11 +02:00
|
|
|
.required(true))
|
2016-09-23 19:59:57 +02:00
|
|
|
.arg(Arg::with_name("extensions")
|
|
|
|
.help("Comma-separated list of file extensions to watch (js,css,html)")
|
|
|
|
.short("e")
|
|
|
|
.long("exts")
|
|
|
|
.takes_value(true))
|
2016-09-18 22:42:11 +02:00
|
|
|
.arg(Arg::with_name("clear")
|
2016-09-23 21:49:55 +02:00
|
|
|
.help("Clear screen before executing command")
|
2016-09-18 22:42:11 +02:00
|
|
|
.short("c")
|
|
|
|
.long("clear"))
|
2016-09-24 19:52:54 +02:00
|
|
|
.arg(Arg::with_name("restart")
|
|
|
|
.help("Restart the process if it's still running")
|
|
|
|
.short("r")
|
|
|
|
.long("restart"))
|
2016-09-23 21:38:26 +02:00
|
|
|
.arg(Arg::with_name("verbose")
|
2016-09-23 21:49:55 +02:00
|
|
|
.help("Prints diagnostic messages")
|
2016-09-23 21:38:26 +02:00
|
|
|
.short("v")
|
|
|
|
.long("verbose"))
|
2016-09-21 23:02:20 +02:00
|
|
|
.arg(Arg::with_name("filter")
|
2016-09-23 21:49:55 +02:00
|
|
|
.help("Ignore all modifications except those matching the pattern")
|
2016-09-21 23:02:20 +02:00
|
|
|
.short("f")
|
|
|
|
.long("filter")
|
|
|
|
.number_of_values(1)
|
|
|
|
.multiple(true)
|
2016-09-23 19:59:57 +02:00
|
|
|
.takes_value(true)
|
|
|
|
.value_name("pattern"))
|
2016-09-21 23:02:20 +02:00
|
|
|
.arg(Arg::with_name("ignore")
|
2016-09-23 21:49:55 +02:00
|
|
|
.help("Ignore modifications to paths matching the pattern")
|
2016-09-21 23:02:20 +02:00
|
|
|
.short("i")
|
|
|
|
.long("ignore")
|
|
|
|
.number_of_values(1)
|
|
|
|
.multiple(true)
|
2016-09-23 19:59:57 +02:00
|
|
|
.takes_value(true)
|
|
|
|
.value_name("pattern"))
|
2016-09-18 22:42:11 +02:00
|
|
|
.get_matches();
|
2016-09-14 22:50:14 +02:00
|
|
|
|
2016-09-23 21:38:26 +02:00
|
|
|
let verbose = args.is_present("verbose");
|
2016-09-21 23:02:20 +02:00
|
|
|
|
|
|
|
let cwd = env::current_dir().unwrap();
|
2016-10-12 04:43:53 +02:00
|
|
|
|
|
|
|
let mut gitignore_file = None;
|
|
|
|
let gitignore_path = cwd.join(".gitignore");
|
|
|
|
if gitignore_path.exists() {
|
|
|
|
if verbose {
|
|
|
|
println!("*** Found .gitignore file: {}", gitignore_path.to_str().unwrap());
|
|
|
|
}
|
|
|
|
|
|
|
|
gitignore_file = gitignore::File::new(&gitignore_path).ok();
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut filter = NotificationFilter::new(&cwd, gitignore_file).expect("unable to create notification filter");
|
2016-09-21 23:02:20 +02:00
|
|
|
|
2016-09-24 20:06:51 +02:00
|
|
|
// Add default ignore list
|
|
|
|
let dotted_dirs = Path::new(".*").join("*");
|
|
|
|
let default_filters = vec!["*.pyc", "*.swp", dotted_dirs.to_str().unwrap()];
|
2016-09-21 23:02:20 +02:00
|
|
|
for p in default_filters {
|
|
|
|
filter.add_ignore(p).expect("bad default filter");
|
|
|
|
}
|
|
|
|
|
2016-09-23 19:59:57 +02:00
|
|
|
if let Some(extensions) = args.values_of("extensions") {
|
|
|
|
for ext in extensions {
|
|
|
|
filter.add_extension(ext).expect("bad extension");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-21 23:02:20 +02:00
|
|
|
if let Some(filters) = args.values_of("filter") {
|
|
|
|
for p in filters {
|
|
|
|
filter.add_filter(p).expect("bad filter");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if let Some(ignores) = args.values_of("ignore") {
|
|
|
|
for i in ignores {
|
|
|
|
filter.add_ignore(i).expect("bad ignore pattern");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-14 15:30:59 +02:00
|
|
|
let (tx, rx) = channel();
|
2016-09-18 22:42:11 +02:00
|
|
|
let mut watcher: RecommendedWatcher = Watcher::new(tx).expect("unable to create watcher");
|
|
|
|
|
|
|
|
let paths = args.values_of("path").unwrap();
|
|
|
|
for path in paths {
|
2016-09-27 15:43:28 +02:00
|
|
|
match Path::new(path).canonicalize() {
|
|
|
|
Ok(canonicalized) => watcher.watch(canonicalized).expect("unable to watch path"),
|
|
|
|
Err(_) => {
|
|
|
|
println!("invalid path: {}", path);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2016-09-18 22:42:11 +02:00
|
|
|
}
|
|
|
|
|
2016-09-21 23:02:20 +02:00
|
|
|
let cmd_parts: Vec<&str> = args.values_of("command").unwrap().collect();
|
|
|
|
let cmd = cmd_parts.join(" ");
|
2016-10-09 20:07:23 +02:00
|
|
|
let mut runner = Runner::new(args.is_present("restart"), args.is_present("clear"), verbose);
|
2016-09-14 15:30:59 +02:00
|
|
|
|
|
|
|
loop {
|
2016-09-23 21:38:26 +02:00
|
|
|
let e = wait(&rx, &filter, verbose).expect("error when waiting for filesystem changes");
|
2016-09-24 19:52:54 +02:00
|
|
|
|
2016-09-23 21:38:26 +02:00
|
|
|
if verbose {
|
2016-09-18 22:42:11 +02:00
|
|
|
println!("*** {:?}: {:?}", e.op, e.path);
|
|
|
|
}
|
2016-09-14 21:43:58 +02:00
|
|
|
|
2016-10-09 20:07:23 +02:00
|
|
|
runner.run_command(&cmd);
|
2016-09-14 15:30:59 +02:00
|
|
|
}
|
|
|
|
}
|