2022-07-04 23:16:31 +08:00
|
|
|
use super::command_prelude::*;
|
2019-05-26 01:50:41 +07:00
|
|
|
use crate::{get_book_dir, open};
|
2017-06-26 23:17:46 +02:00
|
|
|
use mdbook::errors::Result;
|
2018-07-23 12:45:01 -05:00
|
|
|
use mdbook::MDBook;
|
2019-01-31 16:31:02 -02:00
|
|
|
use std::path::{Path, PathBuf};
|
2024-02-24 13:35:39 -08:00
|
|
|
|
|
|
|
|
mod native;
|
|
|
|
|
mod poller;
|
2017-06-25 23:44:28 +02:00
|
|
|
|
2017-06-27 07:59:50 +02:00
|
|
|
// Create clap subcommand arguments
|
2022-07-04 23:16:31 +08:00
|
|
|
pub fn make_subcommand() -> Command {
|
|
|
|
|
Command::new("watch")
|
2018-08-02 15:48:22 -05: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()
|
2024-02-24 13:35:39 -08:00
|
|
|
.arg_watcher()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub enum WatcherKind {
|
|
|
|
|
Poll,
|
|
|
|
|
Native,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl WatcherKind {
|
|
|
|
|
pub fn from_str(s: &str) -> WatcherKind {
|
|
|
|
|
match s {
|
|
|
|
|
"poll" => WatcherKind::Poll,
|
|
|
|
|
"native" => WatcherKind::Native,
|
|
|
|
|
_ => panic!("unsupported watcher {s}"),
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-06-27 07:59:50 +02:00
|
|
|
}
|
|
|
|
|
|
2017-06-25 23:44:28 +02:00
|
|
|
// Watch command implementation
|
2017-06-27 07:59:50 +02:00
|
|
|
pub fn execute(args: &ArgMatches) -> Result<()> {
|
2017-06-25 23:44:28 +02:00
|
|
|
let book_dir = get_book_dir(args);
|
2024-02-24 13:35:39 -08:00
|
|
|
let mut book = MDBook::load(&book_dir)?;
|
2020-05-17 11:04:29 -07: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-17 11:04:29 -07:00
|
|
|
book.config.build.build_dir = dest_dir.into();
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
update_config(&mut book);
|
2017-06-25 23:44:28 +02:00
|
|
|
|
2022-07-04 23:16:31 +08:00
|
|
|
if args.get_flag("open") {
|
2017-06-25 23:44:28 +02:00
|
|
|
book.build()?;
|
2022-06-23 00:05:56 +02: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-10 11:19:23 -07:00
|
|
|
}
|
2022-06-23 00:05:56 +02:00
|
|
|
open(path);
|
2017-06-25 23:44:28 +02:00
|
|
|
}
|
|
|
|
|
|
2024-02-24 13:35:39 -08:00
|
|
|
let watcher = WatcherKind::from_str(args.get_one::<String>("watcher").unwrap());
|
|
|
|
|
rebuild_on_change(watcher, &book_dir, &update_config, &|| {});
|
2017-06-25 23:44:28 +02:00
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-24 13:35:39 -08:00
|
|
|
pub fn rebuild_on_change(
|
|
|
|
|
kind: WatcherKind,
|
|
|
|
|
book_dir: &Path,
|
|
|
|
|
update_config: &dyn Fn(&mut MDBook),
|
|
|
|
|
post_build: &dyn Fn(),
|
|
|
|
|
) {
|
|
|
|
|
match kind {
|
|
|
|
|
WatcherKind::Poll => self::poller::rebuild_on_change(book_dir, update_config, post_build),
|
|
|
|
|
WatcherKind::Native => self::native::rebuild_on_change(book_dir, update_config, post_build),
|
2019-10-04 14:59:17 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-31 20:27:52 -07:00
|
|
|
fn find_gitignore(book_root: &Path) -> Option<PathBuf> {
|
2019-10-04 22:56:56 +02:00
|
|
|
book_root
|
|
|
|
|
.ancestors()
|
|
|
|
|
.map(|p| p.join(".gitignore"))
|
|
|
|
|
.find(|p| p.exists())
|
|
|
|
|
}
|