Skip to content

Commit

Permalink
Add support for watching additional directories
Browse files Browse the repository at this point in the history
  • Loading branch information
willcrichton committed Sep 1, 2022
1 parent 1a08927 commit 08ceb55
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 19 deletions.
37 changes: 20 additions & 17 deletions src/cmd/serve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ pub fn make_subcommand<'help>() -> App<'help> {
.help("Port to use for HTTP connections"),
)
.arg(arg!(-o --open "Opens the compiled book in a web browser"))
.arg(arg!(--"extra-watch-dirs" <dirs> "Extra directories to watch, comma separated"))
}

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

#[cfg(feature = "watch")]
watch::trigger_on_change(&book, move |paths, book_dir| {
info!("Files changed: {:?}", paths);
info!("Building book...");

// FIXME: This area is really ugly because we need to re-set livereload :(
let result = MDBook::load(&book_dir).and_then(|mut b| {
update_config(&mut b);
b.build()
{
let extra_dirs = watch::parse_extra_dirs(args.value_of("extra-watch-dirs"))?;
watch::trigger_on_change(&book, &extra_dirs, move |paths, book_dir| {
info!("Files changed: {:?}", paths);
info!("Building book...");

// FIXME: This area is really ugly because we need to re-set livereload :(
let result = MDBook::load(&book_dir).and_then(|mut b| {
update_config(&mut b);
b.build()
});

if let Err(e) = result {
error!("Unable to load the book");
utils::log_backtrace(&e);
} else {
let _ = tx.send(Message::text("reload"));
}
});

if let Err(e) = result {
error!("Unable to load the book");
utils::log_backtrace(&e);
} else {
let _ = tx.send(Message::text("reload"));
}
});

}
let _ = thread_handle.join();

Ok(())
Expand Down
21 changes: 19 additions & 2 deletions src/cmd/watch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ pub fn make_subcommand<'help>() -> App<'help> {
(Defaults to the Current Directory when omitted)"
))
.arg(arg!(-o --open "Opens the compiled book in a web browser"))
.arg(arg!(--"extra-watch-dirs" <dirs> "Extra directories to watch, comma separated"))
}

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

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

/// Turn comma-separated list of directories into a Vec<PathBuf>
pub fn parse_extra_dirs(dirs: Option<&str>) -> Result<Vec<PathBuf>> {
match dirs {
Some(dirs) => dirs
.split(",")
.map(|s| Ok(PathBuf::from(s).canonicalize()?))
.collect::<Result<Vec<_>>>(),
None => Ok(Vec::new()),
}
}

/// Calls the closure when a book source file is changed, blocking indefinitely.
pub fn trigger_on_change<F>(book: &MDBook, closure: F)
pub fn trigger_on_change<F>(book: &MDBook, extra_dirs: &[PathBuf], closure: F)
where
F: Fn(Vec<PathBuf>, &Path),
{
Expand Down Expand Up @@ -146,6 +159,10 @@ where
// Add the book.toml file to the watcher if it exists
let _ = watcher.watch(book.root.join("book.toml"), NonRecursive);

for dir in extra_dirs {
let _ = watcher.watch(dir, Recursive);
}

info!("Listening for changes...");

loop {
Expand Down

0 comments on commit 08ceb55

Please sign in to comment.