rustfmt
[recipes.git] / backend / build.rs
1 /*
2 Additionnal build, doc: https://doc.rust-lang.org/cargo/reference/build-scripts.html
3
4 What is build here:
5 - Compile the SASS file to CSS file.
6 */
7
8 use std::{
9 env,
10 path::Path,
11 process::{Command, Output},
12 };
13
14 fn exists_in_path<P>(filename: P) -> bool
15 where
16 P: AsRef<Path>,
17 {
18 for path in env::split_paths(&env::var_os("PATH").unwrap()) {
19 if path.join(&filename).is_file() {
20 return true;
21 }
22 }
23 false
24 }
25
26 fn main() {
27 println!("cargo:rerun-if-changed=style.scss");
28
29 fn run_sass(command: &mut Command) -> Output {
30 command
31 .arg("style.scss")
32 .arg("static/style.css")
33 .output()
34 .expect("Unable to compile SASS file, install SASS, see https://sass-lang.com/")
35 }
36
37 let output = if exists_in_path("sass.bat") {
38 run_sass(Command::new("cmd").args(&["/C", "sass.bat"]))
39 } else {
40 run_sass(&mut Command::new("sass"))
41 };
42
43 if !output.status.success() {
44 // SASS will put the error in the file.
45 let error =
46 std::fs::read_to_string("./static/style.css").expect("unable to read style.css");
47 panic!("{}", error);
48 }
49 }