1use minifier::css::minify;
22use rumtk_core::strings::RUMString;
23use std::fs;
24use std::path;
25
26mod animations;
27mod basic;
28mod default;
29mod fonts;
30mod gap;
31mod index;
32
33pub const DEFAULT_OUT_CSS_DIR: &str = "./static/css";
34pub const DEFAULT_OUT_CSS: &str = "bundle.min.css";
35
36pub fn bundle_css(sources: &Vec<String>, out_dir: &str, out_file: &str) {
37 let mut css: RUMString = RUMString::default();
38
39 css += index::BODY;
40 css += basic::BASIC_CSS;
41 css += default::DEFAULT_CSS;
42 css += fonts::FONTS_CSS;
43 css += gap::GAP_CSS;
44 css += animations::ANIMATIONS_CSS;
45
46 for source in sources {
47 let css_data = fs::read_to_string(source).unwrap_or_default();
48 css += &css_data;
49 }
50
51 fs::create_dir_all(out_dir).unwrap_or_default();
52
53 let out_path = path::Path::new(out_dir)
54 .join(out_file)
55 .with_extension("css")
56 .to_str()
57 .expect("Could not create path to CSS file!")
58 .to_string();
59
60 if let Ok(exists) = fs::exists(&out_path) {
61 if !exists {
62 let minified = minify(&css)
63 .expect("Failed to minify the CSS contents!")
64 .to_string();
65 fs::write(&out_path, minified).expect("Failed to write to CSS file!");
66 }
67 }
68}
69
70pub fn collect_css_sources(root: &str, depth: u8) -> Vec<String> {
71 let mut files = Vec::<String>::new();
72
73 let dirs = match fs::read_dir(root) {
74 Ok(dirs) => dirs,
75 Err(_) => return files,
76 };
77
78 for dir_entry in dirs {
79 let dir = dir_entry.unwrap();
80 let dir_name = dir.file_name().into_string().unwrap();
81 let dir_path = dir.path().to_str().unwrap().to_string();
82 if dir_name.ends_with(".css") {
83 files.push(dir_path.clone());
84 }
85
86 if depth == 255 {
87 return files;
88 }
89
90 if dir.file_type().unwrap().is_dir() {
91 files.extend(collect_css_sources(&dir_path, depth + 1));
92 }
93 }
94
95 files
96}
97
98#[macro_export]
99macro_rules! rumtk_web_compile_css_bundle {
100 ( ) => {{
101 use $crate::css::{bundle_css, collect_css_sources};
102 use $crate::css::{DEFAULT_OUT_CSS, DEFAULT_OUT_CSS_DIR};
103 let sources = collect_css_sources(DEFAULT_OUT_CSS_DIR, 0);
104 bundle_css(&sources, DEFAULT_OUT_CSS_DIR, DEFAULT_OUT_CSS);
105 }};
106 ( $static_dir_path:expr ) => {{
107 use $crate::css::{bundle_css, collect_css_sources};
108 use $crate::css::{DEFAULT_OUT_CSS, DEFAULT_OUT_CSS_DIR};
109 let sources = collect_css_sources($static_dir_path, 0);
110 bundle_css(&sources, DEFAULT_OUT_CSS_DIR, DEFAULT_OUT_CSS);
111 }};
112}