Skip to content

Commit

Permalink
Implement copy_files_except_ignore to not break the public API
Browse files Browse the repository at this point in the history
  • Loading branch information
Bergmann89 committed Jul 24, 2024
1 parent d82d9cf commit 2385a9b
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 19 deletions.
2 changes: 1 addition & 1 deletion src/book/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
2 changes: 1 addition & 1 deletion src/renderer/html_handlebars/hbs_renderer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
41 changes: 24 additions & 17 deletions src/utils/fs.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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;
}
}
Expand Down Expand Up @@ -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)?;
Expand Down Expand Up @@ -222,7 +240,6 @@ pub fn get_404_output_file(input_404: &Option<String>) -> 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")]
Expand Down Expand Up @@ -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);
}

Expand Down

0 comments on commit 2385a9b

Please sign in to comment.