From c002256dfda95647473db2c020978f56d06fdcce Mon Sep 17 00:00:00 2001 From: data-pup Date: Mon, 18 Jun 2018 20:01:51 -0400 Subject: [PATCH] Solves #146. Check locally installed wasm-bindgen dependency. --- src/bindgen.rs | 36 +++++++++++++++++++++++++++++++++--- src/command/init.rs | 22 +++++++++++++++++++--- src/manifest.rs | 7 +++++++ 3 files changed, 59 insertions(+), 6 deletions(-) diff --git a/src/bindgen.rs b/src/bindgen.rs index 7405dde3b..0a6b50622 100644 --- a/src/bindgen.rs +++ b/src/bindgen.rs @@ -1,16 +1,46 @@ use emoji; use error::Error; use progressbar::Step; -use std::process::Command; +use std::{path, process::Command}; use PBAR; -pub fn cargo_install_wasm_bindgen(step: &Step) -> Result<(), Error> { +static LOCAL_BINDGEN_PATH: &str = "bin/wasm-bindgen"; + +fn wasm_bindgen_exists_locally(crate_path: &str) -> bool { + let path_str = format!("{}/{}", crate_path, LOCAL_BINDGEN_PATH); + let bindgen_abs_path = path::Path::new(&path_str); + bindgen_abs_path.is_file() +} + +pub fn wasm_bindgen_version_check(crate_path: &str, dep_version: &str) -> Result { + if !wasm_bindgen_exists_locally(crate_path) { + return Ok(false); + } + + let output = Command::new(LOCAL_BINDGEN_PATH).arg("--version").output()?; + if output.status.success() { + let stdout = String::from_utf8_lossy(&output.stdout); + let installed_version = stdout.trim(); + Ok(dep_version == installed_version) + } else { + let message = "Could not find version of local wasm-bindgen".to_string(); + let stderr = String::from_utf8_lossy(&output.stderr).to_string(); + let e = Error::Cli { message, stderr }; + Err(e) + } +} + +pub fn cargo_install_wasm_bindgen(path: &str, version: &str, step: &Step) -> Result<(), Error> { let msg = format!("{}Installing WASM-bindgen...", emoji::DOWN_ARROW); PBAR.step(step, &msg)?; let output = Command::new("cargo") .arg("install") - .arg("wasm-bindgen-cli") .arg("--force") + .arg("wasm-bindgen-cli") + .arg("--version") + .arg(version) + .arg("--root") + .arg(path) .output()?; if !output.status.success() { let s = String::from_utf8_lossy(&output.stderr); diff --git a/src/command/init.rs b/src/command/init.rs index c409a8659..f2211b0b4 100644 --- a/src/command/init.rs +++ b/src/command/init.rs @@ -188,9 +188,25 @@ impl Init { step: &Step, log: &Logger, ) -> result::Result<(), Error> { - info!(&log, "Installing wasm-bindgen-cli..."); - bindgen::cargo_install_wasm_bindgen(step)?; - info!(&log, "Installing wasm-bindgen-cli was successful."); + let bindgen_version = + manifest::get_wasm_bindgen_version(&self.crate_path)?.ok_or(Error::CrateConfig { + message: "Unexpected error while parsing wasm-bindgen dependency version" + .to_string(), + })?; + + if bindgen_version.is_empty() { + let e = Error::CrateConfig { + message: "wasm-bindgen version dependency was empty".to_string(), + }; + return Err(e); + } + + info!(&log, "Checking WASM-bindgen version..."); + if !bindgen::wasm_bindgen_version_check(&self.crate_path, &bindgen_version)? { + info!(&log, "Installing wasm-bindgen-cli..."); + bindgen::cargo_install_wasm_bindgen(&self.crate_path, &bindgen_version, step)?; + info!(&log, "Installing wasm-bindgen-cli was successful."); + } info!(&log, "Getting the crate name from the manifest..."); self.crate_name = Some(manifest::get_crate_name(&self.crate_path)?); diff --git a/src/manifest.rs b/src/manifest.rs index 22c95c4bb..718832c99 100644 --- a/src/manifest.rs +++ b/src/manifest.rs @@ -148,6 +148,13 @@ pub fn get_crate_name(path: &str) -> Result { Ok(read_cargo_toml(path)?.package.name) } +pub fn get_wasm_bindgen_version(path: &str) -> Result, Error> { + let version: Option = read_cargo_toml(path)? + .dependencies + .and_then(|deps| deps.wasm_bindgen); + Ok(version) +} + pub fn check_crate_config(path: &str, step: &Step) -> Result<(), Error> { let msg = format!("{}Checking crate configuration...", emoji::WRENCH); PBAR.step(&step, &msg)?;