smol bot
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#![feature(let_chains)]
use std::fs;
use std::io::prelude::*;
use std::path::Path;

pub fn process(input: impl AsRef<Path>) -> std::io::Result<()> {
    let mut f = fs::File::create(dbg!(Path::new("html").join(input.as_ref()))).unwrap();
    if !matches!(
        input.as_ref().extension().unwrap().to_str().unwrap(),
        "html" | "css"
    ) {
        return f.write_all(&std::fs::read(Path::new("html-src").join(input.as_ref()))?);
    }
    let mut c = std::process::Command::new("minify")
        .arg(Path::new("html-src").join(input.as_ref()))
        .stdout(std::process::Stdio::piped())
        .spawn()
        .unwrap();
    let mut o = c.stdout.take().unwrap();
    let mut buf = [0; 1024];
    while let Ok(x) = o.read(&mut buf)
        && x != 0
    {
        f.write_all(&buf[..x])?;
    }
    c.wait()?;
    Ok(())
}

fn main() -> std::io::Result<()> {
    if !Path::new("html").exists() {
        fs::create_dir("html")?;
    }

    for path in fs::read_dir("html-src")? {
        process(path.unwrap().path().file_name().unwrap())?;
    }
    println!("cargo:rerun-if-changed=html-src/");
    println!("cargo:rerun-if-changed=build.rs");
    Ok(())
}