Merge pull request #150 from kedromelon/cdylib-check

check that crate type includes cdylib
master
ashley williams 7 years ago committed by GitHub
commit 11908d700f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 13
      src/command.rs
  2. 11
      src/error.rs
  3. 24
      src/manifest.rs
  4. 3
      tests/fixtures/bad-cargo-toml/Cargo.toml
  5. 15
      tests/manifest/main.rs

@ -218,6 +218,19 @@ fn init(
#[cfg(target_os = "windows")]
info!(&log, "Copied readme from crate to {}\\pkg.", &crate_path);
info!(&log, "Checking the crate type from the manifest...");
manifest::check_crate_type(&crate_path)?;
#[cfg(not(target_os = "windows"))]
info!(
&log,
"Checked crate type from the manifest at {}/Cargo.toml.", &crate_path
);
#[cfg(target_os = "windows")]
info!(
&log,
"Checked crate type from the manifest at {}\\Cargo.toml.", &crate_path
);
info!(&log, "Installing wasm-bindgen-cli...");
bindgen::cargo_install_wasm_bindgen()?;
info!(&log, "Installing wasm-bindgen-cli was successful.");

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

@ -11,6 +11,7 @@ use PBAR;
#[derive(Deserialize)]
struct CargoManifest {
package: CargoPackage,
lib: Option<CargoLib>,
}
#[derive(Deserialize)]
@ -23,6 +24,12 @@ struct CargoPackage {
repository: Option<String>,
}
#[derive(Deserialize)]
struct CargoLib {
#[serde(rename = "crate-type")]
crate_type: Option<Vec<String>>,
}
#[derive(Serialize)]
struct NpmPackage {
name: String,
@ -136,3 +143,20 @@ pub fn write_package_json(
pub fn get_crate_name(path: &str) -> Result<String, Error> {
Ok(read_cargo_toml(path)?.package.name)
}
fn has_cdylib(path: &str) -> Result<bool, Error> {
Ok(read_cargo_toml(path)?.lib.map_or(false, |lib| {
lib.crate_type
.map_or(false, |types| types.iter().any(|s| s == "cdylib"))
}))
}
pub fn check_crate_type(path: &str) -> Result<(), Error> {
if !has_cdylib(path)? {
Error::crate_config(
"crate-type must include cdylib to compile to wasm32-unknown-unknown. Add the following to your Cargo.toml file:\n\n[lib]\ncrate-type = [\"cdylib\"]"
)
} else {
Ok(())
}
}

@ -3,5 +3,8 @@ name = "bad-cargo-toml"
version = "0.1.0"
authors = ["Michael Gattozzi <mgattozzi@gmail.com>"]
[lib]
crate-type = ["foo"]
[dependencies]
wasm-bindgen = "0.2"

@ -25,6 +25,21 @@ fn it_gets_the_crate_name_provided_path() {
);
}
#[test]
fn it_checks_has_cdylib_default_path() {
assert!(manifest::check_crate_type(".").is_err());
}
#[test]
fn it_checks_has_cdylib_provided_path() {
assert!(manifest::check_crate_type("tests/fixtures/js-hello-world").is_ok());
}
#[test]
fn it_checks_has_cdylib_wrong_crate_type() {
assert!(manifest::check_crate_type("tests/fixtures/bad-cargo-toml").is_err());
}
#[test]
fn it_creates_a_package_json_default_path() {
let path = ".".to_string();

Loading…
Cancel
Save