route prefix

This commit is contained in:
Aravinth Manivannan 2021-04-12 18:23:56 +05:30
parent 09fbbe186c
commit d5593b2db6
Signed by: realaravinth
GPG key ID: AD9F0F08E855ED88
3 changed files with 59 additions and 3 deletions

View file

@ -1,5 +1,8 @@
## 0.1.1 ## 0.1.1
### Added:
- Optional route prefix to `Processor`
### Changed: ### Changed:
- `Files::load()` became `Files::new()` - `Files::load()` became `Files::new()`

View file

@ -17,6 +17,7 @@
- [x] `SHA-256` based name generation during compile-time - [x] `SHA-256` based name generation during compile-time
- [x] Processes files based on provided MIME filters - [x] Processes files based on provided MIME filters
- [x] Exposes modified names to program during runtime - [x] Exposes modified names to program during runtime
- [x] Route prefixes(optional)
## Usage: ## Usage:

View file

@ -60,6 +60,9 @@ pub struct Buster {
/// directory for writing results /// directory for writing results
#[builder(setter(into))] #[builder(setter(into))]
result: String, result: String,
#[builder(setter(into, strip_option), default)]
/// route prefixes
prefix: Option<String>,
/// copy other non-hashed files from source dire to result dir? /// copy other non-hashed files from source dire to result dir?
copy: bool, copy: bool,
/// follow symlinks? /// follow symlinks?
@ -74,6 +77,7 @@ impl Buster {
fs::remove_dir_all(&self.result).unwrap(); fs::remove_dir_all(&self.result).unwrap();
} }
println!("{}", &self.result);
fs::create_dir(&self.result).unwrap(); fs::create_dir(&self.result).unwrap();
self.create_dir_structure(Path::new(&self.source))?; self.create_dir_structure(Path::new(&self.source))?;
Ok(()) Ok(())
@ -195,8 +199,22 @@ 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<'a>(&self, source: &'a Path, name: &str) -> (&'a Path, PathBuf) {
let rel_location = source.strip_prefix(&self.source).unwrap().parent().unwrap(); let rel_location = source.strip_prefix(&self.source).unwrap().parent().unwrap();
let destination = Path::new(&self.result).join(rel_location).join(name); if let Some(prefix) = &self.prefix {
(source, destination) //panic!("{}", &prefix);
let mut result = self.result.as_str();
if result.chars().nth(0) == Some('/') {
result = &self.result[1..];
}
let destination = Path::new(prefix)
.join(&self.result[1..])
.join(rel_location)
.join(name);
(source, destination.into())
} else {
let destination = Path::new(&self.result).join(rel_location).join(name);
(source, destination.into())
}
} }
// helper fn to copy files // helper fn to copy files
@ -231,7 +249,6 @@ impl Buster {
Ok(()) Ok(())
} }
} }
/// Filemap struct /// Filemap struct
/// ///
/// maps original names to generated names /// maps original names to generated names
@ -318,6 +335,7 @@ pub mod tests {
assert_eq!(src.exists(), dest.exists()); assert_eq!(src.exists(), dest.exists());
} }
cleanup(&config); cleanup(&config);
} }
@ -355,4 +373,38 @@ pub mod tests {
pub fn cleanup(config: &Buster) { pub fn cleanup(config: &Buster) {
let _ = fs::remove_dir_all(&config.result); let _ = fs::remove_dir_all(&config.result);
} }
#[test]
fn prefix_works() {
let types = vec![
mime::IMAGE_PNG,
mime::IMAGE_SVG,
mime::IMAGE_JPEG,
mime::IMAGE_GIF,
];
let config = BusterBuilder::default()
.source("./dist")
.result("/tmp/prod2i")
.mime_types(types)
.copy(true)
.follow_links(true)
.prefix("/test")
.build()
.unwrap();
config.process().unwrap();
let mut files = Files::load();
if let Some(prefix) = &config.prefix {
for (k, v) in files.map.drain() {
let src = Path::new(&k);
println!("{}", &v);
let dest = Path::new(&v[prefix.len()..]);
assert_eq!(src.exists(), dest.exists());
}
}
cleanup(&config);
}
} }