Skip to content

Commit 0ba6e5e

Browse files
committed
Solves #146. Check locally installed wasm-bindgen dependency.
1 parent faed391 commit 0ba6e5e

File tree

7 files changed

+107
-34
lines changed

7 files changed

+107
-34
lines changed

src/bindgen.rs

Lines changed: 50 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,67 @@
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+
#[cfg(not(target_family = "windows"))]
8+
static LOCAL_BINDGEN_PATH: &str = "bin/wasm-bindgen";
9+
10+
#[cfg(target_family = "windows")]
11+
static LOCAL_BINDGEN_PATH: &str = "bin\\wasm-bindgen";
12+
13+
fn local_wasm_bindgen_path_str(crate_path: &str) -> String {
14+
#[cfg(not(target_family = "windows"))]
15+
return format!("{}/{}", crate_path, LOCAL_BINDGEN_PATH);
16+
#[cfg(target_family = "windows")]
17+
return format!("{}\\{}", crate_path, LOCAL_BINDGEN_PATH);
18+
}
19+
20+
pub fn wasm_bindgen_version_check(
21+
crate_path: &str,
22+
dep_version: &str,
23+
step: &Step,
24+
) -> Result<bool, Error> {
25+
let msg = format!("{}Checking WASM-bindgen dependency...", emoji::CHECK);
26+
PBAR.step(step, &msg);
27+
28+
let wasm_bindgen = local_wasm_bindgen_path_str(crate_path);
29+
if !path::Path::new(&wasm_bindgen).is_file() {
30+
return Ok(false);
31+
}
32+
33+
let output = Command::new(wasm_bindgen).arg("--version").output()?;
34+
if output.status.success() {
35+
let s = String::from_utf8_lossy(&output.stdout);
36+
let installed_version = s.trim();
37+
Ok(installed_version == dep_version)
38+
} else {
39+
let error_msg = "Could not find version of local wasm-bindgen";
40+
let s = String::from_utf8_lossy(&output.stderr);
41+
let e = Error::cli(error_msg, s);
42+
Err(e)
43+
}
44+
}
45+
46+
pub fn cargo_install_wasm_bindgen(path: &str, version: &str, step: &Step) -> Result<(), Error> {
847
let msg = format!("{}Installing WASM-bindgen...", emoji::DOWN_ARROW);
948
PBAR.step(step, &msg);
1049
let output = Command::new("cargo")
1150
.arg("install")
12-
.arg("wasm-bindgen-cli")
1351
.arg("--force")
52+
.arg("wasm-bindgen-cli")
53+
.arg("--version")
54+
.arg(version)
55+
.arg("--root")
56+
.arg(path)
1457
.output()?;
1558
if !output.status.success() {
1659
let s = String::from_utf8_lossy(&output.stderr);
1760
if s.contains("already exists") {
1861
PBAR.info("wasm-bindgen already installed");
1962
return Ok(());
2063
}
21-
Error::cli("Installing wasm-bindgen failed", s)
64+
Err(Error::cli("Installing wasm-bindgen failed", s))
2265
} else {
2366
Ok(())
2467
}
@@ -51,7 +94,8 @@ pub fn wasm_bindgen_build(
5194
_ => "--browser",
5295
};
5396

54-
let output = Command::new("wasm-bindgen")
97+
let wasm_bindgen = local_wasm_bindgen_path_str(path);
98+
let output = Command::new(wasm_bindgen)
5599
.current_dir(path)
56100
.arg(&wasm_path)
57101
.arg("--out-dir")
@@ -61,7 +105,7 @@ pub fn wasm_bindgen_build(
61105
.output()?;
62106
if !output.status.success() {
63107
let s = String::from_utf8_lossy(&output.stderr);
64-
Error::cli("wasm-bindgen failed to execute properly", s)
108+
Err(Error::cli("wasm-bindgen failed to execute properly", s))
65109
} else {
66110
Ok(())
67111
}

src/build.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,10 @@ pub fn rustup_add_wasm_target(step: &Step) -> Result<(), Error> {
1717
.output()?;
1818
if !output.status.success() {
1919
let s = String::from_utf8_lossy(&output.stderr);
20-
Error::cli("Adding the wasm32-unknown-unknown target failed", s)
20+
Err(Error::cli(
21+
"Adding the wasm32-unknown-unknown target failed",
22+
s,
23+
))
2124
} else {
2225
Ok(())
2326
}
@@ -33,7 +36,7 @@ fn ensure_nightly() -> Result<(), Error> {
3336
.output()?;
3437
if !res.status.success() {
3538
let s = String::from_utf8_lossy(&res.stderr);
36-
return Error::cli("Adding the nightly toolchain failed", s);
39+
return Err(Error::cli("Adding the nightly toolchain failed", s));
3740
}
3841
}
3942
Ok(())
@@ -54,7 +57,7 @@ pub fn cargo_build_wasm(path: &str, debug: bool, step: &Step) -> Result<(), Erro
5457

5558
if !output.status.success() {
5659
let s = String::from_utf8_lossy(&output.stderr);
57-
Error::cli("Compilation of your program failed", s)
60+
Err(Error::cli("Compilation of your program failed", s))
5861
} else {
5962
Ok(())
6063
}

src/command/init.rs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -192,9 +192,15 @@ impl Init {
192192
}
193193

194194
fn step_install_wasm_bindgen(&mut self, step: &Step, log: &Logger) -> Result<(), Error> {
195-
info!(&log, "Installing wasm-bindgen-cli...");
196-
bindgen::cargo_install_wasm_bindgen(step)?;
197-
info!(&log, "Installing wasm-bindgen-cli was successful.");
195+
info!(&log, "Checking WASM-bindgen version...");
196+
let bindgen_version = manifest::get_wasm_bindgen_version(&self.crate_path)?;
197+
let bindgen_installed =
198+
bindgen::wasm_bindgen_version_check(&self.crate_path, &bindgen_version, step)?;
199+
if !bindgen_installed {
200+
info!(&log, "Installing wasm-bindgen-cli...");
201+
bindgen::cargo_install_wasm_bindgen(&self.crate_path, &bindgen_version, step)?;
202+
info!(&log, "Installing wasm-bindgen-cli was successful.");
203+
}
198204

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

src/emoji.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,4 @@ pub static DANCERS: Emoji = Emoji("👯 ", "");
1313
pub static ERROR: Emoji = Emoji("⛔ ", "");
1414
pub static INFO: Emoji = Emoji("ℹ️ ", "");
1515
pub static WRENCH: Emoji = Emoji("🔧 ", "");
16+
pub static CHECK: Emoji = Emoji("✓ ", "");

src/error.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,17 @@ pub enum Error {
2020
}
2121

2222
impl Error {
23-
pub fn cli(message: &str, stderr: Cow<str>) -> Result<(), Self> {
24-
Err(Error::Cli {
23+
pub fn cli(message: &str, stderr: Cow<str>) -> Self {
24+
Error::Cli {
2525
message: message.to_string(),
2626
stderr: stderr.to_string(),
27-
})
27+
}
2828
}
2929

30-
pub fn crate_config(message: &str) -> Result<(), Self> {
31-
Err(Error::CrateConfig {
30+
pub fn crate_config(message: &str) -> Self {
31+
Error::CrateConfig {
3232
message: message.to_string(),
33-
})
33+
}
3434
}
3535

3636
pub fn error_type(&self) -> String {

src/manifest.rs

Lines changed: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,30 @@ 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<String, Error> {
152+
match read_cargo_toml(path)?
153+
.dependencies
154+
.and_then(|deps| deps.wasm_bindgen)
155+
{
156+
Some(ref version) if version.is_empty() => {
157+
let msg = format!(
158+
"\"{}\" dependency is missing its version number",
159+
style("wasm-bindgen").bold().dim()
160+
);
161+
let e = Error::crate_config(&msg);
162+
Err(e)
163+
}
164+
Some(version) => Ok(version),
165+
None => {
166+
let msg = format!(
167+
"Ensure that you have \"{}\" as a dependency in your Cargo.toml file:\n[dependencies]\nwasm-bindgen = \"0.2\"",
168+
style("wasm-bindgen").bold().dim());
169+
let e = Error::crate_config(&msg);
170+
Err(e)
171+
}
172+
}
173+
}
174+
151175
pub fn check_crate_config(path: &str, step: &Step) -> Result<(), Error> {
152176
let msg = format!("{}Checking crate configuration...", emoji::WRENCH);
153177
PBAR.step(&step, &msg);
@@ -157,25 +181,19 @@ pub fn check_crate_config(path: &str, step: &Step) -> Result<(), Error> {
157181
}
158182

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

171188
fn check_crate_type(path: &str) -> Result<(), Error> {
172189
if read_cargo_toml(path)?.lib.map_or(false, |lib| {
173190
lib.crate_type
174191
.map_or(false, |types| types.iter().any(|s| s == "cdylib"))
175192
}) {
176-
return Ok(());
193+
Ok(())
194+
} else {
195+
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\"]";
196+
let e = Error::crate_config(msg);
197+
Err(e)
177198
}
178-
Error::crate_config(
179-
"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\"]"
180-
)
181199
}

src/npm.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ pub fn npm_pack(path: &str) -> Result<(), Error> {
1111
.output()?;
1212
if !output.status.success() {
1313
let s = String::from_utf8_lossy(&output.stderr);
14-
Error::cli("Packaging up your code failed", s)
14+
Err(Error::cli("Packaging up your code failed", s))
1515
} else {
1616
Ok(())
1717
}
@@ -25,7 +25,7 @@ pub fn npm_publish(path: &str) -> Result<(), Error> {
2525
.output()?;
2626
if !output.status.success() {
2727
let s = String::from_utf8_lossy(&output.stderr);
28-
Error::cli("Publishing to npm failed", s)
28+
Err(Error::cli("Publishing to npm failed", s))
2929
} else {
3030
Ok(())
3131
}
@@ -61,8 +61,9 @@ pub fn npm_login(
6161
.output()?;
6262

6363
if !output.status.success() {
64+
let message = format!("Login to registry {} failed", registry);
6465
let s = String::from_utf8_lossy(&output.stderr);
65-
Error::cli(&format!("Login to registry {} failed", registry), s)
66+
Err(Error::cli(&message, s))
6667
} else {
6768
Ok(())
6869
}

0 commit comments

Comments
 (0)