Skip to content

Commit

Permalink
Solves #146. Check locally installed wasm-bindgen dependency.
Browse files Browse the repository at this point in the history
  • Loading branch information
data-pup committed Jun 19, 2018
1 parent faed391 commit 2060191
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 7 deletions.
36 changes: 33 additions & 3 deletions src/bindgen.rs
Original file line number Diff line number Diff line change
@@ -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<bool, Error> {
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);
Expand Down
28 changes: 24 additions & 4 deletions src/command/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -191,10 +191,30 @@ impl Init {
Ok(())
}

fn step_install_wasm_bindgen(&mut self, step: &Step, log: &Logger) -> Result<(), Error> {
info!(&log, "Installing wasm-bindgen-cli...");
bindgen::cargo_install_wasm_bindgen(step)?;
info!(&log, "Installing wasm-bindgen-cli was successful.");
fn step_install_wasm_bindgen(
&mut self,
step: &Step,
log: &Logger,
) -> Result<(), Error> {
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 = manifest::get_crate_name(&self.crate_path)?;
Expand Down
7 changes: 7 additions & 0 deletions src/manifest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,13 @@ pub fn get_crate_name(path: &str) -> Result<String, Error> {
Ok(read_cargo_toml(path)?.package.name)
}

pub fn get_wasm_bindgen_version(path: &str) -> Result<Option<String>, Error> {
let version: Option<String> = 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);
Expand Down

0 comments on commit 2060191

Please sign in to comment.