Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement copy_files_except_ignore to not break the public API
Browse files Browse the repository at this point in the history
Bergmann89 committed Dec 19, 2024

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
1 parent b459b9f commit 98008f8
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
@@ -900,7 +900,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");
2 changes: 1 addition & 1 deletion src/renderer/html_handlebars/hbs_renderer.rs
Original file line number Diff line number Diff line change
@@ -630,7 +630,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,
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;
@@ -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>) -> 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);
}

0 comments on commit 98008f8

Please sign in to comment.