From 90c9a3a2f82b109b066ab3890d46bad8665a8047 Mon Sep 17 00:00:00 2001 From: Amin Yahyaabadi Date: Sun, 4 Apr 2021 21:16:53 -0500 Subject: [PATCH] Refactor get_emcc_path and use PathBuf --- cli/src/wasm.rs | 38 +++++++++++++++++++++++++------------- 1 file changed, 25 insertions(+), 13 deletions(-) diff --git a/cli/src/wasm.rs b/cli/src/wasm.rs index d54272f1..bca6822e 100644 --- a/cli/src/wasm.rs +++ b/cli/src/wasm.rs @@ -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 { + 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()); +}