Skip to content

Commit 08ceb55

Browse files
committed
Add support for watching additional directories
1 parent 1a08927 commit 08ceb55

File tree

2 files changed

+39
-19
lines changed

2 files changed

+39
-19
lines changed

src/cmd/serve.rs

+20-17
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ pub fn make_subcommand<'help>() -> App<'help> {
5555
.help("Port to use for HTTP connections"),
5656
)
5757
.arg(arg!(-o --open "Opens the compiled book in a web browser"))
58+
.arg(arg!(--"extra-watch-dirs" <dirs> "Extra directories to watch, comma separated"))
5859
}
5960

6061
// Serve command implementation
@@ -110,24 +111,26 @@ pub fn execute(args: &ArgMatches) -> Result<()> {
110111
}
111112

112113
#[cfg(feature = "watch")]
113-
watch::trigger_on_change(&book, move |paths, book_dir| {
114-
info!("Files changed: {:?}", paths);
115-
info!("Building book...");
116-
117-
// FIXME: This area is really ugly because we need to re-set livereload :(
118-
let result = MDBook::load(&book_dir).and_then(|mut b| {
119-
update_config(&mut b);
120-
b.build()
114+
{
115+
let extra_dirs = watch::parse_extra_dirs(args.value_of("extra-watch-dirs"))?;
116+
watch::trigger_on_change(&book, &extra_dirs, move |paths, book_dir| {
117+
info!("Files changed: {:?}", paths);
118+
info!("Building book...");
119+
120+
// FIXME: This area is really ugly because we need to re-set livereload :(
121+
let result = MDBook::load(&book_dir).and_then(|mut b| {
122+
update_config(&mut b);
123+
b.build()
124+
});
125+
126+
if let Err(e) = result {
127+
error!("Unable to load the book");
128+
utils::log_backtrace(&e);
129+
} else {
130+
let _ = tx.send(Message::text("reload"));
131+
}
121132
});
122-
123-
if let Err(e) = result {
124-
error!("Unable to load the book");
125-
utils::log_backtrace(&e);
126-
} else {
127-
let _ = tx.send(Message::text("reload"));
128-
}
129-
});
130-
133+
}
131134
let _ = thread_handle.join();
132135

133136
Ok(())

src/cmd/watch.rs

+19-2
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ pub fn make_subcommand<'help>() -> App<'help> {
2929
(Defaults to the Current Directory when omitted)"
3030
))
3131
.arg(arg!(-o --open "Opens the compiled book in a web browser"))
32+
.arg(arg!(--"extra-watch-dirs" <dirs> "Extra directories to watch, comma separated"))
3233
}
3334

3435
// Watch command implementation
@@ -53,7 +54,8 @@ pub fn execute(args: &ArgMatches) -> Result<()> {
5354
open(path);
5455
}
5556

56-
trigger_on_change(&book, |paths, book_dir| {
57+
let extra_dirs = parse_extra_dirs(args.value_of("extra-watch-dirs"))?;
58+
trigger_on_change(&book, &extra_dirs, |paths, book_dir| {
5759
info!("Files changed: {:?}\nBuilding book...\n", paths);
5860
let result = MDBook::load(&book_dir).and_then(|mut b| {
5961
update_config(&mut b);
@@ -116,8 +118,19 @@ fn filter_ignored_files(exclusion_checker: gitignore::File, paths: &[PathBuf]) -
116118
.collect()
117119
}
118120

121+
/// Turn comma-separated list of directories into a Vec<PathBuf>
122+
pub fn parse_extra_dirs(dirs: Option<&str>) -> Result<Vec<PathBuf>> {
123+
match dirs {
124+
Some(dirs) => dirs
125+
.split(",")
126+
.map(|s| Ok(PathBuf::from(s).canonicalize()?))
127+
.collect::<Result<Vec<_>>>(),
128+
None => Ok(Vec::new()),
129+
}
130+
}
131+
119132
/// Calls the closure when a book source file is changed, blocking indefinitely.
120-
pub fn trigger_on_change<F>(book: &MDBook, closure: F)
133+
pub fn trigger_on_change<F>(book: &MDBook, extra_dirs: &[PathBuf], closure: F)
121134
where
122135
F: Fn(Vec<PathBuf>, &Path),
123136
{
@@ -146,6 +159,10 @@ where
146159
// Add the book.toml file to the watcher if it exists
147160
let _ = watcher.watch(book.root.join("book.toml"), NonRecursive);
148161

162+
for dir in extra_dirs {
163+
let _ = watcher.watch(dir, Recursive);
164+
}
165+
149166
info!("Listening for changes...");
150167

151168
loop {

0 commit comments

Comments
 (0)