Skip to content

Commit 2385a9b

Browse files
committed
Implement copy_files_except_ignore to not break the public API
1 parent d82d9cf commit 2385a9b

File tree

3 files changed

+26
-19
lines changed

3 files changed

+26
-19
lines changed

src/book/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -906,7 +906,7 @@ mod tests {
906906
let temp_dir = TempFileBuilder::new().prefix("mdbook-").tempdir().unwrap();
907907
let test_book_dir = PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("test_book");
908908

909-
utils::fs::copy_files_except_ext(&test_book_dir, temp_dir.path(), true, None, None)
909+
utils::fs::copy_files_except_ignored(&test_book_dir, temp_dir.path(), true, None, None)
910910
.expect("Error while copying test book to temp dir");
911911

912912
let book = MDBook::load(temp_dir.path()).expect("Unable to load book");

src/renderer/html_handlebars/hbs_renderer.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -613,7 +613,7 @@ impl Renderer for HtmlHandlebars {
613613
builder.add_line(None, "*.md")?;
614614
let ignore = builder.build()?;
615615

616-
utils::fs::copy_files_except_ext(
616+
utils::fs::copy_files_except_ignored(
617617
&src_dir,
618618
destination,
619619
true,

src/utils/fs.rs

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::errors::*;
2-
use ignore::gitignore::Gitignore;
2+
use ignore::gitignore::{Gitignore, GitignoreBuilder};
33
use log::{debug, trace};
44
use std::fs::{self, File};
55
use std::io::Write;
@@ -87,6 +87,24 @@ pub fn remove_dir_content(dir: &Path) -> Result<()> {
8787
/// Copies all files of a directory to another one except the files
8888
/// with the extensions given in the `ext_blacklist` array
8989
pub fn copy_files_except_ext(
90+
from: &Path,
91+
to: &Path,
92+
recursive: bool,
93+
avoid_dir: Option<&PathBuf>,
94+
ext_blacklist: &[&str],
95+
) -> Result<()> {
96+
let mut builder = GitignoreBuilder::new(from);
97+
for ext in ext_blacklist {
98+
builder.add_line(None, &format!("*.{ext}"))?;
99+
}
100+
let ignore = builder.build()?;
101+
102+
copy_files_except_ignored(from, to, recursive, avoid_dir, Some(&ignore))
103+
}
104+
105+
/// Copies all files of a directory to another one except the files that are
106+
/// ignored by the passed [`Gitignore`]
107+
pub fn copy_files_except_ignored(
90108
from: &Path,
91109
to: &Path,
92110
recursive: bool,
@@ -118,7 +136,7 @@ pub fn copy_files_except_ext(
118136
// Check if it is in the blacklist
119137
if let Some(ignore) = ignore {
120138
let path = entry.as_path();
121-
if ignore.matched(&path, path.is_dir()).is_ignore() {
139+
if ignore.matched(path, path.is_dir()).is_ignore() {
122140
continue;
123141
}
124142
}
@@ -147,7 +165,7 @@ pub fn copy_files_except_ext(
147165
fs::create_dir(&target_file_path)?;
148166
}
149167

150-
copy_files_except_ext(&entry, &target_file_path, true, avoid_dir, ignore)?;
168+
copy_files_except_ignored(&entry, &target_file_path, true, avoid_dir, ignore)?;
151169
} else if metadata.is_file() {
152170
debug!("Copying {entry:?} to {target_file_path:?}");
153171
copy(&entry, &target_file_path)?;
@@ -222,7 +240,6 @@ pub fn get_404_output_file(input_404: &Option<String>) -> String {
222240
#[cfg(test)]
223241
mod tests {
224242
use super::copy_files_except_ext;
225-
use ignore::gitignore::GitignoreBuilder;
226243
use std::{fs, io::Result, path::Path};
227244

228245
#[cfg(target_os = "windows")]
@@ -276,19 +293,9 @@ mod tests {
276293
panic!("Could not create output/sub_dir_exists: {}", err);
277294
}
278295

279-
let ignore = GitignoreBuilder::new(tmp.path())
280-
.add_line(None, "*.md")
281-
.expect("Unable to add '*.md' to gitignore builder")
282-
.build()
283-
.expect("Unable to build gitignore");
284-
285-
if let Err(e) = copy_files_except_ext(
286-
tmp.path(),
287-
&tmp.path().join("output"),
288-
true,
289-
None,
290-
Some(&ignore),
291-
) {
296+
if let Err(e) =
297+
copy_files_except_ext(tmp.path(), &tmp.path().join("output"), true, None, &["md"])
298+
{
292299
panic!("Error while executing the function:\n{:?}", e);
293300
}
294301

0 commit comments

Comments
 (0)