From c39564f8fd0cf1760a3ebb9280daab3db002b370 Mon Sep 17 00:00:00 2001 From: Konstantin Podsvirov Date: Sun, 16 Oct 2022 11:07:45 +0300 Subject: [PATCH] Prefere .bookignore when build too Reuse watch command experience and process .bookignore (as .gitignore) if prezent. --- src/cmd/watch.rs | 48 +------------------------------------------- src/utils/fs.rs | 9 +++++++++ src/utils/ignore.rs | 49 +++++++++++++++++++++++++++++++++++++++++++++ src/utils/mod.rs | 1 + 4 files changed, 60 insertions(+), 47 deletions(-) create mode 100644 src/utils/ignore.rs diff --git a/src/cmd/watch.rs b/src/cmd/watch.rs index 68921810..8e1a4f0b 100644 --- a/src/cmd/watch.rs +++ b/src/cmd/watch.rs @@ -1,3 +1,4 @@ +use crate::utils::ignore::remove_ignored_files; use crate::{get_book_dir, open}; use clap::{arg, App, Arg, ArgMatches}; use mdbook::errors::Result; @@ -69,53 +70,6 @@ pub fn execute(args: &ArgMatches) -> Result<()> { Ok(()) } -fn remove_ignored_files(book_root: &Path, paths: &[PathBuf]) -> Vec { - 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 { - book_root - .ancestors() - .map(|p| p.join(".gitignore")) - .find(|p| p.exists()) -} - -fn filter_ignored_files(exclusion_checker: gitignore::File, paths: &[PathBuf]) -> Vec { - 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. pub fn trigger_on_change(book: &MDBook, closure: F) where diff --git a/src/utils/fs.rs b/src/utils/fs.rs index 0d6f3837..f0ef4ef6 100644 --- a/src/utils/fs.rs +++ b/src/utils/fs.rs @@ -1,4 +1,5 @@ use crate::errors::*; +use crate::utils::ignore::remove_ignored_files; use log::{debug, trace}; use std::convert::Into; use std::fs::{self, File}; @@ -111,6 +112,14 @@ pub fn copy_files_except_ext( for entry in fs::read_dir(from)? { 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 .path() .metadata() diff --git a/src/utils/ignore.rs b/src/utils/ignore.rs new file mode 100644 index 00000000..085cccf3 --- /dev/null +++ b/src/utils/ignore.rs @@ -0,0 +1,49 @@ +use log::warn; +use std::path::{Path, PathBuf}; + +pub fn remove_ignored_files(book_root: &Path, paths: &[PathBuf]) -> Vec { + 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 { + 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 { + 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() +} diff --git a/src/utils/mod.rs b/src/utils/mod.rs index 9f67deda..d72dcbdc 100644 --- a/src/utils/mod.rs +++ b/src/utils/mod.rs @@ -1,6 +1,7 @@ #![allow(missing_docs)] // FIXME: Document this pub mod fs; +pub mod ignore; mod string; pub(crate) mod toml_ext; use crate::errors::Error;