Skip to content

Commit 666975a

Browse files
authored
Merge pull request #1884 from willcrichton/master
Add support for watching additional directories
2 parents 3a24f10 + 144a1e4 commit 666975a

File tree

4 files changed

+26
-2
lines changed

4 files changed

+26
-2
lines changed

guide/src/format/configuration/general.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ This controls the build process of your book.
8787
build-dir = "book" # the directory where the output is placed
8888
create-missing = true # whether or not to create missing pages
8989
use-default-preprocessors = true # use the default preprocessors
90+
extra-watch-dirs = [] # directories to watch for triggering builds
9091
```
9192

9293
- **build-dir:** The directory to put the rendered book in. By default this is
@@ -108,3 +109,6 @@ use-default-preprocessors = true # use the default preprocessors
108109
default preprocessors from running.
109110
- Adding `[preprocessor.links]`, for example, will ensure, regardless of
110111
`use-default-preprocessors` that `links` it will run.
112+
- **extra-watch-dirs**: A list of paths to directories that will be watched in
113+
the `watch` and `serve` commands. Changes to files under these directories will
114+
trigger rebuilds. Useful if your book depends on files outside its `src` directory.

src/cmd/watch.rs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,17 @@ where
146146
// Add the book.toml file to the watcher if it exists
147147
let _ = watcher.watch(book.root.join("book.toml"), NonRecursive);
148148

149+
for dir in &book.config.build.extra_watch_dirs {
150+
let path = dir.canonicalize().unwrap();
151+
if let Err(e) = watcher.watch(&path, Recursive) {
152+
error!(
153+
"Error while watching extra directory {:?}:\n {:?}",
154+
path, e
155+
);
156+
std::process::exit(1);
157+
}
158+
}
159+
149160
info!("Listening for changes...");
150161

151162
loop {
@@ -166,7 +177,11 @@ where
166177
})
167178
.collect::<Vec<_>>();
168179

169-
let paths = remove_ignored_files(&book.root, &paths[..]);
180+
// If we are watching files outside the current repository (via extra-watch-dirs), then they are definitionally
181+
// ignored by gitignore. So we handle this case by including such files into the watched paths list.
182+
let any_external_paths = paths.iter().filter(|p| !p.starts_with(&book.root)).cloned();
183+
let mut paths = remove_ignored_files(&book.root, &paths[..]);
184+
paths.extend(any_external_paths);
170185

171186
if !paths.is_empty() {
172187
closure(paths, &book.root);

src/config.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -438,6 +438,8 @@ pub struct BuildConfig {
438438
/// Should the default preprocessors always be used when they are
439439
/// compatible with the renderer?
440440
pub use_default_preprocessors: bool,
441+
/// Extra directories to trigger rebuild when watching/serving
442+
pub extra_watch_dirs: Vec<PathBuf>,
441443
}
442444

443445
impl Default for BuildConfig {
@@ -446,6 +448,7 @@ impl Default for BuildConfig {
446448
build_dir: PathBuf::from("book"),
447449
create_missing: true,
448450
use_default_preprocessors: true,
451+
extra_watch_dirs: Vec::new(),
449452
}
450453
}
451454
}
@@ -772,6 +775,7 @@ mod tests {
772775
build_dir: PathBuf::from("outputs"),
773776
create_missing: false,
774777
use_default_preprocessors: true,
778+
extra_watch_dirs: Vec::new(),
775779
};
776780
let rust_should_be = RustConfig { edition: None };
777781
let playground_should_be = Playground {
@@ -982,6 +986,7 @@ mod tests {
982986
build_dir: PathBuf::from("my-book"),
983987
create_missing: true,
984988
use_default_preprocessors: true,
989+
extra_watch_dirs: Vec::new(),
985990
};
986991

987992
let html_should_be = HtmlConfig {

tests/init.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ fn run_mdbook_init_with_custom_book_and_src_locations() {
9595
let contents = fs::read_to_string(temp.path().join("book.toml")).unwrap();
9696
assert_eq!(
9797
contents,
98-
"[book]\nauthors = []\nlanguage = \"en\"\nmultilingual = false\nsrc = \"in\"\n\n[build]\nbuild-dir = \"out\"\ncreate-missing = true\nuse-default-preprocessors = true\n"
98+
"[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"
9999
);
100100
}
101101

0 commit comments

Comments
 (0)