Refactor get_emcc_path and use PathBuf

This commit is contained in:
Amin Yahyaabadi 2021-04-04 21:16:53 -05:00
parent 919eab023f
commit 90c9a3a2f8

View file

@ -3,6 +3,7 @@ use super::generate::parse_grammar::GrammarJSON;
use std::ffi::{OsStr, OsString};
use std::fs;
use std::path::Path;
use std::path::PathBuf;
use std::process::Command;
use which::which;
@ -24,21 +25,12 @@ pub fn compile_language_to_wasm(language_dir: &Path, force_docker: bool) -> Resu
let mut command;
if !force_docker {
let emcc_bin;
if cfg!(windows) {
emcc_bin = "emcc.bat"
} else {
emcc_bin = "emcc"
};
let emcc_which = which(emcc_bin).unwrap();
let emcc_path = emcc_which.to_str().unwrap();
if Command::new(emcc_path).output().is_ok() {
command = Command::new(emcc_path);
let emcc_path = get_emcc_path();
if emcc_path.is_ok() {
command = Command::new(emcc_path.unwrap());
command.current_dir(&language_dir);
} else {
return Error::err(
"You must have either emcc or docker on your PATH to run this command".to_string(),
);
return Err(emcc_path.unwrap_err());
}
} else if Command::new("docker").output().is_ok() {
command = Command::new("docker");
@ -131,3 +123,23 @@ pub fn compile_language_to_wasm(language_dir: &Path, force_docker: bool) -> Resu
Ok(())
}
fn get_emcc_path() -> Result<PathBuf> {
let emcc_bin;
if cfg!(windows) {
emcc_bin = "emcc.bat";
} else {
emcc_bin = "emcc";
};
let emcc_which = which(emcc_bin);
let emcc_path;
if emcc_which.is_ok() {
emcc_path = emcc_which.unwrap();
} else {
return Error::err("emcc was not found on PATH".to_string());
}
if Command::new(&emcc_path).output().is_ok() {
return Ok(emcc_path);
}
return Error::err("emcc binary doesn't work properly".to_string());
}