From 92b18f05bb54a32d408fb4c18938b841fd4f7ef1 Mon Sep 17 00:00:00 2001 From: Roberto Huertas <roberto.huertas@outlook.com> Date: Wed, 6 Jun 2018 02:04:52 +0200 Subject: [PATCH 1/2] feat(wasm_bindgen): checks wasm-bindgen declaration in Cargo.toml --- src/command.rs | 4 ++++ src/error.rs | 11 +++++++++++ src/manifest.rs | 19 +++++++++++++++++++ tests/fixtures/bad-cargo-toml/Cargo.toml | 1 - tests/manifest/main.rs | 10 ++++++++++ 5 files changed, 44 insertions(+), 1 deletion(-) diff --git a/src/command.rs b/src/command.rs index 4f6023d..58c47ab 100644 --- a/src/command.rs +++ b/src/command.rs @@ -169,6 +169,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."); diff --git a/src/error.rs b/src/error.rs index 7d84d7c..0bd2ba6 100644 --- a/src/error.rs +++ b/src/error.rs @@ -15,6 +15,8 @@ pub enum Error { SerdeToml(#[cause] toml::de::Error), #[fail(display = "{}. stderr:\n\n{}", message, stderr)] Cli { message: String, stderr: String }, + #[fail(display = "{}", message)] + CrateConfig { message: String }, } impl Error { @@ -25,6 +27,12 @@ impl Error { }) } + pub fn crate_config(message: &str) -> Result<(), Self> { + Err(Error::CrateConfig { + message: message.to_string(), + }) + } + pub fn error_type(&self) -> String { match self { Error::Io(_) => "There was an I/O error. Details:\n\n", @@ -34,6 +42,9 @@ impl Error { message: _, stderr: _, } => "There was an error while calling another CLI tool. Details:\n\n", + Error::CrateConfig { message: _ } => { + "There was a crate configuration error. Details:\n\n" + } }.to_string() } } diff --git a/src/manifest.rs b/src/manifest.rs index 8662eea..4db7fd8 100644 --- a/src/manifest.rs +++ b/src/manifest.rs @@ -11,6 +11,7 @@ use PBAR; #[derive(Deserialize)] struct CargoManifest { package: CargoPackage, + dependencies: Option<CargoDependencies>, } #[derive(Deserialize)] @@ -23,6 +24,12 @@ struct CargoPackage { repository: Option<String>, } +#[derive(Deserialize)] +struct CargoDependencies { + #[serde(rename = "wasm-bindgen")] + wasm_bindgen: Option<String>, +} + #[derive(Serialize)] struct NpmPackage { name: String, @@ -136,3 +143,15 @@ pub fn write_package_json( 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() + )) +} diff --git a/tests/fixtures/bad-cargo-toml/Cargo.toml b/tests/fixtures/bad-cargo-toml/Cargo.toml index 95110a2..3d9d45a 100644 --- a/tests/fixtures/bad-cargo-toml/Cargo.toml +++ b/tests/fixtures/bad-cargo-toml/Cargo.toml @@ -4,4 +4,3 @@ version = "0.1.0" authors = ["Michael Gattozzi <mgattozzi@gmail.com>"] [dependencies] -wasm-bindgen = "0.2" diff --git a/tests/manifest/main.rs b/tests/manifest/main.rs index 995273a..a3037cf 100644 --- a/tests/manifest/main.rs +++ b/tests/manifest/main.rs @@ -69,3 +69,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()); +} From 2100c153d299886996d8bbf2d315de61cc5732ee Mon Sep 17 00:00:00 2001 From: Ashley Williams <ashley666ashley@gmail.com> Date: Mon, 11 Jun 2018 21:20:15 -0400 Subject: [PATCH 2/2] fix(style): appease cargo fmt --- src/manifest.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/manifest.rs b/src/manifest.rs index 0b901ad..872924e 100644 --- a/src/manifest.rs +++ b/src/manifest.rs @@ -178,4 +178,4 @@ pub fn check_crate_type(path: &str) -> Result<(), Error> { } else { Ok(()) } -} \ No newline at end of file +}