From 0ae70289ae3ad214ce008751b3c4744a44f7fdde Mon Sep 17 00:00:00 2001 From: Andrew Hlynskyi Date: Sun, 23 May 2021 13:53:07 +0300 Subject: [PATCH] fix(cli): Correct fallback on docker compilation for all platforms, fixes #1124 --- cli/src/wasm.rs | 37 ++++++++----------------------------- 1 file changed, 8 insertions(+), 29 deletions(-) diff --git a/cli/src/wasm.rs b/cli/src/wasm.rs index bca6822e..c2175938 100644 --- a/cli/src/wasm.rs +++ b/cli/src/wasm.rs @@ -3,7 +3,6 @@ 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; @@ -23,15 +22,15 @@ pub fn compile_language_to_wasm(language_dir: &Path, force_docker: bool) -> Resu let grammar_name = get_grammar_name(&src_dir)?; let output_filename = format!("tree-sitter-{}.wasm", grammar_name); + let emcc_bin = if cfg!(windows) { "emcc.bat" } else { "emcc" }; + let emcc_path = which(emcc_bin) + .ok() + .and_then(|p| Command::new(&p).output().and(Ok(p)).ok()); + let mut command; - if !force_docker { - let emcc_path = get_emcc_path(); - if emcc_path.is_ok() { - command = Command::new(emcc_path.unwrap()); - command.current_dir(&language_dir); - } else { - return Err(emcc_path.unwrap_err()); - } + if !force_docker && emcc_path.is_some() { + command = Command::new(emcc_path.unwrap()); + command.current_dir(&language_dir); } else if Command::new("docker").output().is_ok() { command = Command::new("docker"); command.args(&["run", "--rm"]); @@ -123,23 +122,3 @@ 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()); -}