2017-06-26 05:44:28 +08:00
|
|
|
extern crate notify;
|
|
|
|
|
|
|
|
use self::notify::Watcher;
|
2017-10-03 19:40:23 +08:00
|
|
|
use clap::{App, ArgMatches, SubCommand};
|
2017-06-27 05:17:46 +08:00
|
|
|
use mdbook::errors::Result;
|
2018-07-24 01:45:01 +08:00
|
|
|
use mdbook::utils;
|
|
|
|
use mdbook::MDBook;
|
|
|
|
use std::path::Path;
|
|
|
|
use std::sync::mpsc::channel;
|
|
|
|
use std::time::Duration;
|
2017-06-26 05:44:28 +08:00
|
|
|
use {get_book_dir, open};
|
|
|
|
|
2017-06-27 13:59:50 +08:00
|
|
|
// Create clap subcommand arguments
|
|
|
|
pub fn make_subcommand<'a, 'b>() -> App<'a, 'b> {
|
|
|
|
SubCommand::with_name("watch")
|
|
|
|
.about("Watch the files for changes")
|
|
|
|
.arg_from_usage("-o, --open 'Open the compiled book in a web browser'")
|
2017-10-03 19:40:23 +08:00
|
|
|
.arg_from_usage(
|
2017-12-13 09:04:24 +08:00
|
|
|
"[dir] 'A directory for your book{n}(Defaults to Current Directory when omitted)'",
|
2017-10-03 19:40:23 +08:00
|
|
|
)
|
2017-06-27 13:59:50 +08:00
|
|
|
}
|
|
|
|
|
2017-06-26 05:44:28 +08:00
|
|
|
// Watch command implementation
|
2017-06-27 13:59:50 +08:00
|
|
|
pub fn execute(args: &ArgMatches) -> Result<()> {
|
2017-06-26 05:44:28 +08:00
|
|
|
let book_dir = get_book_dir(args);
|
2018-01-07 22:10:48 +08:00
|
|
|
let book = MDBook::load(&book_dir)?;
|
2017-06-26 05:44:28 +08:00
|
|
|
|
|
|
|
if args.is_present("open") {
|
|
|
|
book.build()?;
|
2018-01-07 22:10:48 +08:00
|
|
|
open(book.build_dir_for("html").join("index.html"));
|
2017-06-26 05:44:28 +08:00
|
|
|
}
|
|
|
|
|
2017-12-13 09:04:24 +08:00
|
|
|
trigger_on_change(&book, |path, book_dir| {
|
2018-01-07 22:10:48 +08:00
|
|
|
info!("File changed: {:?}\nBuilding book...\n", path);
|
|
|
|
let result = MDBook::load(&book_dir).and_then(|b| b.build());
|
2017-12-13 09:04:24 +08:00
|
|
|
|
|
|
|
if let Err(e) = result {
|
2018-01-07 22:10:48 +08:00
|
|
|
error!("Unable to build the book");
|
|
|
|
utils::log_backtrace(&e);
|
2017-06-26 05:44:28 +08:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2017-12-13 09:04:24 +08:00
|
|
|
/// Calls the closure when a book source file is changed, blocking indefinitely.
|
|
|
|
pub fn trigger_on_change<F>(book: &MDBook, closure: F)
|
2017-10-03 19:40:23 +08:00
|
|
|
where
|
2017-12-13 09:04:24 +08:00
|
|
|
F: Fn(&Path, &Path),
|
2017-06-26 05:44:28 +08:00
|
|
|
{
|
|
|
|
use self::notify::DebouncedEvent::*;
|
2018-07-24 01:45:01 +08:00
|
|
|
use self::notify::RecursiveMode::*;
|
2017-06-26 05:44:28 +08:00
|
|
|
|
|
|
|
// Create a channel to receive the events.
|
|
|
|
let (tx, rx) = channel();
|
|
|
|
|
|
|
|
let mut watcher = match notify::watcher(tx, Duration::from_secs(1)) {
|
|
|
|
Ok(w) => w,
|
|
|
|
Err(e) => {
|
2018-01-07 22:10:48 +08:00
|
|
|
error!("Error while trying to watch the files:\n\n\t{:?}", e);
|
2017-12-13 09:04:24 +08:00
|
|
|
::std::process::exit(1)
|
2017-10-03 19:40:23 +08:00
|
|
|
}
|
2017-06-26 05:44:28 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
// Add the source directory to the watcher
|
2017-12-11 08:24:43 +08:00
|
|
|
if let Err(e) = watcher.watch(book.source_dir(), Recursive) {
|
2018-01-07 22:10:48 +08:00
|
|
|
error!("Error while watching {:?}:\n {:?}", book.source_dir(), e);
|
2017-12-13 09:04:24 +08:00
|
|
|
::std::process::exit(1);
|
2017-06-26 05:44:28 +08:00
|
|
|
};
|
|
|
|
|
2017-12-13 09:04:24 +08:00
|
|
|
let _ = watcher.watch(book.theme_dir(), Recursive);
|
2017-06-26 05:44:28 +08:00
|
|
|
|
2017-12-13 09:04:24 +08:00
|
|
|
// Add the book.toml file to the watcher if it exists
|
|
|
|
let _ = watcher.watch(book.root.join("book.toml"), NonRecursive);
|
2017-06-26 05:44:28 +08:00
|
|
|
|
2018-01-07 22:10:48 +08:00
|
|
|
info!("Listening for changes...");
|
2017-06-26 05:44:28 +08:00
|
|
|
|
2018-01-03 19:32:49 +08:00
|
|
|
for event in rx.iter() {
|
2018-01-07 22:10:48 +08:00
|
|
|
debug!("Received filesystem event: {:?}", event);
|
2017-12-13 09:04:24 +08:00
|
|
|
match event {
|
|
|
|
Create(path) | Write(path) | Remove(path) | Rename(_, path) => {
|
|
|
|
closure(&path, &book.root);
|
2017-10-03 19:40:23 +08:00
|
|
|
}
|
2017-12-13 09:04:24 +08:00
|
|
|
_ => {}
|
2017-06-26 05:44:28 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|