From 032c9f4cbd0e8361a57135daa64c145e76c0e63a Mon Sep 17 00:00:00 2001 From: Ashley Williams Date: Tue, 16 Jul 2019 17:29:08 -0500 Subject: [PATCH] feat(bindgen): check wasm-bindgen cli version when running target web --- Cargo.lock | 1 + Cargo.toml | 1 + src/bindgen.rs | 27 ++++++++++++++++++--------- src/lib.rs | 1 + tests/all/build.rs | 12 +++++++++--- 5 files changed, 30 insertions(+), 12 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 4e26d13..8a27240 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1987,6 +1987,7 @@ dependencies = [ "parking_lot 0.6.4 (registry+https://github.com/rust-lang/crates.io-index)", "predicates 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)", "reqwest 0.9.14 (registry+https://github.com/rust-lang/crates.io-index)", + "semver 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)", "serde 1.0.89 (registry+https://github.com/rust-lang/crates.io-index)", "serde_derive 1.0.89 (registry+https://github.com/rust-lang/crates.io-index)", "serde_ignored 0.0.4 (registry+https://github.com/rust-lang/crates.io-index)", diff --git a/Cargo.toml b/Cargo.toml index e12044a..d85d70f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -24,6 +24,7 @@ log = "0.4.6" openssl = { version = '0.10.11', optional = true } parking_lot = "0.6" reqwest = "0.9.14" +semver = "0.9.0" serde = "1.0.74" serde_derive = "1.0.74" serde_ignored = "0.0.4" diff --git a/src/bindgen.rs b/src/bindgen.rs index fe50688..898743d 100644 --- a/src/bindgen.rs +++ b/src/bindgen.rs @@ -2,11 +2,11 @@ use binary_install::Download; use child; -use crate::install; use command::build::{BuildProfile, Target}; use failure::{self, ResultExt}; -use install::Tool; +use install; use manifest::CrateData; +use semver; use std::path::{Path, PathBuf}; use std::process::Command; @@ -40,13 +40,20 @@ pub fn wasm_bindgen_build( } else { "--typescript" }; + let bindgen_path = bindgen.binary("wasm-bindgen")?; let target_arg = match target { Target::Nodejs => "--nodejs", Target::NoModules => "--no-modules", - Target::Web => "--web", + Target::Web => { + if supports_web_target(&bindgen_path)? { + "--web" + } else { + bail!("Your current version of wasm-bindgen does not support the 'web' target. Please update your project to wasm-bindgen version >= 0.2.39.") + } + } Target::Bundler => "--browser", }; - let bindgen_path = bindgen.binary("wasm-bindgen")?; + let mut cmd = Command::new(&bindgen_path); cmd.arg(&wasm_path) .arg("--out-dir") @@ -69,14 +76,16 @@ pub fn wasm_bindgen_build( cmd.arg("--keep-debug"); } - let versions_match = install::check_version(&Tool::WasmBindgen, &bindgen_path, "0.2.37")?; - assert!(versions_match, "Something went wrong! wasm-bindgen CLI and dependency version don't match. This is likely not your fault! You should file an issue: https://github.com/rustwasm/wasm-pack/issues/new?template=bug_report.md."); - child::run(cmd, "wasm-bindgen").context("Running the wasm-bindgen CLI")?; Ok(()) } /// Check if the `wasm-bindgen` dependency is locally satisfied. -fn supports_web_target(cli_path: &PathBuf, dep_version: &str) -> Result { - unimplemented!(); +fn supports_web_target(cli_path: &PathBuf) -> Result { + let cli_version = semver::Version::parse(&install::get_cli_version( + &install::Tool::WasmBindgen, + cli_path, + )?)?; + let expected_version = semver::Version::parse("0.2.39")?; + Ok(cli_version >= expected_version) } diff --git a/src/lib.rs b/src/lib.rs index c327d30..7eff5a3 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -9,6 +9,7 @@ extern crate strsim; extern crate failure; extern crate glob; extern crate parking_lot; +extern crate semver; extern crate serde; extern crate which; #[macro_use] diff --git a/tests/all/build.rs b/tests/all/build.rs index 8296d6a..4ae4f9c 100644 --- a/tests/all/build.rs +++ b/tests/all/build.rs @@ -136,14 +136,20 @@ fn dash_dash_web_target_has_error_on_old_bindgen() { "#, ) .install_local_wasm_bindgen(); - fixture + let cmd = fixture .wasm_pack() .arg("build") .arg("--target") .arg("web") .assert() - .success() - .stdout(""); + .failure(); + let output = String::from_utf8(cmd.get_output().stderr.clone()).unwrap(); + + assert!( + output.contains("0.2.39"), + "Output did not contain '0.2.39', output was {}", + output + ); } #[test]