rustfmt
[recipes.git] / backend / build.rs
index 48175fa..45ca9a5 100644 (file)
@@ -5,21 +5,45 @@ What is build here:
  - Compile the SASS file to CSS file.
 */
 
-use std::process::Command;
+use std::{
+    env,
+    path::Path,
+    process::{Command, Output},
+};
+
+fn exists_in_path<P>(filename: P) -> bool
+where
+    P: AsRef<Path>,
+{
+    for path in env::split_paths(&env::var_os("PATH").unwrap()) {
+        if path.join(&filename).is_file() {
+            return true;
+        }
+    }
+    false
+}
 
 fn main() {
     println!("cargo:rerun-if-changed=style.scss");
 
-    let output =
-        Command::new("sass")
-            .arg("./style.scss")
-            .arg("./static/style.css")
+    fn run_sass(command: &mut Command) -> Output {
+        command
+            .arg("style.scss")
+            .arg("static/style.css")
             .output()
-            .expect("Unable to compile SASS file, install SASS, see https://sass-lang.com/");
+            .expect("Unable to compile SASS file, install SASS, see https://sass-lang.com/")
+    }
+
+    let output = if exists_in_path("sass.bat") {
+        run_sass(Command::new("cmd").args(&["/C", "sass.bat"]))
+    } else {
+        run_sass(&mut Command::new("sass"))
+    };
 
     if !output.status.success() {
-        //panic!("Unable to compile SASS file, install SASS, see https://sass-lang.com/")
-        let error = std::fs::read_to_string("./static/style.css").expect("unable to read style.css");
-        panic!(error);
+        // SASS will put the error in the file.
+        let error =
+            std::fs::read_to_string("./static/style.css").expect("unable to read style.css");
+        panic!("{}", error);
     }
-}
\ No newline at end of file
+}