2022-07-04 23:16:31 +08:00
|
|
|
use super::command_prelude::*;
|
2019-05-26 02:50:41 +08:00
|
|
|
use crate::{get_book_dir, open};
|
2023-04-23 03:49:39 +08:00
|
|
|
use ignore::gitignore::Gitignore;
|
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;
|
2019-02-01 02:31:02 +08:00
|
|
|
use std::path::{Path, PathBuf};
|
2018-07-24 01:45:01 +08:00
|
|
|
use std::sync::mpsc::channel;
|
2019-02-01 02:31:02 +08:00
|
|
|
use std::thread::sleep;
|
2019-05-05 22:57:43 +08:00
|
|
|
use std::time::Duration;
|
2017-06-26 05:44:28 +08:00
|
|
|
|
2017-06-27 13:59:50 +08:00
|
|
|
// Create clap subcommand arguments
|
2022-07-04 23:16:31 +08:00
|
|
|
pub fn make_subcommand() -> Command {
|
|
|
|
Command::new("watch")
|
2018-08-03 04:48:22 +08:00
|
|
|
.about("Watches a book's files and rebuilds it on changes")
|
2022-07-04 23:16:31 +08:00
|
|
|
.arg_dest_dir()
|
|
|
|
.arg_root_dir()
|
|
|
|
.arg_open()
|
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);
|
2023-05-14 00:44:11 +08:00
|
|
|
let mut book = MDBook::load(book_dir)?;
|
2020-05-18 02:04:29 +08:00
|
|
|
|
|
|
|
let update_config = |book: &mut MDBook| {
|
2022-07-04 23:16:31 +08:00
|
|
|
if let Some(dest_dir) = args.get_one::<PathBuf>("dest-dir") {
|
2020-05-18 02:04:29 +08:00
|
|
|
book.config.build.build_dir = dest_dir.into();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
update_config(&mut book);
|
2017-06-26 05:44:28 +08:00
|
|
|
|
2022-07-04 23:16:31 +08:00
|
|
|
if args.get_flag("open") {
|
2017-06-26 05:44:28 +08:00
|
|
|
book.build()?;
|
2022-06-23 06:05:56 +08:00
|
|
|
let path = book.build_dir_for("html").join("index.html");
|
|
|
|
if !path.exists() {
|
|
|
|
error!("No chapter available to open");
|
|
|
|
std::process::exit(1)
|
2022-05-11 02:19:23 +08:00
|
|
|
}
|
2022-06-23 06:05:56 +08:00
|
|
|
open(path);
|
2017-06-26 05:44:28 +08:00
|
|
|
}
|
|
|
|
|
2019-02-01 02:31:02 +08:00
|
|
|
trigger_on_change(&book, |paths, book_dir| {
|
|
|
|
info!("Files changed: {:?}\nBuilding book...\n", paths);
|
2023-05-14 00:44:11 +08:00
|
|
|
let result = MDBook::load(book_dir).and_then(|mut b| {
|
2020-05-18 02:04:29 +08:00
|
|
|
update_config(&mut 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(())
|
|
|
|
}
|
|
|
|
|
2021-06-01 11:27:52 +08:00
|
|
|
fn remove_ignored_files(book_root: &Path, paths: &[PathBuf]) -> Vec<PathBuf> {
|
2019-10-04 20:59:17 +08:00
|
|
|
if paths.is_empty() {
|
|
|
|
return vec![];
|
|
|
|
}
|
|
|
|
|
2019-10-05 04:56:56 +08:00
|
|
|
match find_gitignore(book_root) {
|
|
|
|
Some(gitignore_path) => {
|
2023-04-23 03:49:39 +08:00
|
|
|
let (ignore, err) = Gitignore::new(&gitignore_path);
|
|
|
|
if let Some(err) = err {
|
|
|
|
warn!(
|
|
|
|
"error reading gitignore `{}`: {err}",
|
|
|
|
gitignore_path.display()
|
|
|
|
);
|
2019-10-05 04:56:56 +08:00
|
|
|
}
|
2023-04-23 03:49:39 +08:00
|
|
|
filter_ignored_files(ignore, paths)
|
2019-10-05 04:56:56 +08:00
|
|
|
}
|
|
|
|
None => {
|
|
|
|
// There is no .gitignore file.
|
2019-10-04 20:59:17 +08:00
|
|
|
paths.iter().map(|path| path.to_path_buf()).collect()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-01 11:27:52 +08:00
|
|
|
fn find_gitignore(book_root: &Path) -> Option<PathBuf> {
|
2019-10-05 04:56:56 +08:00
|
|
|
book_root
|
|
|
|
.ancestors()
|
|
|
|
.map(|p| p.join(".gitignore"))
|
|
|
|
.find(|p| p.exists())
|
|
|
|
}
|
|
|
|
|
2023-04-23 03:49:39 +08:00
|
|
|
fn filter_ignored_files(ignore: Gitignore, paths: &[PathBuf]) -> Vec<PathBuf> {
|
2019-10-05 04:56:56 +08:00
|
|
|
paths
|
|
|
|
.iter()
|
2023-04-23 03:49:39 +08:00
|
|
|
.filter(|path| {
|
|
|
|
!ignore
|
|
|
|
.matched_path_or_any_parents(path, path.is_dir())
|
|
|
|
.is_ignore()
|
2019-10-05 04:56:56 +08:00
|
|
|
})
|
|
|
|
.map(|path| path.to_path_buf())
|
|
|
|
.collect()
|
|
|
|
}
|
|
|
|
|
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
|
2019-02-01 02:31:02 +08:00
|
|
|
F: Fn(Vec<PathBuf>, &Path),
|
2017-06-26 05:44:28 +08:00
|
|
|
{
|
2019-05-26 02:50:41 +08:00
|
|
|
use notify::RecursiveMode::*;
|
2017-06-26 05:44:28 +08:00
|
|
|
|
|
|
|
// Create a channel to receive the events.
|
|
|
|
let (tx, rx) = channel();
|
|
|
|
|
2022-12-15 06:01:27 +08:00
|
|
|
let mut debouncer = match notify_debouncer_mini::new_debouncer(Duration::from_secs(1), None, tx)
|
|
|
|
{
|
|
|
|
Ok(d) => d,
|
2017-06-26 05:44:28 +08:00
|
|
|
Err(e) => {
|
2018-01-07 22:10:48 +08:00
|
|
|
error!("Error while trying to watch the files:\n\n\t{:?}", e);
|
2019-05-31 00:12:33 +08:00
|
|
|
std::process::exit(1)
|
2017-10-03 19:40:23 +08:00
|
|
|
}
|
2017-06-26 05:44:28 +08:00
|
|
|
};
|
2022-12-15 06:01:27 +08:00
|
|
|
let watcher = debouncer.watcher();
|
2017-06-26 05:44:28 +08:00
|
|
|
|
|
|
|
// Add the source directory to the watcher
|
2022-12-15 06:01:27 +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);
|
2019-05-31 00:12:33 +08:00
|
|
|
std::process::exit(1);
|
2017-06-26 05:44:28 +08:00
|
|
|
};
|
|
|
|
|
2022-12-15 06:01:27 +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
|
2022-12-15 06:01:27 +08:00
|
|
|
let _ = watcher.watch(&book.root.join("book.toml"), NonRecursive);
|
2017-06-26 05:44:28 +08:00
|
|
|
|
2022-10-29 02:13:33 +08:00
|
|
|
for dir in &book.config.build.extra_watch_dirs {
|
2022-11-08 04:20:59 +08:00
|
|
|
let path = dir.canonicalize().unwrap();
|
|
|
|
if let Err(e) = watcher.watch(&path, Recursive) {
|
2022-11-13 06:23:05 +08:00
|
|
|
error!(
|
|
|
|
"Error while watching extra directory {:?}:\n {:?}",
|
|
|
|
path, e
|
|
|
|
);
|
2022-11-08 04:20:59 +08:00
|
|
|
std::process::exit(1);
|
|
|
|
}
|
2022-10-29 02:13:33 +08:00
|
|
|
}
|
|
|
|
|
2018-01-07 22:10:48 +08:00
|
|
|
info!("Listening for changes...");
|
2017-06-26 05:44:28 +08:00
|
|
|
|
2019-02-01 02:31:02 +08:00
|
|
|
loop {
|
|
|
|
let first_event = rx.recv().unwrap();
|
|
|
|
sleep(Duration::from_millis(50));
|
|
|
|
let other_events = rx.try_iter();
|
|
|
|
|
|
|
|
let all_events = std::iter::once(first_event).chain(other_events);
|
|
|
|
|
2022-12-15 06:01:27 +08:00
|
|
|
let paths: Vec<_> = all_events
|
|
|
|
.filter_map(|event| match event {
|
|
|
|
Ok(events) => Some(events),
|
|
|
|
Err(errors) => {
|
|
|
|
for error in errors {
|
|
|
|
log::warn!("error while watching for changes: {error}");
|
|
|
|
}
|
|
|
|
None
|
2019-02-01 02:31:02 +08:00
|
|
|
}
|
|
|
|
})
|
2022-12-15 06:01:27 +08:00
|
|
|
.flatten()
|
|
|
|
.map(|event| event.path)
|
|
|
|
.collect();
|
2019-02-01 02:31:02 +08:00
|
|
|
|
2022-11-08 04:20:59 +08:00
|
|
|
// If we are watching files outside the current repository (via extra-watch-dirs), then they are definitionally
|
|
|
|
// ignored by gitignore. So we handle this case by including such files into the watched paths list.
|
|
|
|
let any_external_paths = paths.iter().filter(|p| !p.starts_with(&book.root)).cloned();
|
|
|
|
let mut paths = remove_ignored_files(&book.root, &paths[..]);
|
|
|
|
paths.extend(any_external_paths);
|
2019-10-04 20:59:17 +08:00
|
|
|
|
|
|
|
if !paths.is_empty() {
|
|
|
|
closure(paths, &book.root);
|
|
|
|
}
|
2017-06-26 05:44:28 +08:00
|
|
|
}
|
|
|
|
}
|