Skip to content

Commit c002256

Browse files
committed
Solves #146. Check locally installed wasm-bindgen dependency.
1 parent 1c50b12 commit c002256

File tree

3 files changed

+59
-6
lines changed

3 files changed

+59
-6
lines changed

src/bindgen.rs

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,46 @@
11
use emoji;
22
use error::Error;
33
use progressbar::Step;
4-
use std::process::Command;
4+
use std::{path, process::Command};
55
use PBAR;
66

7-
pub fn cargo_install_wasm_bindgen(step: &Step) -> Result<(), Error> {
7+
static LOCAL_BINDGEN_PATH: &str = "bin/wasm-bindgen";
8+
9+
fn wasm_bindgen_exists_locally(crate_path: &str) -> bool {
10+
let path_str = format!("{}/{}", crate_path, LOCAL_BINDGEN_PATH);
11+
let bindgen_abs_path = path::Path::new(&path_str);
12+
bindgen_abs_path.is_file()
13+
}
14+
15+
pub fn wasm_bindgen_version_check(crate_path: &str, dep_version: &str) -> Result<bool, Error> {
16+
if !wasm_bindgen_exists_locally(crate_path) {
17+
return Ok(false);
18+
}
19+
20+
let output = Command::new(LOCAL_BINDGEN_PATH).arg("--version").output()?;
21+
if output.status.success() {
22+
let stdout = String::from_utf8_lossy(&output.stdout);
23+
let installed_version = stdout.trim();
24+
Ok(dep_version == installed_version)
25+
} else {
26+
let message = "Could not find version of local wasm-bindgen".to_string();
27+
let stderr = String::from_utf8_lossy(&output.stderr).to_string();
28+
let e = Error::Cli { message, stderr };
29+
Err(e)
30+
}
31+
}
32+
33+
pub fn cargo_install_wasm_bindgen(path: &str, version: &str, step: &Step) -> Result<(), Error> {
834
let msg = format!("{}Installing WASM-bindgen...", emoji::DOWN_ARROW);
935
PBAR.step(step, &msg)?;
1036
let output = Command::new("cargo")
1137
.arg("install")
12-
.arg("wasm-bindgen-cli")
1338
.arg("--force")
39+
.arg("wasm-bindgen-cli")
40+
.arg("--version")
41+
.arg(version)
42+
.arg("--root")
43+
.arg(path)
1444
.output()?;
1545
if !output.status.success() {
1646
let s = String::from_utf8_lossy(&output.stderr);

src/command/init.rs

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -188,9 +188,25 @@ impl Init {
188188
step: &Step,
189189
log: &Logger,
190190
) -> result::Result<(), Error> {
191-
info!(&log, "Installing wasm-bindgen-cli...");
192-
bindgen::cargo_install_wasm_bindgen(step)?;
193-
info!(&log, "Installing wasm-bindgen-cli was successful.");
191+
let bindgen_version =
192+
manifest::get_wasm_bindgen_version(&self.crate_path)?.ok_or(Error::CrateConfig {
193+
message: "Unexpected error while parsing wasm-bindgen dependency version"
194+
.to_string(),
195+
})?;
196+
197+
if bindgen_version.is_empty() {
198+
let e = Error::CrateConfig {
199+
message: "wasm-bindgen version dependency was empty".to_string(),
200+
};
201+
return Err(e);
202+
}
203+
204+
info!(&log, "Checking WASM-bindgen version...");
205+
if !bindgen::wasm_bindgen_version_check(&self.crate_path, &bindgen_version)? {
206+
info!(&log, "Installing wasm-bindgen-cli...");
207+
bindgen::cargo_install_wasm_bindgen(&self.crate_path, &bindgen_version, step)?;
208+
info!(&log, "Installing wasm-bindgen-cli was successful.");
209+
}
194210

195211
info!(&log, "Getting the crate name from the manifest...");
196212
self.crate_name = Some(manifest::get_crate_name(&self.crate_path)?);

src/manifest.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,13 @@ pub fn get_crate_name(path: &str) -> Result<String, Error> {
148148
Ok(read_cargo_toml(path)?.package.name)
149149
}
150150

151+
pub fn get_wasm_bindgen_version(path: &str) -> Result<Option<String>, Error> {
152+
let version: Option<String> = read_cargo_toml(path)?
153+
.dependencies
154+
.and_then(|deps| deps.wasm_bindgen);
155+
Ok(version)
156+
}
157+
151158
pub fn check_crate_config(path: &str, step: &Step) -> Result<(), Error> {
152159
let msg = format!("{}Checking crate configuration...", emoji::WRENCH);
153160
PBAR.step(&step, &msg)?;

0 commit comments

Comments
 (0)