Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for watching additional directories #1884

Merged
merged 5 commits into from
Nov 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions guide/src/format/configuration/general.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ This controls the build process of your book.
build-dir = "book" # the directory where the output is placed
create-missing = true # whether or not to create missing pages
use-default-preprocessors = true # use the default preprocessors
extra-watch-dirs = [] # directories to watch for triggering builds
```

- **build-dir:** The directory to put the rendered book in. By default this is
Expand All @@ -108,3 +109,6 @@ use-default-preprocessors = true # use the default preprocessors
default preprocessors from running.
- Adding `[preprocessor.links]`, for example, will ensure, regardless of
`use-default-preprocessors` that `links` it will run.
- **extra-watch-dirs**: A list of paths to directories that will be watched in
the `watch` and `serve` commands. Changes to files under these directories will
trigger rebuilds. Useful if your book depends on files outside its `src` directory.
17 changes: 16 additions & 1 deletion src/cmd/watch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,17 @@ where
// Add the book.toml file to the watcher if it exists
let _ = watcher.watch(book.root.join("book.toml"), NonRecursive);

for dir in &book.config.build.extra_watch_dirs {
let path = dir.canonicalize().unwrap();
if let Err(e) = watcher.watch(&path, Recursive) {
error!(
"Error while watching extra directory {:?}:\n {:?}",
path, e
);
std::process::exit(1);
}
}

info!("Listening for changes...");

loop {
Expand All @@ -166,7 +177,11 @@ where
})
.collect::<Vec<_>>();

let paths = remove_ignored_files(&book.root, &paths[..]);
// 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);

if !paths.is_empty() {
closure(paths, &book.root);
Expand Down
5 changes: 5 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -437,6 +437,8 @@ pub struct BuildConfig {
/// Should the default preprocessors always be used when they are
/// compatible with the renderer?
pub use_default_preprocessors: bool,
/// Extra directories to trigger rebuild when watching/serving
pub extra_watch_dirs: Vec<PathBuf>,
}

impl Default for BuildConfig {
Expand All @@ -445,6 +447,7 @@ impl Default for BuildConfig {
build_dir: PathBuf::from("book"),
create_missing: true,
use_default_preprocessors: true,
extra_watch_dirs: Vec::new(),
}
}
}
Expand Down Expand Up @@ -770,6 +773,7 @@ mod tests {
build_dir: PathBuf::from("outputs"),
create_missing: false,
use_default_preprocessors: true,
extra_watch_dirs: Vec::new(),
};
let rust_should_be = RustConfig { edition: None };
let playground_should_be = Playground {
Expand Down Expand Up @@ -980,6 +984,7 @@ mod tests {
build_dir: PathBuf::from("my-book"),
create_missing: true,
use_default_preprocessors: true,
extra_watch_dirs: Vec::new(),
};

let html_should_be = HtmlConfig {
Expand Down
2 changes: 1 addition & 1 deletion tests/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ fn run_mdbook_init_with_custom_book_and_src_locations() {
let contents = fs::read_to_string(temp.path().join("book.toml")).unwrap();
assert_eq!(
contents,
"[book]\nauthors = []\nlanguage = \"en\"\nmultilingual = false\nsrc = \"in\"\n\n[build]\nbuild-dir = \"out\"\ncreate-missing = true\nuse-default-preprocessors = true\n"
"[book]\nauthors = []\nlanguage = \"en\"\nmultilingual = false\nsrc = \"in\"\n\n[build]\nbuild-dir = \"out\"\ncreate-missing = true\nextra-watch-dirs = []\nuse-default-preprocessors = true\n"
);
}

Expand Down