watchexec/src/main.rs

114 lines
2.7 KiB
Rust
Raw Normal View History

2016-09-18 22:42:11 +02:00
extern crate clap;
2016-09-14 15:30:59 +02:00
extern crate libc;
2016-09-18 22:42:11 +02:00
extern crate notify;
2016-09-14 15:30:59 +02:00
use std::ffi::CString;
2016-09-18 22:42:11 +02:00
use std::path::Path;
2016-09-14 21:43:58 +02:00
use std::sync::mpsc::{channel, Receiver, RecvError};
use std::{thread, time};
2016-09-18 22:42:11 +02:00
use std::process::Command;
use libc::system;
use clap::{App, Arg};
use notify::{Event, RecommendedWatcher, Watcher};
2016-09-14 21:43:58 +02:00
fn clear() {
2016-09-18 22:42:11 +02:00
// TODO: determine better way to do this
let clear_cmd;
if cfg!(target_os = "windows") {
clear_cmd = "cls";
2016-09-14 21:43:58 +02:00
}
2016-09-18 22:42:11 +02:00
else {
clear_cmd = "clear";
2016-09-14 22:11:35 +02:00
}
2016-09-18 22:42:11 +02:00
let _ = Command::new(clear_cmd).status();
2016-09-14 22:11:35 +02:00
}
2016-09-18 22:42:11 +02:00
fn invoke(cmd: &str) {
// TODO: determine a better way to get at system()
2016-09-14 15:30:59 +02:00
let s = CString::new(cmd.clone()).unwrap();
unsafe {
system(s.as_ptr());
}
}
2016-09-18 22:42:11 +02:00
fn ignored(_: &Path) -> bool {
// TODO: ignore *.pyc files
// TODO: handle .git directory?
false
2016-09-14 22:50:14 +02:00
}
2016-09-18 22:42:11 +02:00
fn wait(rx: &Receiver<Event>) -> 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 {
if ignored(&path) {
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")
.version("0.7")
.about("Runs a command when any of the specified files/directories are modified")
.arg(Arg::with_name("path")
.help("Path to watch for changes")
.required(true))
.arg(Arg::with_name("command")
.help("Command to run")
.required(true))
.arg(Arg::with_name("clear")
.help("Clear screen before running command")
.short("c")
.long("clear"))
.arg(Arg::with_name("debug")
.help("Enable debug messages")
.short("d")
.long("debug"))
.get_matches();
2016-09-14 22:50:14 +02:00
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");
// TODO: handle multiple paths
let paths = args.values_of("path").unwrap();
for path in paths {
watcher.watch(path).expect("unable to watch path");
}
let cmd = args.value_of("command").unwrap();
let need_clear = args.is_present("clear");
let debug = args.is_present("debug");
2016-09-14 15:30:59 +02:00
loop {
2016-09-18 22:42:11 +02:00
let e = wait(&rx).expect("error when waiting for filesystem changes");
if need_clear {
clear();
}
if debug {
println!("*** {:?}: {:?}", e.op, e.path);
}
2016-09-14 21:43:58 +02:00
2016-09-18 22:42:11 +02:00
invoke(cmd);
2016-09-14 15:30:59 +02:00
}
}