From 2385a9be6c73fb0a99b996eef49c6d3a591fa4b4 Mon Sep 17 00:00:00 2001 From: Bergmann89 Date: Wed, 24 Jul 2024 15:49:41 +0200 Subject: [PATCH] Implement `copy_files_except_ignore` to not break the public API --- src/book/mod.rs | 2 +- src/renderer/html_handlebars/hbs_renderer.rs | 2 +- src/utils/fs.rs | 41 ++++++++++++-------- 3 files changed, 26 insertions(+), 19 deletions(-) diff --git a/src/book/mod.rs b/src/book/mod.rs index 9a384dead6..8b277f7e45 100644 --- a/src/book/mod.rs +++ b/src/book/mod.rs @@ -906,7 +906,7 @@ mod tests { let temp_dir = TempFileBuilder::new().prefix("mdbook-").tempdir().unwrap(); let test_book_dir = PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("test_book"); - utils::fs::copy_files_except_ext(&test_book_dir, temp_dir.path(), true, None, None) + utils::fs::copy_files_except_ignored(&test_book_dir, temp_dir.path(), true, None, None) .expect("Error while copying test book to temp dir"); let book = MDBook::load(temp_dir.path()).expect("Unable to load book"); diff --git a/src/renderer/html_handlebars/hbs_renderer.rs b/src/renderer/html_handlebars/hbs_renderer.rs index 219f1293b8..ff776d62dd 100644 --- a/src/renderer/html_handlebars/hbs_renderer.rs +++ b/src/renderer/html_handlebars/hbs_renderer.rs @@ -613,7 +613,7 @@ impl Renderer for HtmlHandlebars { builder.add_line(None, "*.md")?; let ignore = builder.build()?; - utils::fs::copy_files_except_ext( + utils::fs::copy_files_except_ignored( &src_dir, destination, true, diff --git a/src/utils/fs.rs b/src/utils/fs.rs index 964f6bafab..5dadc02ad7 100644 --- a/src/utils/fs.rs +++ b/src/utils/fs.rs @@ -1,5 +1,5 @@ use crate::errors::*; -use ignore::gitignore::Gitignore; +use ignore::gitignore::{Gitignore, GitignoreBuilder}; use log::{debug, trace}; use std::fs::{self, File}; use std::io::Write; @@ -87,6 +87,24 @@ pub fn remove_dir_content(dir: &Path) -> Result<()> { /// Copies all files of a directory to another one except the files /// with the extensions given in the `ext_blacklist` array pub fn copy_files_except_ext( + from: &Path, + to: &Path, + recursive: bool, + avoid_dir: Option<&PathBuf>, + ext_blacklist: &[&str], +) -> Result<()> { + let mut builder = GitignoreBuilder::new(from); + for ext in ext_blacklist { + builder.add_line(None, &format!("*.{ext}"))?; + } + let ignore = builder.build()?; + + copy_files_except_ignored(from, to, recursive, avoid_dir, Some(&ignore)) +} + +/// Copies all files of a directory to another one except the files that are +/// ignored by the passed [`Gitignore`] +pub fn copy_files_except_ignored( from: &Path, to: &Path, recursive: bool, @@ -118,7 +136,7 @@ pub fn copy_files_except_ext( // Check if it is in the blacklist if let Some(ignore) = ignore { let path = entry.as_path(); - if ignore.matched(&path, path.is_dir()).is_ignore() { + if ignore.matched(path, path.is_dir()).is_ignore() { continue; } } @@ -147,7 +165,7 @@ pub fn copy_files_except_ext( fs::create_dir(&target_file_path)?; } - copy_files_except_ext(&entry, &target_file_path, true, avoid_dir, ignore)?; + copy_files_except_ignored(&entry, &target_file_path, true, avoid_dir, ignore)?; } else if metadata.is_file() { debug!("Copying {entry:?} to {target_file_path:?}"); copy(&entry, &target_file_path)?; @@ -222,7 +240,6 @@ pub fn get_404_output_file(input_404: &Option) -> String { #[cfg(test)] mod tests { use super::copy_files_except_ext; - use ignore::gitignore::GitignoreBuilder; use std::{fs, io::Result, path::Path}; #[cfg(target_os = "windows")] @@ -276,19 +293,9 @@ mod tests { panic!("Could not create output/sub_dir_exists: {}", err); } - let ignore = GitignoreBuilder::new(tmp.path()) - .add_line(None, "*.md") - .expect("Unable to add '*.md' to gitignore builder") - .build() - .expect("Unable to build gitignore"); - - if let Err(e) = copy_files_except_ext( - tmp.path(), - &tmp.path().join("output"), - true, - None, - Some(&ignore), - ) { + if let Err(e) = + copy_files_except_ext(tmp.path(), &tmp.path().join("output"), true, None, &["md"]) + { panic!("Error while executing the function:\n{:?}", e); }