gitscope
← rgit / main / build.rs
https://dev.kronoz.win/r/rgit.git
build.rs · Rust
Blame Raw
 1use std::{
 2    io::Write,
 3    path::{Path, PathBuf},
 4};
 5
 6use anyhow::Context;
 7
 8#[derive(Copy, Clone)]
 9pub struct Paths<'a> {
10    statics_in_dir: &'a Path,
11    statics_out_dir: &'a Path,
12}
13
14fn main() {
15    if let Err(e) = run() {
16        eprintln!("An error occurred within the rgit build script:\n\n{:?}", e);
17        std::process::exit(1);
18    }
19}
20
21fn run() -> anyhow::Result<()> {
22    let manifest_dir =
23        PathBuf::from(std::env::var("CARGO_MANIFEST_DIR").context("CARGO_MANIFEST_DIR not set")?);
24    let statics_in_dir = manifest_dir.join("statics");
25
26    let out_dir = PathBuf::from(std::env::var("OUT_DIR").context("OUT_DIR not set by rustc")?);
27    let statics_out_dir = out_dir.join("statics");
28
29    let paths = Paths {
30        statics_in_dir: &statics_in_dir,
31        statics_out_dir: &statics_out_dir,
32    };
33
34    build_scss(paths).context("Failed to build CSS stylesheets")?;
35
36    Ok(())
37}
38
39fn build_scss(paths: Paths) -> anyhow::Result<()> {
40    let in_dir = paths.statics_in_dir.join("sass");
41    let out_dir = paths.statics_out_dir.join("css");
42    std::fs::create_dir_all(&out_dir).context("Failed to create output directory")?;
43
44    println!("cargo:rerun-if-changed={}", in_dir.display());
45
46    let input_file = in_dir.join("style.scss");
47    let output_file = out_dir.join("style.css");
48    let format = rsass::output::Format {
49        style: rsass::output::Style::Compressed,
50        ..rsass::output::Format::default()
51    };
52
53    let output_content =
54        rsass::compile_scss_path(&input_file, format).context("Failed to compile SASS")?;
55
56    let mut output_file = std::fs::OpenOptions::new()
57        .write(true)
58        .create(true)
59        .truncate(true)
60        .open(output_file)
61        .context("Failed to open output file")?;
62    output_file
63        .write_all(&output_content)
64        .context("Failed to write compiled CSS to output")?;
65
66    Ok(())
67}