79 lines
2.3 KiB
Rust
Executable file
79 lines
2.3 KiB
Rust
Executable file
#!/usr/bin/env rust-script
|
|
//! This is a regular crate doc comment, but it also contains a partial
|
|
//! Cargo manifest. Note the use of a *fenced* code block, and the
|
|
//! `cargo` "language".
|
|
//!
|
|
//! ```cargo
|
|
//! [dependencies]
|
|
//! encre-css = "0.14"
|
|
//! walkdir = "2"
|
|
//! ```
|
|
|
|
use std::{
|
|
collections::BTreeSet,
|
|
fs::{self, File},
|
|
io::{self, Write},
|
|
path::Path,
|
|
};
|
|
|
|
use encre_css::{utils::split_ignore_arbitrary, Config, Scanner};
|
|
use walkdir::WalkDir;
|
|
|
|
fn main() -> io::Result<()> {
|
|
// Define the source directory to scan
|
|
let source_dir = "./"; // Change this to your source directory
|
|
|
|
eprintln!("Scanning directory: {}", source_dir);
|
|
|
|
// Walk through the directory structure
|
|
let contents = WalkDir::new(source_dir)
|
|
.into_iter()
|
|
.filter_map(|e| e.ok())
|
|
.filter(|e| {
|
|
e.file_type().is_file()
|
|
&& e.path()
|
|
.extension()
|
|
.map_or(false, |ext| ext == "rs" || ext == "scss")
|
|
})
|
|
.map(|entry| {
|
|
let path = entry.path();
|
|
eprintln!("Processing file: {}", path.display());
|
|
let content = fs::read_to_string(path).expect("Failed to read");
|
|
let bx = Box::new(content);
|
|
let static_ref: &'static str = Box::leak(bx);
|
|
static_ref
|
|
});
|
|
|
|
let mut config = Config::default();
|
|
config.scanner = Scanner::from_fn(|val| {
|
|
let mut is_arbitrary = false;
|
|
|
|
val.split(|ch| {
|
|
// Escape all characters in arbitrary values prefixed by a dash (used to avoid
|
|
// ignoring values in, for example, JS arrays, given that they are defined
|
|
// using square brackets)
|
|
match ch {
|
|
'[' => {
|
|
is_arbitrary = true;
|
|
false
|
|
}
|
|
']' => {
|
|
is_arbitrary = false;
|
|
false
|
|
}
|
|
_ => {
|
|
ch == ' '
|
|
|| (!is_arbitrary
|
|
&& (ch == '.' || ch == '\'' || ch == '"' || ch == '`' || ch == '\n'))
|
|
}
|
|
}
|
|
})
|
|
.collect::<BTreeSet<&str>>()
|
|
});
|
|
|
|
let generated = encre_css::generate(contents, &config);
|
|
|
|
println!("{generated}");
|
|
|
|
Ok(())
|
|
}
|