2024-08-14 19:09:13 +01:00
|
|
|
use super::command_prelude::*;
|
|
|
|
|
use crate::{get_book_dir, open};
|
|
|
|
|
use mdbook::errors::Result;
|
|
|
|
|
use mdbook::MDBook;
|
|
|
|
|
use std::path::{Path, PathBuf};
|
2024-10-01 22:41:37 +01:00
|
|
|
|
|
|
|
|
mod native;
|
|
|
|
|
mod poller;
|
2024-08-14 19:09:13 +01:00
|
|
|
|
|
|
|
|
// Create clap subcommand arguments
|
|
|
|
|
pub fn make_subcommand() -> Command {
|
|
|
|
|
Command::new("watch")
|
|
|
|
|
.about("Watches a book's files and rebuilds it on changes")
|
|
|
|
|
.arg_dest_dir()
|
|
|
|
|
.arg_root_dir()
|
|
|
|
|
.arg_open()
|
2024-10-01 22:41:37 +01: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}"),
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-08-14 19:09:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Watch command implementation
|
|
|
|
|
pub fn execute(args: &ArgMatches) -> Result<()> {
|
|
|
|
|
let book_dir = get_book_dir(args);
|
2024-10-01 22:41:37 +01:00
|
|
|
let mut book = MDBook::load(&book_dir)?;
|
2024-08-14 19:09:13 +01:00
|
|
|
|
|
|
|
|
let update_config = |book: &mut MDBook| {
|
|
|
|
|
if let Some(dest_dir) = args.get_one::<PathBuf>("dest-dir") {
|
|
|
|
|
book.config.build.build_dir = dest_dir.into();
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
update_config(&mut book);
|
|
|
|
|
|
|
|
|
|
if args.get_flag("open") {
|
|
|
|
|
book.build()?;
|
|
|
|
|
let path = book.build_dir_for("html").join("index.html");
|
|
|
|
|
if !path.exists() {
|
|
|
|
|
error!("No chapter available to open");
|
|
|
|
|
std::process::exit(1)
|
|
|
|
|
}
|
|
|
|
|
open(path);
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-01 22:41:37 +01:00
|
|
|
let watcher = WatcherKind::from_str(args.get_one::<String>("watcher").unwrap());
|
|
|
|
|
rebuild_on_change(watcher, &book_dir, &update_config, &|| {});
|
2024-08-14 19:09:13 +01:00
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-01 22:41:37 +01: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),
|
2024-08-14 19:09:13 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn find_gitignore(book_root: &Path) -> Option<PathBuf> {
|
|
|
|
|
book_root
|
|
|
|
|
.ancestors()
|
|
|
|
|
.map(|p| p.join(".gitignore"))
|
|
|
|
|
.find(|p| p.exists())
|
|
|
|
|
}
|