From d1568f406df77e57b2efccfb721ddd5618c4e4ca Mon Sep 17 00:00:00 2001 From: Mylloon Date: Sun, 9 Apr 2023 17:35:15 +0200 Subject: [PATCH] always use dist directory --- askama.toml | 3 +-- src/config.rs | 67 +++++++++++++++++++++++++++------------------------ 2 files changed, 36 insertions(+), 34 deletions(-) diff --git a/askama.toml b/askama.toml index 4c5d332..4ac275b 100644 --- a/askama.toml +++ b/askama.toml @@ -1,3 +1,2 @@ [general] -# Directories to search for templates, relative to the crate root. -dirs = ["dist/templates", "templates"] +dirs = ["dist/templates"] diff --git a/src/config.rs b/src/config.rs index aa85c47..589d92d 100644 --- a/src/config.rs +++ b/src/config.rs @@ -89,10 +89,17 @@ pub fn get_config(file_path: &str) -> Config { let internal_config = get_file_config(file_path); let static_dir = "static".to_string(); - // TODO: Check if templates dir is coherent with the whole list - let templates_dir = get_askama_config().general.dirs.last().unwrap().to_string(); - // TODO: Check dist by askama config file - let files_root = init("dist".to_string(), static_dir.clone(), templates_dir); + let templates_dirs = get_askama_config().general.dirs; + if templates_dirs.len() != 1 { + panic!("Too many templates directories") + } + let mut template_data = templates_dirs[0].split('/'); + + let files_root = init( + template_data.next().unwrap().to_string(), + static_dir.clone(), + template_data.next().unwrap().to_string(), + ); Config { fc: internal_config, @@ -101,40 +108,35 @@ pub fn get_config(file_path: &str) -> Config { } fn init(dist_dir: String, static_dir: String, templates_dir: String) -> String { - // The static folder is minimized only in release mode - if cfg!(debug_assertions) { - // Be sure that we not gonna use the dist folder by deleting it - remove_dir_all(dist_dir).unwrap_or_default(); + // Be sure that we not gonna use the dist folder by deleting it + remove_dir_all(&dist_dir).unwrap_or_default(); - ".".to_string() - } else { - let cfg = Cfg::spec_compliant(); + let cfg = Cfg::spec_compliant(); - // Static files - for entry in glob(&format!("{static_dir}/**/*.*")).unwrap() { - let path = entry.unwrap(); - let path_with_dist = path - .to_string_lossy() - .replace(&static_dir, &format!("{dist_dir}/{static_dir}")); + // Static files + for entry in glob(&format!("{static_dir}/**/*.*")).unwrap() { + let path = entry.unwrap(); + let path_with_dist = path + .to_string_lossy() + .replace(&static_dir, &format!("{dist_dir}/{static_dir}")); - minify_and_copy(&cfg, path, path_with_dist); - } - - // Template files - for entry in glob(&format!("{templates_dir}/**/*.*")).unwrap() { - let path = entry.unwrap(); - let path_with_dist = path - .to_string_lossy() - .replace(&templates_dir, &format!("{dist_dir}/{templates_dir}")); - - minify_and_copy(&cfg, path, path_with_dist); - } - - dist_dir + copy_to_dist(&cfg, path, path_with_dist); } + + // Template files + for entry in glob(&format!("{templates_dir}/**/*.*")).unwrap() { + let path = entry.unwrap(); + let path_with_dist = path + .to_string_lossy() + .replace(&templates_dir, &format!("{dist_dir}/{templates_dir}")); + + copy_to_dist(&cfg, path, path_with_dist); + } + + dist_dir } -fn minify_and_copy(cfg: &Cfg, path: PathBuf, path_with_dist: String) { +fn copy_to_dist(cfg: &Cfg, path: PathBuf, path_with_dist: String) { // Create folders let new_path = Path::new(&path_with_dist); fs::create_dir_all(new_path.parent().unwrap()).unwrap(); @@ -145,6 +147,7 @@ fn minify_and_copy(cfg: &Cfg, path: PathBuf, path_with_dist: String) { if ["html", "css", "js", "svg", "webmanifest", "xml"] .iter() .any(|item| ext.to_string_lossy().to_lowercase().contains(item)) + && !cfg!(debug_assertions) { // We won't copy, we'll minify copy = false;