Skip to content

Commit

Permalink
feat: add flag to choose backend
Browse files Browse the repository at this point in the history
  • Loading branch information
KFearsoff committed Dec 6, 2023
1 parent f427f24 commit 43464c6
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 32 deletions.
62 changes: 46 additions & 16 deletions src/cmd/serve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ use mdbook::errors::*;
use mdbook::utils;
use mdbook::utils::fs::get_404_output_file;
use mdbook::MDBook;
use notify::PollWatcher;
use notify::RecommendedWatcher;
use std::net::{SocketAddr, ToSocketAddrs};
use std::path::PathBuf;
use tokio::sync::broadcast;
Expand Down Expand Up @@ -43,6 +45,9 @@ pub fn make_subcommand() -> Command {
.help("Port to use for HTTP connections"),
)
.arg_open()
.arg(arg!(--compat "Watch files in compatibility mode.\n\
Use this if your environment doesn't support filesystem events (Windows, Docker, NFS),
or if you encounter issues otherwise"))
}

// Serve command implementation
Expand Down Expand Up @@ -97,23 +102,48 @@ pub fn execute(args: &ArgMatches) -> Result<()> {
}

#[cfg(feature = "watch")]
watch::trigger_on_change(&book, move |paths, book_dir| {
info!("Files changed: {:?}", paths);
info!("Building book...");

// FIXME: This area is really ugly because we need to re-set livereload :(
let result = MDBook::load(book_dir).and_then(|mut b| {
update_config(&mut b);
b.build()
});
let polling = args.get_flag("compat");

if let Err(e) = result {
error!("Unable to load the book");
utils::log_backtrace(&e);
} else {
let _ = tx.send(Message::text("reload"));
}
});
#[cfg(feature = "watch")]
if polling {
debug!("Using PollWatcher backend");
watch::trigger_on_change::<_, PollWatcher>(&book, move |paths, book_dir| {
info!("Files changed: {:?}", paths);
info!("Building book...");

// FIXME: This area is really ugly because we need to re-set livereload :(
let result = MDBook::load(book_dir).and_then(|mut b| {
update_config(&mut b);
b.build()
});

if let Err(e) = result {
error!("Unable to load the book");
utils::log_backtrace(&e);
} else {
let _ = tx.send(Message::text("reload"));
}
});
} else {
debug!("Using RecommendWatcher backend");
watch::trigger_on_change::<_, RecommendedWatcher>(&book, move |paths, book_dir| {
info!("Files changed: {:?}", paths);
info!("Building book...");

// FIXME: This area is really ugly because we need to re-set livereload :(
let result = MDBook::load(book_dir).and_then(|mut b| {
update_config(&mut b);
b.build()
});

if let Err(e) = result {
error!("Unable to load the book");
utils::log_backtrace(&e);
} else {
let _ = tx.send(Message::text("reload"));
}
});
}

let _ = thread_handle.join();

Expand Down
56 changes: 40 additions & 16 deletions src/cmd/watch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ use ignore::gitignore::Gitignore;
use mdbook::errors::Result;
use mdbook::utils;
use mdbook::MDBook;
use notify::PollWatcher;
use notify::RecommendedWatcher;
use notify::Watcher;
use notify_debouncer_mini::Config;
use pathdiff::diff_paths;
use std::path::{Path, PathBuf};
Expand All @@ -18,6 +21,9 @@ pub fn make_subcommand() -> Command {
.arg_dest_dir()
.arg_root_dir()
.arg_open()
.arg(arg!(--compat "Watch files in compatibility mode.\n\
Use this if your environment doesn't support filesystem events (Windows, Docker, NFS),
or if you encounter issues otherwise"))
}

// Watch command implementation
Expand All @@ -42,18 +48,37 @@ pub fn execute(args: &ArgMatches) -> Result<()> {
open(path);
}

trigger_on_change(&book, |paths, book_dir| {
info!("Files changed: {:?}\nBuilding book...\n", paths);
let result = MDBook::load(book_dir).and_then(|mut b| {
update_config(&mut b);
b.build()
});
let polling = args.get_flag("compat");

if let Err(e) = result {
error!("Unable to build the book");
utils::log_backtrace(&e);
}
});
if polling {
debug!("Using PollWatcher backend");
trigger_on_change::<_, PollWatcher>(&book, |paths, book_dir| {
info!("Files changed: {:?}\nBuilding book...\n", paths);
let result = MDBook::load(book_dir).and_then(|mut b| {
update_config(&mut b);
b.build()
});

if let Err(e) = result {
error!("Unable to build the book");
utils::log_backtrace(&e);
}
});
} else {
debug!("Using RecommendWatcher backend");
trigger_on_change::<_, RecommendedWatcher>(&book, |paths, book_dir| {
info!("Files changed: {:?}\nBuilding book...\n", paths);
let result = MDBook::load(book_dir).and_then(|mut b| {
update_config(&mut b);
b.build()
});

if let Err(e) = result {
error!("Unable to build the book");
utils::log_backtrace(&e);
}
});
};

Ok(())
}
Expand Down Expand Up @@ -110,9 +135,10 @@ fn filter_ignored_files(ignore: Gitignore, paths: &[PathBuf]) -> Vec<PathBuf> {
}

/// Calls the closure when a book source file is changed, blocking indefinitely.
pub fn trigger_on_change<F>(book: &MDBook, closure: F)
pub fn trigger_on_change<F, W>(book: &MDBook, closure: F)
where
F: Fn(Vec<PathBuf>, &Path),
W: 'static + Watcher + Send,
{
use notify::RecursiveMode::*;

Expand All @@ -125,10 +151,8 @@ where
.with_timeout(Duration::from_secs(1))
.with_notify_config(backend_config);

let mut debouncer = match notify_debouncer_mini::new_debouncer_opt::<_, notify::PollWatcher>(
debouncer_config,
tx,
) {
let mut debouncer = match notify_debouncer_mini::new_debouncer_opt::<_, W>(debouncer_config, tx)
{
Ok(d) => d,
Err(e) => {
error!("Error while trying to watch the files:\n\n\t{:?}", e);
Expand Down

0 comments on commit 43464c6

Please sign in to comment.