no_hash copies files

This commit is contained in:
Aravinth Manivannan 2021-07-05 15:24:56 +05:30
parent 209682b33c
commit 33d0612292
Signed by: realaravinth
GPG key ID: AD9F0F08E855ED88
2 changed files with 71 additions and 21 deletions

BIN
dist/858fd6c482cc75111d54.module.wasm vendored Normal file

Binary file not shown.

View file

@ -55,7 +55,8 @@ pub struct Buster<'a> {
#[builder(setter(into))] #[builder(setter(into))]
source: String, source: String,
/// mime_types for hashing /// mime_types for hashing
mime_types: Vec<mime::Mime>, #[builder(setter(into, strip_option), default)]
mime_types: Option<Vec<mime::Mime>>,
/// directory for writing results /// directory for writing results
#[builder(setter(into))] #[builder(setter(into))]
result: String, result: String,
@ -127,29 +128,50 @@ impl<'a> Buster<'a> {
let entry = entry?; let entry = entry?;
let path = entry.path(); let path = entry.path();
if !path.is_dir() && !self.no_hash.contains(&path.to_str().unwrap()) { if !path.is_dir() {
let path = Path::new(&path); let path = Path::new(&path);
for mime_type in self.mime_types.iter() { let mut process_worker = |path: &Path| {
let file_mime = mime_guess::from_path(path) let contents = Self::read_to_string(&path).unwrap();
.first() let hash = Self::hasher(&contents);
.unwrap_or_else(|| panic!("couldn't resolve MIME for file: {:?}", &path)); let new_name = if self.no_hash.iter().any(|no_hash| {
if &file_mime == mime_type { let no_hash = Path::new(&self.source).join(&no_hash);
let contents = Self::read_to_string(&path).unwrap(); no_hash == path
let hash = Self::hasher(&contents); }) {
let new_name = format!( format!(
"{}.{}",
path.file_stem().unwrap().to_str().unwrap(),
path.extension().unwrap().to_str().unwrap()
)
} else {
format!(
"{}.{}.{}", "{}.{}.{}",
path.file_stem().unwrap().to_str().unwrap(), path.file_stem().unwrap().to_str().unwrap(),
hash, hash,
path.extension().unwrap().to_str().unwrap() path.extension().unwrap().to_str().unwrap()
); )
self.copy(path, &new_name); };
let (source, destination) = self.gen_map(path, &&new_name); self.copy(path, &new_name);
let _ = file_map.add( let (source, destination) = self.gen_map(path, &&new_name);
source.to_str().unwrap().into(), let _ = file_map.add(
destination.to_str().unwrap().into(), source.to_str().unwrap().into(),
); destination.to_str().unwrap().into(),
);
};
match self.mime_types.as_ref() {
Some(mime_types) => {
for mime_type in mime_types.iter() {
let file_mime =
mime_guess::from_path(path).first().unwrap_or_else(|| {
panic!("couldn't resolve MIME for file: {:?}", &path)
});
if &file_mime == mime_type {
process_worker(&path);
}
}
} }
None => process_worker(&path),
} }
} }
} }
@ -340,7 +362,7 @@ pub mod tests {
let config = BusterBuilder::default() let config = BusterBuilder::default()
.source("./dist") .source("./dist")
.result("/tmp/prod2i") .result("/tmp/prod2i2")
.mime_types(types) .mime_types(types)
.copy(true) .copy(true)
.follow_links(true) .follow_links(true)
@ -354,11 +376,16 @@ pub mod tests {
if let Some(prefix) = &config.prefix { if let Some(prefix) = &config.prefix {
no_hash.iter().for_each(|file| { no_hash.iter().for_each(|file| {
files.map.iter().any(|(k, v)| { assert!(files.map.iter().any(|(k, v)| {
let source = Path::new(k);
let dest = Path::new(&v[prefix.len()..]); let dest = Path::new(&v[prefix.len()..]);
let no_hash = Path::new(file); let no_hash = Path::new(file);
k == file && dest.exists() && no_hash.file_name() == dest.file_name() let stat = source == &Path::new(&config.source).join(file)
}); && dest.exists()
&& no_hash.file_name() == dest.file_name();
println!("[{}] file: {}", stat, file);
stat
}));
}); });
for (k, v) in files.map.drain() { for (k, v) in files.map.drain() {
@ -394,6 +421,29 @@ pub mod tests {
.is_err()) .is_err())
} }
// #[test]
// fn no_specific_mime() {
// let no_hash = vec!["858fd6c482cc75111d54.module.wasm"];
// let config = BusterBuilder::default()
// .source("./dist")
// .result("/tmp/prod2ii")
// .copy(true)
// .follow_links(true)
// .no_hash(no_hash.clone())
// .prefix("/test")
// .build()
// .unwrap();
// config.process().unwrap();
// let files = Files::load();
// files.map.iter().any(|(k, v)| {
// let dest = Path::new(&v[prefix.len()..]);
// let no_hash = Path::new(file);
// k == file && dest.exists() && no_hash.file_name() == dest.file_name()
// });
//
// cleanup(&config);
// }
pub fn runner() { pub fn runner() {
prefix_works(); prefix_works();
hasher_works(); hasher_works();