2017-06-26 05:44:28 +08:00
|
|
|
extern crate notify;
|
|
|
|
|
2017-09-30 21:44:25 +08:00
|
|
|
use std::path::{Path, PathBuf};
|
2017-06-26 05:44:28 +08:00
|
|
|
use self::notify::Watcher;
|
|
|
|
use std::time::Duration;
|
|
|
|
use std::sync::mpsc::channel;
|
2017-10-03 19:40:23 +08:00
|
|
|
use clap::{App, ArgMatches, SubCommand};
|
2017-06-26 05:44:28 +08:00
|
|
|
use mdbook::MDBook;
|
2017-06-27 05:17:46 +08:00
|
|
|
use mdbook::errors::Result;
|
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(
|
|
|
|
"-d, --dest-dir=[dest-dir] 'The output directory for \
|
|
|
|
your book{n}(Defaults to ./book when omitted)'",
|
|
|
|
)
|
|
|
|
.arg_from_usage(
|
|
|
|
"[dir] 'A directory for your book{n}(Defaults to \
|
|
|
|
Current Directory when omitted)'",
|
|
|
|
)
|
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);
|
2017-11-18 20:41:04 +08:00
|
|
|
let mut book = MDBook::load(&book_dir)?;
|
2017-06-26 05:44:28 +08:00
|
|
|
|
2017-09-30 21:44:25 +08:00
|
|
|
if let Some(dest_dir) = args.value_of("dest-dir") {
|
2017-11-30 23:26:30 +08:00
|
|
|
book.config.build.build_dir = PathBuf::from(dest_dir);
|
2017-06-26 05:44:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if args.is_present("open") {
|
|
|
|
book.build()?;
|
2017-06-27 20:01:33 +08:00
|
|
|
open(book.get_destination().join("index.html"));
|
2017-06-26 05:44:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
trigger_on_change(&mut book, |path, book| {
|
|
|
|
println!("File changed: {:?}\nBuilding book...\n", path);
|
|
|
|
if let Err(e) = book.build() {
|
|
|
|
println!("Error while building: {:?}", e);
|
|
|
|
}
|
|
|
|
println!("");
|
|
|
|
});
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
// Calls the closure when a book source file is changed. This is blocking!
|
|
|
|
pub fn trigger_on_change<F>(book: &mut MDBook, closure: F) -> ()
|
2017-10-03 19:40:23 +08:00
|
|
|
where
|
|
|
|
F: Fn(&Path, &mut MDBook) -> (),
|
2017-06-26 05:44:28 +08:00
|
|
|
{
|
|
|
|
use self::notify::RecursiveMode::*;
|
|
|
|
use self::notify::DebouncedEvent::*;
|
|
|
|
|
|
|
|
// 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) => {
|
|
|
|
println!("Error while trying to watch the files:\n\n\t{:?}", e);
|
2017-10-08 00:11:05 +08:00
|
|
|
::std::process::exit(0)
|
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) {
|
|
|
|
println!("Error while watching {:?}:\n {:?}", book.source_dir(), e);
|
2017-06-26 05:44:28 +08:00
|
|
|
::std::process::exit(0);
|
|
|
|
};
|
|
|
|
|
|
|
|
// Add the theme directory to the watcher
|
2017-10-04 19:55:21 +08:00
|
|
|
watcher.watch(book.theme_dir(), Recursive)
|
2017-10-03 19:40:23 +08:00
|
|
|
.unwrap_or_default();
|
2017-06-26 05:44:28 +08:00
|
|
|
|
|
|
|
// Add the book.{json,toml} file to the watcher if it exists, because it's not
|
|
|
|
// located in the source directory
|
2017-10-04 19:55:21 +08:00
|
|
|
if watcher.watch(book.root.join("book.json"), NonRecursive)
|
2017-10-03 19:40:23 +08:00
|
|
|
.is_err()
|
|
|
|
{
|
2017-06-26 05:44:28 +08:00
|
|
|
// do nothing if book.json is not found
|
|
|
|
}
|
2017-10-04 19:55:21 +08:00
|
|
|
if watcher.watch(book.root.join("book.toml"), NonRecursive)
|
2017-10-03 19:40:23 +08:00
|
|
|
.is_err()
|
|
|
|
{
|
2017-06-26 05:44:28 +08:00
|
|
|
// do nothing if book.toml is not found
|
|
|
|
}
|
|
|
|
|
|
|
|
println!("\nListening for changes...\n");
|
|
|
|
|
|
|
|
loop {
|
|
|
|
match rx.recv() {
|
|
|
|
Ok(event) => {
|
|
|
|
match event {
|
2017-10-03 19:40:23 +08:00
|
|
|
Create(path) | Write(path) | Remove(path) | Rename(_, path) => {
|
2017-06-26 05:44:28 +08:00
|
|
|
closure(&path, book);
|
2017-10-03 19:40:23 +08:00
|
|
|
}
|
|
|
|
_ => {}
|
2017-06-26 05:44:28 +08:00
|
|
|
}
|
2017-10-03 19:40:23 +08:00
|
|
|
}
|
2017-06-26 05:44:28 +08:00
|
|
|
Err(e) => {
|
|
|
|
println!("An error occured: {:?}", e);
|
2017-10-03 19:40:23 +08:00
|
|
|
}
|
2017-06-26 05:44:28 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|