no_hash option
This commit is contained in:
parent
551603035a
commit
747a474e9e
2 changed files with 65 additions and 4 deletions
|
@ -1,5 +1,11 @@
|
||||||
## 0.2.0
|
## 0.2.0
|
||||||
|
|
||||||
|
### Added:
|
||||||
|
|
||||||
|
- `Buster.no_hash` Option to exclude select files from processing. These
|
||||||
|
files will be copied over without any processing for cache
|
||||||
|
busting(i.e, no renaming)
|
||||||
|
|
||||||
### Changed:
|
### Changed:
|
||||||
|
|
||||||
- `Files::new()` takes a `&str`: Earlier versions were using
|
- `Files::new()` takes a `&str`: Earlier versions were using
|
||||||
|
|
|
@ -51,7 +51,8 @@ use crate::*;
|
||||||
|
|
||||||
/// Configuration for setting up cache-busting
|
/// Configuration for setting up cache-busting
|
||||||
#[derive(Debug, Clone, Builder)]
|
#[derive(Debug, Clone, Builder)]
|
||||||
pub struct Buster {
|
#[builder(build_fn(validate = "Self::validate"))]
|
||||||
|
pub struct Buster<'a> {
|
||||||
/// source directory
|
/// source directory
|
||||||
#[builder(setter(into))]
|
#[builder(setter(into))]
|
||||||
source: String,
|
source: String,
|
||||||
|
@ -67,9 +68,30 @@ pub struct Buster {
|
||||||
copy: bool,
|
copy: bool,
|
||||||
/// follow symlinks?
|
/// follow symlinks?
|
||||||
follow_links: bool,
|
follow_links: bool,
|
||||||
|
/// exclude these files for hashing.
|
||||||
|
/// They will be copied over without including a hash in the filename
|
||||||
|
/// Path should be relative to [self.source]
|
||||||
|
#[builder(default)]
|
||||||
|
no_hash: Vec<&'a str>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Buster {
|
impl<'a> BusterBuilder<'a> {
|
||||||
|
fn validate(&self) -> Result<(), String> {
|
||||||
|
for file in self.no_hash.iter() {
|
||||||
|
for file in file.iter() {
|
||||||
|
if !Path::new(&self.source.as_ref().unwrap())
|
||||||
|
.join(file)
|
||||||
|
.exists()
|
||||||
|
{
|
||||||
|
return Err(format!("File {} doesn't exist", file));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a> Buster<'a> {
|
||||||
// creates base_dir to output files to
|
// creates base_dir to output files to
|
||||||
fn init(&self) -> Result<(), Error> {
|
fn init(&self) -> Result<(), Error> {
|
||||||
let res = Path::new(&self.result);
|
let res = Path::new(&self.result);
|
||||||
|
@ -107,7 +129,7 @@ impl Buster {
|
||||||
let entry = entry?;
|
let entry = entry?;
|
||||||
|
|
||||||
let path = entry.path();
|
let path = entry.path();
|
||||||
if !path.is_dir() {
|
if !path.is_dir() && !self.no_hash.contains(&path.to_str().unwrap()) {
|
||||||
let path = Path::new(&path);
|
let path = Path::new(&path);
|
||||||
|
|
||||||
for mime_type in self.mime_types.iter() {
|
for mime_type in self.mime_types.iter() {
|
||||||
|
@ -150,7 +172,7 @@ impl Buster {
|
||||||
}
|
}
|
||||||
|
|
||||||
// helper fn to generate filemap
|
// helper fn to generate filemap
|
||||||
fn gen_map<'a>(&self, source: &'a Path, name: &str) -> (&'a Path, PathBuf) {
|
fn gen_map<'b>(&self, source: &'b Path, name: &str) -> (&'b Path, PathBuf) {
|
||||||
let rel_location = source.strip_prefix(&self.source).unwrap().parent().unwrap();
|
let rel_location = source.strip_prefix(&self.source).unwrap().parent().unwrap();
|
||||||
if let Some(prefix) = &self.prefix {
|
if let Some(prefix) = &self.prefix {
|
||||||
//panic!("{}", &prefix);
|
//panic!("{}", &prefix);
|
||||||
|
@ -318,6 +340,8 @@ pub mod tests {
|
||||||
mime::IMAGE_GIF,
|
mime::IMAGE_GIF,
|
||||||
];
|
];
|
||||||
|
|
||||||
|
let no_hash = vec!["bell.svg", "eye.svg", "a/b/c/d/s/d/svg/10.svg"];
|
||||||
|
|
||||||
let config = BusterBuilder::default()
|
let config = BusterBuilder::default()
|
||||||
.source("./dist")
|
.source("./dist")
|
||||||
.result("/tmp/prod2i")
|
.result("/tmp/prod2i")
|
||||||
|
@ -325,6 +349,7 @@ pub mod tests {
|
||||||
.copy(true)
|
.copy(true)
|
||||||
.follow_links(true)
|
.follow_links(true)
|
||||||
.prefix("/test")
|
.prefix("/test")
|
||||||
|
.no_hash(no_hash.clone())
|
||||||
.build()
|
.build()
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
|
@ -332,6 +357,14 @@ pub mod tests {
|
||||||
let mut files = Files::load();
|
let mut files = Files::load();
|
||||||
|
|
||||||
if let Some(prefix) = &config.prefix {
|
if let Some(prefix) = &config.prefix {
|
||||||
|
no_hash.iter().for_each(|file| {
|
||||||
|
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()
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
for (k, v) in files.map.drain() {
|
for (k, v) in files.map.drain() {
|
||||||
let src = Path::new(&k);
|
let src = Path::new(&k);
|
||||||
let dest = Path::new(&v[prefix.len()..]);
|
let dest = Path::new(&v[prefix.len()..]);
|
||||||
|
@ -343,6 +376,28 @@ pub mod tests {
|
||||||
cleanup(&config);
|
cleanup(&config);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn no_hash_validation_works() {
|
||||||
|
let types = vec![
|
||||||
|
mime::IMAGE_PNG,
|
||||||
|
mime::IMAGE_SVG,
|
||||||
|
mime::IMAGE_JPEG,
|
||||||
|
mime::IMAGE_GIF,
|
||||||
|
];
|
||||||
|
|
||||||
|
let no_hash = vec!["bbell.svg", "eye.svg", "a/b/c/d/s/d/svg/10.svg"];
|
||||||
|
assert!(BusterBuilder::default()
|
||||||
|
.source("./dist")
|
||||||
|
.result("/tmp/prod2i")
|
||||||
|
.mime_types(types)
|
||||||
|
.copy(true)
|
||||||
|
.follow_links(true)
|
||||||
|
.prefix("/test")
|
||||||
|
.no_hash(no_hash.clone())
|
||||||
|
.build()
|
||||||
|
.is_err())
|
||||||
|
}
|
||||||
|
|
||||||
pub fn runner() {
|
pub fn runner() {
|
||||||
prefix_works();
|
prefix_works();
|
||||||
hasher_works();
|
hasher_works();
|
||||||
|
|
Loading…
Reference in a new issue