Infer path, add -e option to monitor files based on file extensions

This commit is contained in:
Matt Green 2016-09-23 11:59:57 -06:00
parent 9927e1018e
commit d087092f64
4 changed files with 49 additions and 16 deletions

View File

@ -1,6 +1,6 @@
[package]
name = "watchexec"
version = "0.8.0"
version = "0.9.0"
authors = ["Matt Green <mattgreenrocks@gmail.com>"]
[profile.release]

View File

@ -11,15 +11,16 @@ Example use cases:
##Status
Beta: CLI arguments subject to change
Beta: CLI arguments stabilizing
##Features
* Simple invocation and use
* Runs on OS X, Linux and Windows
* Monitors path specified on command line for changes
* Uses most efficient event polling mechanism, based on platform (except for [BSD](https://github.com/passcod/rsnotify#todo))
* Uses most efficient event polling mechanism for your platform (except for [BSD](https://github.com/passcod/rsnotify#todo))
* Coalesces multiple filesystem events into one, for editors that use swap/backup files during saving
* Support for watching files with a specific extension
* Support for filtering/ignoring events based on glob patterns
* Optionally clears screen between executions
* Does not require a language runtime
@ -30,21 +31,19 @@ Beta: CLI arguments subject to change
* Not tied to any particular language or ecosystem
* Does not require a cryptic command line involving `xargs`
##Usage
##Usage Examples
Call `make test` when there are any changes in the `src` directory:
Watch all JavaScript, CSS and HTML files in the current directory and all subdirectories for changes, running `make` when a change is detected:
$ watchexec src make test
$ watchexec --exts js,css,html make
Call `make test` when any Python file changes in this directory, or a subdirectory:
Watch all files below `src` and subdirectories for changes, running `make test` when a change is detected:
$ watchexec -f '*.py' . make test
$ watchexec --watch src make test
Call `make test` when any file changes in this directory/subdirectory, except for everything below `target`:
$ watchexec -i target . make test
Always quote glob patterns (*.py)!
$ watchexec -i target make
##Installation

View File

@ -19,6 +19,24 @@ impl Filter {
}
}
pub fn add_extension(&mut self, extension: &str) -> Result<(), PatternError> {
let mut pattern = String::new();
for ext in extension.split(",") {
pattern.clear();
pattern.push_str("*");
if !ext.starts_with(".") {
pattern.push_str(".");
}
pattern.push_str(ext);
try!(self.add_filter(&pattern));
}
Ok(())
}
pub fn add_filter(&mut self, pattern: &str) -> Result<Pattern, PatternError> {
let compiled = try!(self.pattern_for(pattern));
self.filters.push(compiled.clone());

View File

@ -66,15 +66,23 @@ fn wait(rx: &Receiver<Event>, filter: &Filter, debug: bool) -> Result<Event, Rec
fn main() {
let args = App::new("watchexec")
.version("0.8")
.version("0.9")
.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))
.help("Path(s) to watch for changes")
.short("w")
.long("watch")
.takes_value(true)
.default_value("."))
.arg(Arg::with_name("command")
.help("Command to run")
.multiple(true)
.required(true))
.arg(Arg::with_name("extensions")
.help("Comma-separated list of file extensions to watch (js,css,html)")
.short("e")
.long("exts")
.takes_value(true))
.arg(Arg::with_name("clear")
.help("Clear screen before running command")
.short("c")
@ -89,14 +97,16 @@ fn main() {
.long("filter")
.number_of_values(1)
.multiple(true)
.takes_value(true))
.takes_value(true)
.value_name("pattern"))
.arg(Arg::with_name("ignore")
.help("Ignore events from paths matching a pattern")
.short("i")
.long("ignore")
.number_of_values(1)
.multiple(true)
.takes_value(true))
.takes_value(true)
.value_name("pattern"))
.get_matches();
let debug = args.is_present("debug");
@ -110,6 +120,12 @@ fn main() {
filter.add_ignore(p).expect("bad default filter");
}
if let Some(extensions) = args.values_of("extensions") {
for ext in extensions {
filter.add_extension(ext).expect("bad extension");
}
}
if let Some(filters) = args.values_of("filter") {
for p in filters {
filter.add_filter(p).expect("bad filter");