check that crate type includes cdylib

master
Noah Lemen 7 years ago
parent 1c33879c7e
commit cad30f42ce
  1. 13
      src/command.rs
  2. 9
      src/error.rs
  3. 28
      src/manifest.rs
  4. 3
      tests/fixtures/bad-cargo-toml/Cargo.toml
  5. 15
      tests/manifest/main.rs

@ -211,6 +211,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)]
Config { message: String },
}
impl Error {
@ -25,6 +27,12 @@ impl Error {
})
}
pub fn config(message: &str) -> Result<(), Self> {
Err(Error::Config {
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,7 @@ impl Error {
message: _,
stderr: _,
} => "There was an error while calling another CLI tool. Details:\n\n",
Error::Config { message: _ } => "There was a 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,11 @@ struct CargoPackage {
repository: Option<String>,
}
#[derive(Deserialize)]
struct CargoLib {
crate_type: Option<Vec<String>>,
}
#[derive(Serialize)]
struct NpmPackage {
name: String,
@ -49,7 +55,9 @@ fn read_cargo_toml(path: &str) -> Result<CargoManifest, Error> {
let mut cargo_contents = String::new();
cargo_file.read_to_string(&mut cargo_contents)?;
Ok(toml::from_str(&cargo_contents)?)
Ok(toml::from_str(
&cargo_contents.replace("crate-type", "crate_type"),
)?)
}
impl CargoManifest {
@ -136,3 +144,21 @@ 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) -> bool {
match read_cargo_toml(path).unwrap().lib {
Some(lib) => match lib.crate_type {
Some(types) => types.iter().any(|s| s == "cdylib"),
_ => false,
},
_ => false,
}
}
pub fn check_crate_type(path: &str) -> Result<(), Error> {
if !has_cdylib(path) {
Error::config("crate-type must include cdylib to compile to wasm32-unknown-unknown")
} 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