@@ -8,33 +8,10 @@ use manifest;
8
8
use progressbar:: Step ;
9
9
use readme;
10
10
use slog:: Logger ;
11
- use std:: fs;
12
- use std:: path:: { Path , PathBuf } ;
11
+ use std:: path:: PathBuf ;
13
12
use std:: time:: Instant ;
14
13
use PBAR ;
15
14
16
- /// Construct our `pkg` directory in the crate.
17
- pub fn create_pkg_dir ( path : & Path , step : & Step ) -> Result < ( ) , Error > {
18
- let msg = format ! ( "{}Creating a pkg directory..." , emoji:: FOLDER ) ;
19
- PBAR . step ( step, & msg) ;
20
- let pkg_dir_path = path. join ( "pkg" ) ;
21
- fs:: create_dir_all ( pkg_dir_path) ?;
22
- Ok ( ( ) )
23
- }
24
-
25
- /// The `InitMode` determines which mode of initialization we are running, and
26
- /// what build and install steps we perform.
27
- pub enum InitMode {
28
- /// Perform all the build and install steps.
29
- Normal ,
30
- /// Don't build the crate as a `.wasm` but do install tools and create
31
- /// meta-data.
32
- Nobuild ,
33
- /// Don't install tools like `wasm-bindgen`, just use the global
34
- /// environment's existing versions to do builds.
35
- Noinstall ,
36
- }
37
-
38
15
/// Everything required to configure and run the `wasm-pack init` command.
39
16
pub struct Init {
40
17
crate_path : PathBuf ,
@@ -47,7 +24,8 @@ pub struct Init {
47
24
#[ derive( Debug , StructOpt ) ]
48
25
pub struct InitOptions {
49
26
/// The path to the Rust crate.
50
- pub path : Option < String > ,
27
+ #[ structopt( parse( from_os_str) ) ]
28
+ pub path : Option < PathBuf > ,
51
29
52
30
/// The npm scope to use in package.json, if any.
53
31
#[ structopt( long = "scope" , short = "s" ) ]
@@ -109,34 +87,18 @@ impl Init {
109
87
Ok ( ( ) )
110
88
}
111
89
112
- fn step_check_crate_config ( & mut self , step : & Step , log : & Logger ) -> Result < ( ) , Error > {
113
- info ! ( & log, "Checking crate configuration..." ) ;
114
- manifest:: check_crate_config ( & self . crate_path , step) ?;
115
- info ! ( & log, "Crate is correctly configured." ) ;
116
- Ok ( ( ) )
117
- }
118
-
119
- fn step_add_wasm_target ( & mut self , step : & Step , log : & Logger ) -> Result < ( ) , Error > {
120
- info ! ( & log, "Adding wasm-target..." ) ;
121
- build:: rustup_add_wasm_target ( step) ?;
122
- info ! ( & log, "Adding wasm-target was successful." ) ;
123
- Ok ( ( ) )
124
- }
125
-
126
- fn step_build_wasm ( & mut self , step : & Step , log : & Logger ) -> Result < ( ) , Error > {
127
- info ! ( & log, "Building wasm..." ) ;
128
- build:: cargo_build_wasm ( & self . crate_path , self . debug , step) ?;
129
-
130
- info ! (
131
- & log,
132
- "wasm built at {:#?}." ,
133
- & self
134
- . crate_path
135
- . join( "target" )
136
- . join( "wasm32-unknown-unknown" )
137
- . join( "release" )
138
- ) ;
139
- Ok ( ( ) )
90
+ fn set_process_steps ( ) -> Vec < ( & ' static str , InitStep ) > {
91
+ macro_rules! steps {
92
+ ( $( $name: ident) ,+) => {
93
+ {
94
+ let mut steps: Vec <( & ' static str , InitStep ) > = Vec :: new( ) ;
95
+ $( steps. push( ( stringify!( $name) , Init :: $name) ) ; ) *
96
+ steps
97
+ }
98
+ } ;
99
+ ( $( $name: ident, ) * ) => ( steps![ $( $name) ,* ] )
100
+ }
101
+ steps ! [ step_create_dir, step_create_json, step_copy_readme, ]
140
102
}
141
103
142
104
fn step_create_dir ( & mut self , step : & Step , log : & Logger ) -> Result < ( ) , Error > {
@@ -173,95 +135,4 @@ impl Init {
173
135
) ;
174
136
Ok ( ( ) )
175
137
}
176
-
177
- fn step_install_wasm_bindgen ( & mut self , step : & Step , log : & Logger ) -> Result < ( ) , Error > {
178
- info ! ( & log, "Installing wasm-bindgen-cli..." ) ;
179
- bindgen:: cargo_install_wasm_bindgen ( step) ?;
180
- info ! ( & log, "Installing wasm-bindgen-cli was successful." ) ;
181
-
182
- info ! ( & log, "Getting the crate name from the manifest..." ) ;
183
- self . crate_name = manifest:: get_crate_name ( & self . crate_path ) ?;
184
- info ! (
185
- & log,
186
- "Got crate name {:#?} from the manifest at {:#?}." ,
187
- & self . crate_name,
188
- & self . crate_path. join( "Cargo.toml" )
189
- ) ;
190
- Ok ( ( ) )
191
- }
192
-
193
- fn step_run_wasm_bindgen ( & mut self , step : & Step , log : & Logger ) -> Result < ( ) , Error > {
194
- info ! ( & log, "Building the wasm bindings..." ) ;
195
- bindgen:: wasm_bindgen_build (
196
- & self . crate_path ,
197
- & self . crate_name ,
198
- self . disable_dts ,
199
- & self . target ,
200
- self . debug ,
201
- step,
202
- ) ?;
203
- info ! (
204
- & log,
205
- "wasm bindings were built at {:#?}." ,
206
- & self . crate_path. join( "pkg" )
207
- ) ;
208
- Ok ( ( ) )
209
- }
210
- }
211
-
212
- #[ cfg( test) ]
213
- mod test {
214
- use super :: * ;
215
-
216
- #[ test]
217
- fn init_normal_build ( ) {
218
- let steps: Vec < & str > = Init :: get_process_steps ( InitMode :: Normal )
219
- . into_iter ( )
220
- . map ( |( n, _) | n)
221
- . collect ( ) ;
222
- assert_eq ! (
223
- steps,
224
- [
225
- "step_check_crate_config" ,
226
- "step_add_wasm_target" ,
227
- "step_build_wasm" ,
228
- "step_create_dir" ,
229
- "step_create_json" ,
230
- "step_copy_readme" ,
231
- "step_install_wasm_bindgen" ,
232
- "step_run_wasm_bindgen"
233
- ]
234
- ) ;
235
- }
236
-
237
- #[ test]
238
- fn init_skip_build ( ) {
239
- let steps: Vec < & str > = Init :: get_process_steps ( InitMode :: Nobuild )
240
- . into_iter ( )
241
- . map ( |( n, _) | n)
242
- . collect ( ) ;
243
- assert_eq ! (
244
- steps,
245
- [ "step_create_dir" , "step_create_json" , "step_copy_readme" ]
246
- ) ;
247
- }
248
-
249
- #[ test]
250
- fn init_skip_install ( ) {
251
- let steps: Vec < & str > = Init :: get_process_steps ( InitMode :: Noinstall )
252
- . into_iter ( )
253
- . map ( |( n, _) | n)
254
- . collect ( ) ;
255
- assert_eq ! (
256
- steps,
257
- [
258
- "step_check_crate_config" ,
259
- "step_build_wasm" ,
260
- "step_create_dir" ,
261
- "step_create_json" ,
262
- "step_copy_readme" ,
263
- "step_run_wasm_bindgen"
264
- ]
265
- ) ;
266
- }
267
138
}
0 commit comments