leptonic_theme/
lib.rs

1use anyhow::{Context, Result};
2use include_dir::{include_dir, Dir};
3use indoc::indoc;
4use std::{io::Write, path::Path};
5
6static SCSS_DIR: Dir<'static> = include_dir!("$CARGO_MANIFEST_DIR/scss");
7
8/// Path must point to a folder which can be deleted and recreated freely!
9///
10/// # Errors
11///
12/// Will return `Err` if `path` can not be deleted or created or if
13/// files cannot be successfully copied or written.
14pub fn generate(path: impl AsRef<Path>) -> Result<()> {
15    let path = path.as_ref();
16
17    if path.exists() {
18        std::fs::remove_dir_all(path)
19            .with_context(|| format!("Could not remove path '{path:?}'"))?;
20    }
21    std::fs::create_dir_all(path).with_context(|| format!("Could not create path '{path:?}'"))?;
22
23    SCSS_DIR
24        .extract(path)
25        .with_context(|| format!("Could not extract theme into '{path:?}'"))?;
26
27    let themes_file_path = path.join("leptonic-themes.scss");
28    let mut file = std::fs::OpenOptions::new()
29        .create(true)
30        .write(true)
31        .append(false)
32        .truncate(true)
33        .open(&themes_file_path)
34        .context("Could not find leptonic-themes.scss after copying comp-time created SCSS_DIR. This must be a bug.")?;
35
36    file.write_all(
37        indoc!(
38            r#"
39            @import "./themes/builder";
40            @import "./themes/light";
41            @import "./themes/dark";
42            "#
43        )
44        .as_bytes(),
45    )
46    .with_context(|| format!("Could not write to '{}'", themes_file_path.display()))?;
47
48    Ok(())
49}