Skip to content

Commit

Permalink
Solves rustwasm#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 20, 2018
1 parent faed391 commit 0ba6e5e
Show file tree
Hide file tree
Showing 7 changed files with 107 additions and 34 deletions.
56 changes: 50 additions & 6 deletions src/bindgen.rs
Original file line number Diff line number Diff line change
@@ -1,24 +1,67 @@
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> {
#[cfg(not(target_family = "windows"))]
static LOCAL_BINDGEN_PATH: &str = "bin/wasm-bindgen";

#[cfg(target_family = "windows")]
static LOCAL_BINDGEN_PATH: &str = "bin\\wasm-bindgen";

fn local_wasm_bindgen_path_str(crate_path: &str) -> String {
#[cfg(not(target_family = "windows"))]
return format!("{}/{}", crate_path, LOCAL_BINDGEN_PATH);
#[cfg(target_family = "windows")]
return format!("{}\\{}", crate_path, LOCAL_BINDGEN_PATH);
}

pub fn wasm_bindgen_version_check(
crate_path: &str,
dep_version: &str,
step: &Step,
) -> Result<bool, Error> {
let msg = format!("{}Checking WASM-bindgen dependency...", emoji::CHECK);
PBAR.step(step, &msg);

let wasm_bindgen = local_wasm_bindgen_path_str(crate_path);
if !path::Path::new(&wasm_bindgen).is_file() {
return Ok(false);
}

let output = Command::new(wasm_bindgen).arg("--version").output()?;
if output.status.success() {
let s = String::from_utf8_lossy(&output.stdout);
let installed_version = s.trim();
Ok(installed_version == dep_version)
} else {
let error_msg = "Could not find version of local wasm-bindgen";
let s = String::from_utf8_lossy(&output.stderr);
let e = Error::cli(error_msg, s);
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);
if s.contains("already exists") {
PBAR.info("wasm-bindgen already installed");
return Ok(());
}
Error::cli("Installing wasm-bindgen failed", s)
Err(Error::cli("Installing wasm-bindgen failed", s))
} else {
Ok(())
}
Expand Down Expand Up @@ -51,7 +94,8 @@ pub fn wasm_bindgen_build(
_ => "--browser",
};

let output = Command::new("wasm-bindgen")
let wasm_bindgen = local_wasm_bindgen_path_str(path);
let output = Command::new(wasm_bindgen)
.current_dir(path)
.arg(&wasm_path)
.arg("--out-dir")
Expand All @@ -61,7 +105,7 @@ pub fn wasm_bindgen_build(
.output()?;
if !output.status.success() {
let s = String::from_utf8_lossy(&output.stderr);
Error::cli("wasm-bindgen failed to execute properly", s)
Err(Error::cli("wasm-bindgen failed to execute properly", s))
} else {
Ok(())
}
Expand Down
9 changes: 6 additions & 3 deletions src/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@ pub fn rustup_add_wasm_target(step: &Step) -> Result<(), Error> {
.output()?;
if !output.status.success() {
let s = String::from_utf8_lossy(&output.stderr);
Error::cli("Adding the wasm32-unknown-unknown target failed", s)
Err(Error::cli(
"Adding the wasm32-unknown-unknown target failed",
s,
))
} else {
Ok(())
}
Expand All @@ -33,7 +36,7 @@ fn ensure_nightly() -> Result<(), Error> {
.output()?;
if !res.status.success() {
let s = String::from_utf8_lossy(&res.stderr);
return Error::cli("Adding the nightly toolchain failed", s);
return Err(Error::cli("Adding the nightly toolchain failed", s));
}
}
Ok(())
Expand All @@ -54,7 +57,7 @@ pub fn cargo_build_wasm(path: &str, debug: bool, step: &Step) -> Result<(), Erro

if !output.status.success() {
let s = String::from_utf8_lossy(&output.stderr);
Error::cli("Compilation of your program failed", s)
Err(Error::cli("Compilation of your program failed", s))
} else {
Ok(())
}
Expand Down
12 changes: 9 additions & 3 deletions src/command/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -192,9 +192,15 @@ impl Init {
}

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.");
info!(&log, "Checking WASM-bindgen version...");
let bindgen_version = manifest::get_wasm_bindgen_version(&self.crate_path)?;
let bindgen_installed =
bindgen::wasm_bindgen_version_check(&self.crate_path, &bindgen_version, step)?;
if !bindgen_installed {
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
1 change: 1 addition & 0 deletions src/emoji.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ pub static DANCERS: Emoji = Emoji("👯 ", "");
pub static ERROR: Emoji = Emoji("⛔ ", "");
pub static INFO: Emoji = Emoji("ℹ️ ", "");
pub static WRENCH: Emoji = Emoji("🔧 ", "");
pub static CHECK: Emoji = Emoji("✓ ", "");
12 changes: 6 additions & 6 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,17 @@ pub enum Error {
}

impl Error {
pub fn cli(message: &str, stderr: Cow<str>) -> Result<(), Self> {
Err(Error::Cli {
pub fn cli(message: &str, stderr: Cow<str>) -> Self {
Error::Cli {
message: message.to_string(),
stderr: stderr.to_string(),
})
}
}

pub fn crate_config(message: &str) -> Result<(), Self> {
Err(Error::CrateConfig {
pub fn crate_config(message: &str) -> Self {
Error::CrateConfig {
message: message.to_string(),
})
}
}

pub fn error_type(&self) -> String {
Expand Down
44 changes: 31 additions & 13 deletions src/manifest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,30 @@ 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<String, Error> {
match read_cargo_toml(path)?
.dependencies
.and_then(|deps| deps.wasm_bindgen)
{
Some(ref version) if version.is_empty() => {
let msg = format!(
"\"{}\" dependency is missing its version number",
style("wasm-bindgen").bold().dim()
);
let e = Error::crate_config(&msg);
Err(e)
}
Some(version) => Ok(version),
None => {
let msg = format!(
"Ensure that you have \"{}\" as a dependency in your Cargo.toml file:\n[dependencies]\nwasm-bindgen = \"0.2\"",
style("wasm-bindgen").bold().dim());
let e = Error::crate_config(&msg);
Err(e)
}
}
}

pub fn check_crate_config(path: &str, step: &Step) -> Result<(), Error> {
let msg = format!("{}Checking crate configuration...", emoji::WRENCH);
PBAR.step(&step, &msg);
Expand All @@ -157,25 +181,19 @@ pub fn check_crate_config(path: &str, step: &Step) -> Result<(), Error> {
}

fn check_wasm_bindgen(path: &str) -> Result<(), Error> {
if read_cargo_toml(path)?.dependencies.map_or(false, |x| {
!x.wasm_bindgen.unwrap_or("".to_string()).is_empty()
}) {
return Ok(());
}
Error::crate_config(&format!(
"Ensure that you have \"{}\" as a dependency in your Cargo.toml file:\n[dependencies]\nwasm-bindgen = \"0.2\"",
style("wasm-bindgen").bold().dim()
))
let _ = get_wasm_bindgen_version(path)?;
Ok(())
}

fn check_crate_type(path: &str) -> Result<(), Error> {
if read_cargo_toml(path)?.lib.map_or(false, |lib| {
lib.crate_type
.map_or(false, |types| types.iter().any(|s| s == "cdylib"))
}) {
return Ok(());
Ok(())
} else {
let msg = "crate-type must be cdylib to compile to wasm32-unknown-unknown. Add the following to your Cargo.toml file:\n\n[lib]\ncrate-type = [\"cdylib\"]";
let e = Error::crate_config(msg);
Err(e)
}
Error::crate_config(
"crate-type must be cdylib to compile to wasm32-unknown-unknown. Add the following to your Cargo.toml file:\n\n[lib]\ncrate-type = [\"cdylib\"]"
)
}
7 changes: 4 additions & 3 deletions src/npm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ pub fn npm_pack(path: &str) -> Result<(), Error> {
.output()?;
if !output.status.success() {
let s = String::from_utf8_lossy(&output.stderr);
Error::cli("Packaging up your code failed", s)
Err(Error::cli("Packaging up your code failed", s))
} else {
Ok(())
}
Expand All @@ -25,7 +25,7 @@ pub fn npm_publish(path: &str) -> Result<(), Error> {
.output()?;
if !output.status.success() {
let s = String::from_utf8_lossy(&output.stderr);
Error::cli("Publishing to npm failed", s)
Err(Error::cli("Publishing to npm failed", s))
} else {
Ok(())
}
Expand Down Expand Up @@ -61,8 +61,9 @@ pub fn npm_login(
.output()?;

if !output.status.success() {
let message = format!("Login to registry {} failed", registry);
let s = String::from_utf8_lossy(&output.stderr);
Error::cli(&format!("Login to registry {} failed", registry), s)
Err(Error::cli(&message, s))
} else {
Ok(())
}
Expand Down

0 comments on commit 0ba6e5e

Please sign in to comment.