Skip to content

Commit

Permalink
Merge pull request #167 from ashleygwilliams/robertohuertasm-feature-…
Browse files Browse the repository at this point in the history
…wasm-bindgen-detection

Robertohuertasm feature wasm bindgen detection
  • Loading branch information
ashleygwilliams authored Jun 12, 2018
2 parents 11908d7 + 2100c15 commit 06160a1
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 1 deletion.
4 changes: 4 additions & 0 deletions src/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,10 @@ fn init(

let crate_path = set_crate_path(path);

info!(&log, "Checking wasm-bindgen dependency...");
manifest::check_wasm_bindgen(&crate_path)?;
info!(&log, "wasm-bindgen dependency is correctly declared.");

info!(&log, "Adding wasm-target...");
build::rustup_add_wasm_target()?;
info!(&log, "Adding wasm-target was successful.");
Expand Down
19 changes: 19 additions & 0 deletions src/manifest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use PBAR;
#[derive(Deserialize)]
struct CargoManifest {
package: CargoPackage,
dependencies: Option<CargoDependencies>,
lib: Option<CargoLib>,
}

Expand All @@ -24,6 +25,12 @@ struct CargoPackage {
repository: Option<String>,
}

#[derive(Deserialize)]
struct CargoDependencies {
#[serde(rename = "wasm-bindgen")]
wasm_bindgen: Option<String>,
}

#[derive(Deserialize)]
struct CargoLib {
#[serde(rename = "crate-type")]
Expand Down Expand Up @@ -144,6 +151,18 @@ pub fn get_crate_name(path: &str) -> Result<String, Error> {
Ok(read_cargo_toml(path)?.package.name)
}

pub 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()
))
}

fn has_cdylib(path: &str) -> Result<bool, Error> {
Ok(read_cargo_toml(path)?.lib.map_or(false, |lib| {
lib.crate_type
Expand Down
1 change: 0 additions & 1 deletion tests/fixtures/bad-cargo-toml/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,3 @@ authors = ["Michael Gattozzi <mgattozzi@gmail.com>"]
crate-type = ["foo"]

[dependencies]
wasm-bindgen = "0.2"
10 changes: 10 additions & 0 deletions tests/manifest/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,3 +84,13 @@ fn it_creates_a_package_json_provided_path_with_scope() {
let pkg = utils::read_package_json(&path).unwrap();
assert_eq!(pkg.name, "@test/scopes-hello-world");
}

#[test]
fn it_errors_when_wasm_bindgen_is_not_declared() {
assert!(manifest::check_wasm_bindgen("tests/fixtures/bad-cargo-toml").is_err());
}

#[test]
fn it_does_not_error_when_wasm_bindgen_is_declared() {
assert!(manifest::check_wasm_bindgen("tests/fixtures/js-hello-world").is_ok());
}

0 comments on commit 06160a1

Please sign in to comment.