use crate::{Result, error};
use snafu::ResultExt;
use std::path::Path;
pub(crate) fn copy_source_tree(src: &Path, dst: &Path) -> Result<()> {
use ignore::WalkBuilder;
let walker = WalkBuilder::new(src)
.hidden(false) .git_ignore(true) .git_exclude(true) .build();
for result in walker {
let entry = result
.map_err(|e| Box::new(e) as _)
.with_context(|_| error::CopySourceTreeSnafu {
src: src.to_path_buf(),
dst: dst.to_path_buf(),
})?;
let src_path = entry.path();
if src_path == src {
continue; }
let rel_path = src_path.strip_prefix(src).unwrap();
let dst_path = dst.join(rel_path);
let file_type = entry.file_type().unwrap();
if file_type.is_dir() {
std::fs::create_dir_all(&dst_path)
.map_err(|e| Box::new(e) as _)
.with_context(|_| error::CopySourceTreeSnafu {
src: src.to_path_buf(),
dst: dst.to_path_buf(),
})?;
} else if file_type.is_file() {
if let Some(parent) = dst_path.parent() {
std::fs::create_dir_all(parent)
.map_err(|e| Box::new(e) as _)
.with_context(|_| error::CopySourceTreeSnafu {
src: src.to_path_buf(),
dst: dst.to_path_buf(),
})?;
}
std::fs::copy(src_path, &dst_path)
.map_err(|e| Box::new(e) as _)
.with_context(|_| error::CopySourceTreeSnafu {
src: src.to_path_buf(),
dst: dst.to_path_buf(),
})?;
}
}
Ok(())
}