Prefere .bookignore when build too
Reuse watch command experience and process .bookignore (as .gitignore) if prezent.
This commit is contained in:
parent
678b469835
commit
c39564f8fd
|
@ -1,3 +1,4 @@
|
||||||
|
use crate::utils::ignore::remove_ignored_files;
|
||||||
use crate::{get_book_dir, open};
|
use crate::{get_book_dir, open};
|
||||||
use clap::{arg, App, Arg, ArgMatches};
|
use clap::{arg, App, Arg, ArgMatches};
|
||||||
use mdbook::errors::Result;
|
use mdbook::errors::Result;
|
||||||
|
@ -69,53 +70,6 @@ pub fn execute(args: &ArgMatches) -> Result<()> {
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn remove_ignored_files(book_root: &Path, paths: &[PathBuf]) -> Vec<PathBuf> {
|
|
||||||
if paths.is_empty() {
|
|
||||||
return vec![];
|
|
||||||
}
|
|
||||||
|
|
||||||
match find_gitignore(book_root) {
|
|
||||||
Some(gitignore_path) => {
|
|
||||||
match gitignore::File::new(gitignore_path.as_path()) {
|
|
||||||
Ok(exclusion_checker) => filter_ignored_files(exclusion_checker, paths),
|
|
||||||
Err(_) => {
|
|
||||||
// We're unable to read the .gitignore file, so we'll silently allow everything.
|
|
||||||
// Please see discussion: https://github.com/rust-lang/mdBook/pull/1051
|
|
||||||
paths.iter().map(|path| path.to_path_buf()).collect()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
None => {
|
|
||||||
// There is no .gitignore file.
|
|
||||||
paths.iter().map(|path| path.to_path_buf()).collect()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn find_gitignore(book_root: &Path) -> Option<PathBuf> {
|
|
||||||
book_root
|
|
||||||
.ancestors()
|
|
||||||
.map(|p| p.join(".gitignore"))
|
|
||||||
.find(|p| p.exists())
|
|
||||||
}
|
|
||||||
|
|
||||||
fn filter_ignored_files(exclusion_checker: gitignore::File, paths: &[PathBuf]) -> Vec<PathBuf> {
|
|
||||||
paths
|
|
||||||
.iter()
|
|
||||||
.filter(|path| match exclusion_checker.is_excluded(path) {
|
|
||||||
Ok(exclude) => !exclude,
|
|
||||||
Err(error) => {
|
|
||||||
warn!(
|
|
||||||
"Unable to determine if {:?} is excluded: {:?}. Including it.",
|
|
||||||
&path, error
|
|
||||||
);
|
|
||||||
true
|
|
||||||
}
|
|
||||||
})
|
|
||||||
.map(|path| path.to_path_buf())
|
|
||||||
.collect()
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Calls the closure when a book source file is changed, blocking indefinitely.
|
/// 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>(book: &MDBook, closure: F)
|
||||||
where
|
where
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
use crate::errors::*;
|
use crate::errors::*;
|
||||||
|
use crate::utils::ignore::remove_ignored_files;
|
||||||
use log::{debug, trace};
|
use log::{debug, trace};
|
||||||
use std::convert::Into;
|
use std::convert::Into;
|
||||||
use std::fs::{self, File};
|
use std::fs::{self, File};
|
||||||
|
@ -111,6 +112,14 @@ pub fn copy_files_except_ext(
|
||||||
|
|
||||||
for entry in fs::read_dir(from)? {
|
for entry in fs::read_dir(from)? {
|
||||||
let entry = entry?;
|
let entry = entry?;
|
||||||
|
|
||||||
|
// Check if entry is ignored
|
||||||
|
let paths = vec![entry.path()];
|
||||||
|
let paths = remove_ignored_files(from, &paths[..]);
|
||||||
|
if paths.is_empty() {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
let metadata = entry
|
let metadata = entry
|
||||||
.path()
|
.path()
|
||||||
.metadata()
|
.metadata()
|
||||||
|
|
|
@ -0,0 +1,49 @@
|
||||||
|
use log::warn;
|
||||||
|
use std::path::{Path, PathBuf};
|
||||||
|
|
||||||
|
pub fn remove_ignored_files(book_root: &Path, paths: &[PathBuf]) -> Vec<PathBuf> {
|
||||||
|
if paths.is_empty() {
|
||||||
|
return vec![];
|
||||||
|
}
|
||||||
|
|
||||||
|
match find_ignorefile(book_root) {
|
||||||
|
Some(gitignore_path) => {
|
||||||
|
match gitignore::File::new(gitignore_path.as_path()) {
|
||||||
|
Ok(exclusion_checker) => filter_ignored_files(exclusion_checker, paths),
|
||||||
|
Err(_) => {
|
||||||
|
// We're unable to read the .gitignore file, so we'll silently allow everything.
|
||||||
|
// Please see discussion: https://github.com/rust-lang/mdBook/pull/1051
|
||||||
|
paths.iter().map(|path| path.to_path_buf()).collect()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
None => {
|
||||||
|
// There is no .gitignore file.
|
||||||
|
paths.iter().map(|path| path.to_path_buf()).collect()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn find_ignorefile(book_root: &Path) -> Option<PathBuf> {
|
||||||
|
book_root
|
||||||
|
.ancestors()
|
||||||
|
.flat_map(|p| vec![p.join(".bookignore"), p.join(".gitignore")].into_iter())
|
||||||
|
.find(|p| p.exists())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn filter_ignored_files(exclusion_checker: gitignore::File<'_>, paths: &[PathBuf]) -> Vec<PathBuf> {
|
||||||
|
paths
|
||||||
|
.iter()
|
||||||
|
.filter(|path| match exclusion_checker.is_excluded(path) {
|
||||||
|
Ok(exclude) => !exclude,
|
||||||
|
Err(error) => {
|
||||||
|
warn!(
|
||||||
|
"Unable to determine if {:?} is excluded: {:?}. Including it.",
|
||||||
|
&path, error
|
||||||
|
);
|
||||||
|
true
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.map(|path| path.to_path_buf())
|
||||||
|
.collect()
|
||||||
|
}
|
|
@ -1,6 +1,7 @@
|
||||||
#![allow(missing_docs)] // FIXME: Document this
|
#![allow(missing_docs)] // FIXME: Document this
|
||||||
|
|
||||||
pub mod fs;
|
pub mod fs;
|
||||||
|
pub mod ignore;
|
||||||
mod string;
|
mod string;
|
||||||
pub(crate) mod toml_ext;
|
pub(crate) mod toml_ext;
|
||||||
use crate::errors::Error;
|
use crate::errors::Error;
|
||||||
|
|
Loading…
Reference in New Issue