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
42
43
44
45
46
47
48
49
use anyhow::{Context, Result};
use include_dir::{include_dir, Dir};
use indoc::indoc;
use std::{io::Write, path::Path};

static SCSS_DIR: Dir<'static> = include_dir!("$CARGO_MANIFEST_DIR/scss");

/// Path must point to a folder which can be deleted and recreated freely!
///
/// # Errors
///
/// Will return `Err` if `path` can not be deleted or created or if
/// files cannot be successfully copied or written.
pub fn generate(path: impl AsRef<Path>) -> Result<()> {
    let path = path.as_ref();

    if path.exists() {
        std::fs::remove_dir_all(path)
            .with_context(|| format!("Could not remove path '{path:?}'"))?;
    }
    std::fs::create_dir_all(path).with_context(|| format!("Could not create path '{path:?}'"))?;

    SCSS_DIR
        .extract(path)
        .with_context(|| format!("Could not extract theme into '{path:?}'"))?;

    let themes_file_path = path.join("leptonic-themes.scss");
    let mut file = std::fs::OpenOptions::new()
        .create(true)
        .write(true)
        .append(false)
        .truncate(true)
        .open(&themes_file_path)
        .context("Could not find leptonic-themes.scss after copying comp-time created SCSS_DIR. This must be a bug.")?;

    file.write_all(
        indoc!(
            r#"
            @import "./themes/builder";
            @import "./themes/light";
            @import "./themes/dark";
            "#
        )
        .as_bytes(),
    )
    .with_context(|| format!("Could not write to '{}'", themes_file_path.display()))?;

    Ok(())
}