|
1 | 1 | use crate::errors::*; |
2 | | -use ignore::gitignore::Gitignore; |
| 2 | +use ignore::gitignore::{Gitignore, GitignoreBuilder}; |
3 | 3 | use log::{debug, trace}; |
4 | 4 | use std::fs::{self, File}; |
5 | 5 | use std::io::Write; |
@@ -87,6 +87,24 @@ pub fn remove_dir_content(dir: &Path) -> Result<()> { |
87 | 87 | /// Copies all files of a directory to another one except the files |
88 | 88 | /// with the extensions given in the `ext_blacklist` array |
89 | 89 | 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( |
90 | 108 | from: &Path, |
91 | 109 | to: &Path, |
92 | 110 | recursive: bool, |
@@ -118,7 +136,7 @@ pub fn copy_files_except_ext( |
118 | 136 | // Check if it is in the blacklist |
119 | 137 | if let Some(ignore) = ignore { |
120 | 138 | 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() { |
122 | 140 | continue; |
123 | 141 | } |
124 | 142 | } |
@@ -147,7 +165,7 @@ pub fn copy_files_except_ext( |
147 | 165 | fs::create_dir(&target_file_path)?; |
148 | 166 | } |
149 | 167 |
|
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)?; |
151 | 169 | } else if metadata.is_file() { |
152 | 170 | debug!("Copying {entry:?} to {target_file_path:?}"); |
153 | 171 | copy(&entry, &target_file_path)?; |
@@ -222,7 +240,6 @@ pub fn get_404_output_file(input_404: &Option<String>) -> String { |
222 | 240 | #[cfg(test)] |
223 | 241 | mod tests { |
224 | 242 | use super::copy_files_except_ext; |
225 | | - use ignore::gitignore::GitignoreBuilder; |
226 | 243 | use std::{fs, io::Result, path::Path}; |
227 | 244 |
|
228 | 245 | #[cfg(target_os = "windows")] |
@@ -276,19 +293,9 @@ mod tests { |
276 | 293 | panic!("Could not create output/sub_dir_exists: {}", err); |
277 | 294 | } |
278 | 295 |
|
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 | + { |
292 | 299 | panic!("Error while executing the function:\n{:?}", e); |
293 | 300 | } |
294 | 301 |
|
|
0 commit comments