1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
//! Auto run command when some files changed.
//!
//! `fwatcher` also has a [cli command](./cli/index.html).
//!
//! # Usage
//!
//! Dependencies in your project's `Cargo.toml`:
//!
//! ```toml
//! [dependencies]
//! glob = "0.2"
//! notify = "4.0"
//! fwatcher = "*"
//! ```
//!
//! # Example
//!
//! ```rust
//! extern crate glob;
//! extern crate fwatcher;
//!
//! use fwatcher::Fwatcher;
//! use glob::Pattern;
//! use std::path::PathBuf;
//! use std::time::Duration;
//!
//! fn main() {
//!     let dirs =vec![PathBuf::from("src")];
//!     let cmd = vec!["pytest".to_string()];
//!
//!     let mut fwatcher = Fwatcher::new(dirs, cmd);
//!     fwatcher.pattern(Pattern::new("**/*.py").unwrap())
//!             .exclude_pattern(Pattern::new("**/.git/**").unwrap())
//!             .interval(Duration::new(1, 0))
//!             .restart(false)
//!             .run();
//! }
//! ```

#![doc(html_logo_url = "https://www.rust-lang.org/logos/rust-logo-128x128-blk-v2.png",
       html_favicon_url = "https://www.rust-lang.org/favicon.ico",
       html_root_url = "https://docs.rs/fwatcher")]

extern crate getopts;
extern crate glob;
extern crate notify;

use glob::Pattern;
use notify::{DebouncedEvent, RecursiveMode, Watcher, watcher};
use std::path::PathBuf;
use std::process::{Child, Command};
use std::sync::mpsc::channel;
use std::time::{Duration, Instant};

pub mod cli;

/// a struct save `Fwatcher` state
pub struct Fwatcher {
    dirs: Vec<PathBuf>,
    patterns: Vec<Pattern>,
    exclude_patterns: Vec<Pattern>,
    delay: Duration,
    interval: Duration,
    restart: bool,
    cmd: Vec<String>,
    last_run: Option<Instant>,
    child: Option<Child>,
}

impl Fwatcher {
    /// Constructs a new `Fwatcher`
    pub fn new(dirs: Vec<PathBuf>, cmd: Vec<String>) -> Self {
        Fwatcher {
            dirs: dirs,
            patterns: Vec::new(),
            exclude_patterns: Vec::new(),
            delay: Duration::new(2, 0),
            interval: Duration::new(1, 0),
            restart: false,
            cmd: cmd,
            last_run: None,
            child: None,
        }
    }

    /// add a watcher pattern
    pub fn pattern(&mut self, pat: Pattern) -> &mut Self {
        self.patterns.push(pat);
        self
    }

    /// add watcher patterns
    pub fn patterns(&mut self, pats: &[Pattern]) -> &mut Self {
        self.patterns.extend_from_slice(pats);
        self
    }

    /// add a watcher exclusive pattern
    pub fn exclude_pattern(&mut self, pat: Pattern) -> &mut Self {
        self.exclude_patterns.push(pat);
        self
    }

    /// add watcher exclusive patterns
    pub fn exclude_patterns(&mut self, pats: &[Pattern]) -> &mut Self {
        self.exclude_patterns.extend_from_slice(pats);
        self
    }

    /// set watcher interval seconds
    pub fn delay(&mut self, d: Duration) -> &mut Self {
        self.delay = d;
        self
    }

    /// set watcher interval seconds
    pub fn interval(&mut self, d: Duration) -> &mut Self {
        self.interval = d;
        self
    }

    /// set watcher restart flag
    pub fn restart(&mut self, flag: bool) -> &mut Self {
        self.restart = flag;
        self
    }

    /// run `Fwatcher`
    pub fn run(&mut self) {
        let (tx, rx) = channel();
        let mut watcher = watcher(tx, self.delay).expect("can not create a watcher");

        if self.dirs.is_empty() {
            watcher.watch(::std::env::current_dir().expect("get current dir error"),
                          RecursiveMode::Recursive)
                   .expect("can not watch dir");
        } else {
            for d in self.dirs.iter() {
                watcher.watch(d, RecursiveMode::Recursive)
                       .expect("can not watch dir");
            }
        }
        self.restart_child();

        loop {
            match rx.recv() {
                Ok(event) => {
                    if self.interval_consumed() {
                        self.process_event(event)
                    }
                },
                Err(why) => println!("watch error: {:?}", why),
            }
        }
    }

    fn interval_consumed(&mut self) -> bool {
        let now = Instant::now();

        if let Some(last_run) = self.last_run {
            if now.duration_since(last_run) < self.interval {
                return false;
            }
        }

        return true;
    }

    fn process_event(&mut self, event: DebouncedEvent) {
        match event {
            DebouncedEvent::Create(ref fpath) |
            DebouncedEvent::Write(ref fpath)  |
            DebouncedEvent::Remove(ref fpath) |
            DebouncedEvent::Rename(ref fpath, _) => {
                if self.patterns
                       .iter()
                       .any(|ref pat| pat.matches_path(fpath)) &&
                   self.exclude_patterns
                       .iter()
                       .all(|ref pat| !pat.matches_path(fpath)) {
                    println!("Modified: {:?}", fpath);
                    self.restart_child();
                }
            },
            _ => {},
        }
    }

    fn restart_child(&mut self) {
        if let Some(ref mut child) = self.child {
            if self.restart {
                let _ = child.kill();
            }
        }
        self.child = Command::new(&self.cmd[0])
            .args(&self.cmd[1..])
            .spawn()
            .ok();
        self.last_run = Some(Instant::now());
    }
}