From: Ruben Beltran del Rio Date: Sun, 16 Apr 2023 11:46:46 +0000 (+0200) Subject: Add static file copying X-Git-Tag: 1.0.0~8 X-Git-Url: https://git.r.bdr.sh/rbdr/page/commitdiff_plain/ea5297364f8a1b2c4e684140024b60a83b087b50?ds=sidebyside Add static file copying --- diff --git a/src/file_finder.rs b/src/file_finder.rs index 105a999..bcd368d 100644 --- a/src/file_finder.rs +++ b/src/file_finder.rs @@ -13,7 +13,7 @@ fn find_files_recursively(root_path: &PathBuf, directory_path: &PathBuf) -> Vec< for entry in entries { let path = entry.unwrap().path(); let relative_path = path.strip_prefix(&root_path).unwrap(); - if relative_path.starts_with(".git") { + if relative_path.starts_with(".git") || relative_path.starts_with(".gitignore") { continue; } if path.is_dir() { diff --git a/src/file_handler/file_strategies/file.rs b/src/file_handler/file_strategies/file.rs index b1de596..42b87d5 100644 --- a/src/file_handler/file_strategies/file.rs +++ b/src/file_handler/file_strategies/file.rs @@ -1,6 +1,7 @@ pub struct Strategy {} use std::path::PathBuf; +use std::fs::{copy, create_dir_all}; use crate::file_handler::{File, FileType, FileHandlerStrategy}; @@ -20,7 +21,11 @@ impl FileHandlerStrategy for Strategy { } } - fn handle(&self, file: &File) { - println!("Should copy {}", file.path.display()) + fn handle(&self, source: &PathBuf, destination: &PathBuf, file: &File) { + let relative_path = file.path.strip_prefix(&source).unwrap(); + let complete_destination = destination.join(relative_path); + let destination_parent = complete_destination.parent().unwrap(); + create_dir_all(destination_parent).unwrap(); + copy(&file.path, &complete_destination).unwrap(); } } diff --git a/src/file_handler/file_strategies/gemini.rs b/src/file_handler/file_strategies/gemini.rs index 905e1a9..2427422 100644 --- a/src/file_handler/file_strategies/gemini.rs +++ b/src/file_handler/file_strategies/gemini.rs @@ -23,7 +23,7 @@ impl FileHandlerStrategy for Strategy { } } - fn handle(&self, file: &File) { + fn handle(&self, source: &PathBuf, destination: &PathBuf, file: &File) { println!("Should parse and copy {}", file.path.display()) } } diff --git a/src/file_handler/file_strategies/layout.rs b/src/file_handler/file_strategies/layout.rs index bac1059..21ce9ba 100644 --- a/src/file_handler/file_strategies/layout.rs +++ b/src/file_handler/file_strategies/layout.rs @@ -20,5 +20,7 @@ impl FileHandlerStrategy for Strategy { } } - fn handle(&self, _file: &File) {} + // We don't implement handling for layout, as we assume there's only one + // and it got handled before. + fn handle(&self, _s: &PathBuf, _d: &PathBuf, _f: &File) {} } diff --git a/src/file_handler/mod.rs b/src/file_handler/mod.rs index 53aaba9..64224ee 100644 --- a/src/file_handler/mod.rs +++ b/src/file_handler/mod.rs @@ -58,7 +58,7 @@ impl FileHandler { pub fn handle(&self, source: &PathBuf, destination: &PathBuf, file: &File) { for strategy in self.strategies.iter() { if strategy.can_handle(&file.file_type) { - return strategy.handle(file); + return strategy.handle(source, destination, file); } } } @@ -68,7 +68,7 @@ pub trait FileHandlerStrategy { fn is(&self, path: &PathBuf) -> bool; fn identify(&self) -> FileType; fn can_handle(&self, file_type: &FileType) -> bool; - fn handle(&self, file: &File); + fn handle(&self, source: &PathBuf, destination: &PathBuf, file: &File); } pub enum FileType { diff --git a/src/main.rs b/src/main.rs index e04da8a..7ab009e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -4,7 +4,7 @@ mod file_handler; use std::io::Result; use std::env::current_dir; -use std::fs::create_dir_all; +use std::fs::{create_dir_all, remove_dir_all}; use crate::gemini_parser::parse; use crate::file_finder::find_files; @@ -25,6 +25,9 @@ fn main() -> Result<()> { let files = find_files(&source); // Step 2. Prepare the target priority + match remove_dir_all(&destination) { + _ => {} + }; create_dir_all(&destination)?; println!("Found {} files", files.len());