Skip to content

Commit cad30f4

Browse files
committed
check that crate type includes cdylib
1 parent 1c33879 commit cad30f4

File tree

5 files changed

+67
-1
lines changed

5 files changed

+67
-1
lines changed

src/command.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,19 @@ fn init(
211211
#[cfg(target_os = "windows")]
212212
info!(&log, "Copied readme from crate to {}\\pkg.", &crate_path);
213213

214+
info!(&log, "Checking the crate type from the manifest...");
215+
manifest::check_crate_type(&crate_path)?;
216+
#[cfg(not(target_os = "windows"))]
217+
info!(
218+
&log,
219+
"Checked crate type from the manifest at {}/Cargo.toml.", &crate_path
220+
);
221+
#[cfg(target_os = "windows")]
222+
info!(
223+
&log,
224+
"Checked crate type from the manifest at {}\\Cargo.toml.", &crate_path
225+
);
226+
214227
info!(&log, "Installing wasm-bindgen-cli...");
215228
bindgen::cargo_install_wasm_bindgen()?;
216229
info!(&log, "Installing wasm-bindgen-cli was successful.");

src/error.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ pub enum Error {
1515
SerdeToml(#[cause] toml::de::Error),
1616
#[fail(display = "{}. stderr:\n\n{}", message, stderr)]
1717
Cli { message: String, stderr: String },
18+
#[fail(display = "{}", message)]
19+
Config { message: String },
1820
}
1921

2022
impl Error {
@@ -25,6 +27,12 @@ impl Error {
2527
})
2628
}
2729

30+
pub fn config(message: &str) -> Result<(), Self> {
31+
Err(Error::Config {
32+
message: message.to_string(),
33+
})
34+
}
35+
2836
pub fn error_type(&self) -> String {
2937
match self {
3038
Error::Io(_) => "There was an I/O error. Details:\n\n",
@@ -34,6 +42,7 @@ impl Error {
3442
message: _,
3543
stderr: _,
3644
} => "There was an error while calling another CLI tool. Details:\n\n",
45+
Error::Config { message: _ } => "There was a configuration error. Details:\n\n",
3746
}.to_string()
3847
}
3948
}

src/manifest.rs

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ use PBAR;
1111
#[derive(Deserialize)]
1212
struct CargoManifest {
1313
package: CargoPackage,
14+
lib: Option<CargoLib>,
1415
}
1516

1617
#[derive(Deserialize)]
@@ -23,6 +24,11 @@ struct CargoPackage {
2324
repository: Option<String>,
2425
}
2526

27+
#[derive(Deserialize)]
28+
struct CargoLib {
29+
crate_type: Option<Vec<String>>,
30+
}
31+
2632
#[derive(Serialize)]
2733
struct NpmPackage {
2834
name: String,
@@ -49,7 +55,9 @@ fn read_cargo_toml(path: &str) -> Result<CargoManifest, Error> {
4955
let mut cargo_contents = String::new();
5056
cargo_file.read_to_string(&mut cargo_contents)?;
5157

52-
Ok(toml::from_str(&cargo_contents)?)
58+
Ok(toml::from_str(
59+
&cargo_contents.replace("crate-type", "crate_type"),
60+
)?)
5361
}
5462

5563
impl CargoManifest {
@@ -136,3 +144,21 @@ pub fn write_package_json(
136144
pub fn get_crate_name(path: &str) -> Result<String, Error> {
137145
Ok(read_cargo_toml(path)?.package.name)
138146
}
147+
148+
fn has_cdylib(path: &str) -> bool {
149+
match read_cargo_toml(path).unwrap().lib {
150+
Some(lib) => match lib.crate_type {
151+
Some(types) => types.iter().any(|s| s == "cdylib"),
152+
_ => false,
153+
},
154+
_ => false,
155+
}
156+
}
157+
158+
pub fn check_crate_type(path: &str) -> Result<(), Error> {
159+
if !has_cdylib(path) {
160+
Error::config("crate-type must include cdylib to compile to wasm32-unknown-unknown")
161+
} else {
162+
Ok(())
163+
}
164+
}

tests/fixtures/bad-cargo-toml/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,8 @@ name = "bad-cargo-toml"
33
version = "0.1.0"
44
authors = ["Michael Gattozzi <[email protected]>"]
55

6+
[lib]
7+
crate-type = ["foo"]
8+
69
[dependencies]
710
wasm-bindgen = "0.2"

tests/manifest/main.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,21 @@ fn it_gets_the_crate_name_provided_path() {
2525
);
2626
}
2727

28+
#[test]
29+
fn it_checks_has_cdylib_default_path() {
30+
assert!(manifest::check_crate_type(".").is_err());
31+
}
32+
33+
#[test]
34+
fn it_checks_has_cdylib_provided_path() {
35+
assert!(manifest::check_crate_type("tests/fixtures/js-hello-world").is_ok());
36+
}
37+
38+
#[test]
39+
fn it_checks_has_cdylib_wrong_crate_type() {
40+
assert!(manifest::check_crate_type("tests/fixtures/bad-cargo-toml").is_err());
41+
}
42+
2843
#[test]
2944
fn it_creates_a_package_json_default_path() {
3045
let path = ".".to_string();

0 commit comments

Comments
 (0)