File tree 5 files changed +66
-0
lines changed
5 files changed +66
-0
lines changed Original file line number Diff line number Diff line change @@ -218,6 +218,19 @@ fn init(
218
218
#[ cfg( target_os = "windows" ) ]
219
219
info ! ( & log, "Copied readme from crate to {}\\ pkg." , & crate_path) ;
220
220
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
+
221
234
info ! ( & log, "Installing wasm-bindgen-cli..." ) ;
222
235
bindgen:: cargo_install_wasm_bindgen ( ) ?;
223
236
info ! ( & log, "Installing wasm-bindgen-cli was successful." ) ;
Original file line number Diff line number Diff line change @@ -15,6 +15,8 @@ pub enum Error {
15
15
SerdeToml ( #[ cause] toml:: de:: Error ) ,
16
16
#[ fail( display = "{}. stderr:\n \n {}" , message, stderr) ]
17
17
Cli { message : String , stderr : String } ,
18
+ #[ fail( display = "{}" , message) ]
19
+ CrateConfig { message : String } ,
18
20
}
19
21
20
22
impl Error {
@@ -25,6 +27,12 @@ impl Error {
25
27
} )
26
28
}
27
29
30
+ pub fn crate_config ( message : & str ) -> Result < ( ) , Self > {
31
+ Err ( Error :: CrateConfig {
32
+ message : message. to_string ( ) ,
33
+ } )
34
+ }
35
+
28
36
pub fn error_type ( & self ) -> String {
29
37
match self {
30
38
Error :: Io ( _) => "There was an I/O error. Details:\n \n " ,
@@ -34,6 +42,9 @@ impl Error {
34
42
message : _,
35
43
stderr : _,
36
44
} => "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
+ }
37
48
} . to_string ( )
38
49
}
39
50
}
Original file line number Diff line number Diff line change @@ -11,6 +11,7 @@ use PBAR;
11
11
#[ derive( Deserialize ) ]
12
12
struct CargoManifest {
13
13
package : CargoPackage ,
14
+ lib : Option < CargoLib > ,
14
15
}
15
16
16
17
#[ derive( Deserialize ) ]
@@ -23,6 +24,12 @@ struct CargoPackage {
23
24
repository : Option < String > ,
24
25
}
25
26
27
+ #[ derive( Deserialize ) ]
28
+ struct CargoLib {
29
+ #[ serde( rename = "crate-type" ) ]
30
+ crate_type : Option < Vec < String > > ,
31
+ }
32
+
26
33
#[ derive( Serialize ) ]
27
34
struct NpmPackage {
28
35
name : String ,
@@ -136,3 +143,20 @@ pub fn write_package_json(
136
143
pub fn get_crate_name ( path : & str ) -> Result < String , Error > {
137
144
Ok ( read_cargo_toml ( path) ?. package . name )
138
145
}
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]\n crate-type = [\" cdylib\" ]"
158
+ )
159
+ } else {
160
+ Ok ( ( ) )
161
+ }
162
+ }
Original file line number Diff line number Diff line change @@ -3,5 +3,8 @@ name = "bad-cargo-toml"
3
3
version = " 0.1.0"
4
4
authors = [
" Michael Gattozzi <[email protected] >" ]
5
5
6
+ [lib ]
7
+ crate-type = [" foo" ]
8
+
6
9
[dependencies ]
7
10
wasm-bindgen = " 0.2"
Original file line number Diff line number Diff line change @@ -25,6 +25,21 @@ fn it_gets_the_crate_name_provided_path() {
25
25
) ;
26
26
}
27
27
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
+
28
43
#[ test]
29
44
fn it_creates_a_package_json_default_path ( ) {
30
45
let path = "." . to_string ( ) ;
You can’t perform that action at this time.
0 commit comments