Skip to content

Commit 11908d7

Browse files
Merge pull request #150 from kedromelon/cdylib-check
check that crate type includes cdylib
2 parents d9fdd63 + 2cb2921 commit 11908d7

File tree

5 files changed

+66
-0
lines changed

5 files changed

+66
-0
lines changed

src/command.rs

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

221+
info!(&log, "Checking the crate type from the manifest...");
222+
manifest::check_crate_type(&crate_path)?;
223+
#[cfg(not(target_os = "windows"))]
224+
info!(
225+
&log,
226+
"Checked crate type from the manifest at {}/Cargo.toml.", &crate_path
227+
);
228+
#[cfg(target_os = "windows")]
229+
info!(
230+
&log,
231+
"Checked crate type from the manifest at {}\\Cargo.toml.", &crate_path
232+
);
233+
221234
info!(&log, "Installing wasm-bindgen-cli...");
222235
bindgen::cargo_install_wasm_bindgen()?;
223236
info!(&log, "Installing wasm-bindgen-cli was successful.");

src/error.rs

Lines changed: 11 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+
CrateConfig { message: String },
1820
}
1921

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

30+
pub fn crate_config(message: &str) -> Result<(), Self> {
31+
Err(Error::CrateConfig {
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,9 @@ impl Error {
3442
message: _,
3543
stderr: _,
3644
} => "There was an error while calling another CLI tool. Details:\n\n",
45+
Error::CrateConfig { message: _ } => {
46+
"There was a crate configuration error. Details:\n\n"
47+
}
3748
}.to_string()
3849
}
3950
}

src/manifest.rs

Lines changed: 24 additions & 0 deletions
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,12 @@ struct CargoPackage {
2324
repository: Option<String>,
2425
}
2526

27+
#[derive(Deserialize)]
28+
struct CargoLib {
29+
#[serde(rename = "crate-type")]
30+
crate_type: Option<Vec<String>>,
31+
}
32+
2633
#[derive(Serialize)]
2734
struct NpmPackage {
2835
name: String,
@@ -136,3 +143,20 @@ pub fn write_package_json(
136143
pub fn get_crate_name(path: &str) -> Result<String, Error> {
137144
Ok(read_cargo_toml(path)?.package.name)
138145
}
146+
147+
fn has_cdylib(path: &str) -> Result<bool, Error> {
148+
Ok(read_cargo_toml(path)?.lib.map_or(false, |lib| {
149+
lib.crate_type
150+
.map_or(false, |types| types.iter().any(|s| s == "cdylib"))
151+
}))
152+
}
153+
154+
pub fn check_crate_type(path: &str) -> Result<(), Error> {
155+
if !has_cdylib(path)? {
156+
Error::crate_config(
157+
"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\"]"
158+
)
159+
} else {
160+
Ok(())
161+
}
162+
}

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)