|
@@ -1,9 +1,9 @@
|
1
|
1
|
use std::sync::mpsc::channel;
|
2
|
2
|
use std::time::Duration;
|
3
|
|
-use std::{env, fs};
|
|
3
|
+use std::{env, fs, path};
|
4
|
4
|
|
5
|
5
|
use fs_extra::dir;
|
6
|
|
-use notify::{RecommendedWatcher, RecursiveMode, Watcher};
|
|
6
|
+use notify::{DebouncedEvent, RecommendedWatcher, RecursiveMode, Watcher};
|
7
|
7
|
use toml::Value;
|
8
|
8
|
|
9
|
9
|
use config::Config;
|
|
@@ -105,17 +105,39 @@ pub fn new(name: &str) {
|
105
|
105
|
}
|
106
|
106
|
}
|
107
|
107
|
|
|
108
|
+fn should_rebuild(cwd: &path::PathBuf, path: &path::PathBuf) -> bool {
|
|
109
|
+ let path_string = path.to_str().unwrap().to_string();
|
|
110
|
+ let change_is_from_public = path_string.contains(cwd.join("public").to_str().unwrap());
|
|
111
|
+ let change_is_from_git = path_string.contains(cwd.join(".git").to_str().unwrap());
|
|
112
|
+
|
|
113
|
+ !change_is_from_public && !change_is_from_git
|
|
114
|
+}
|
|
115
|
+
|
108
|
116
|
pub fn watch(include_drafts: bool) -> notify::Result<()> {
|
109
|
117
|
let cwd = env::current_dir().expect("Couldn't read current directory");
|
110
|
118
|
let (tx, rx) = channel();
|
111
|
119
|
let mut watcher: RecommendedWatcher = try!(Watcher::new(tx, Duration::from_secs(2)));
|
112
|
|
- try!(watcher.watch(&cwd.join("posts"), RecursiveMode::Recursive));
|
113
|
|
- println!("Watching {}/posts", cwd.to_str().unwrap());
|
|
120
|
+ try!(watcher.watch(&cwd, RecursiveMode::Recursive));
|
|
121
|
+ println!("Watching {}", cwd.to_str().unwrap());
|
|
122
|
+
|
|
123
|
+ let handle_event = |path: &path::PathBuf| {
|
|
124
|
+ if should_rebuild(&cwd, &path) {
|
|
125
|
+ println!("Rebuilding");
|
|
126
|
+ build(include_drafts);
|
|
127
|
+ }
|
|
128
|
+ };
|
|
129
|
+
|
114
|
130
|
loop {
|
115
|
131
|
match rx.recv() {
|
116
|
|
- Ok(_) => {
|
117
|
|
- build(include_drafts);
|
118
|
|
- }
|
|
132
|
+ Ok(e) => match e {
|
|
133
|
+ DebouncedEvent::Create(path) => {
|
|
134
|
+ handle_event(&path);
|
|
135
|
+ }
|
|
136
|
+ DebouncedEvent::Write(path) => {
|
|
137
|
+ handle_event(&path);
|
|
138
|
+ }
|
|
139
|
+ _ => {}
|
|
140
|
+ },
|
119
|
141
|
Err(e) => println!("watch error: {:?}", e),
|
120
|
142
|
}
|
121
|
143
|
}
|
|
@@ -332,4 +354,30 @@ mod tests {
|
332
|
354
|
|
333
|
355
|
fs::remove_dir_all(project_dir).unwrap();
|
334
|
356
|
}
|
|
357
|
+
|
|
358
|
+ #[test]
|
|
359
|
+ fn test_should_rebuild() {
|
|
360
|
+ let cwd = env::current_dir().unwrap();
|
|
361
|
+ assert_eq!(
|
|
362
|
+ false,
|
|
363
|
+ should_rebuild(&cwd, &cwd.join("public").join("index.html"))
|
|
364
|
+ );
|
|
365
|
+ assert_eq!(
|
|
366
|
+ false,
|
|
367
|
+ should_rebuild(&cwd, &cwd.join(".git").join("index.html"))
|
|
368
|
+ );
|
|
369
|
+ assert_eq!(
|
|
370
|
+ true,
|
|
371
|
+ should_rebuild(&cwd, &cwd.join("posts").join("test.md"))
|
|
372
|
+ );
|
|
373
|
+ assert_eq!(
|
|
374
|
+ true,
|
|
375
|
+ should_rebuild(&cwd, &cwd.join("drafts").join("test.md"))
|
|
376
|
+ );
|
|
377
|
+ assert_eq!(
|
|
378
|
+ true,
|
|
379
|
+ should_rebuild(&cwd, &cwd.join("css").join("style.css"))
|
|
380
|
+ );
|
|
381
|
+ assert_eq!(true, should_rebuild(&cwd, &cwd.join("js").join("index.js")));
|
|
382
|
+ }
|
335
|
383
|
}
|