|
| 1 | +//! Initializing a crate for packing `.wasm`s. |
| 2 | +
|
| 3 | +use bindgen; |
| 4 | +use build; |
| 5 | +use command::utils::{set_crate_path, create_pkg_dir}; |
| 6 | +use emoji; |
| 7 | +use error::Error; |
| 8 | +use indicatif::HumanDuration; |
| 9 | +use manifest; |
| 10 | +use progressbar::Step; |
| 11 | +use readme; |
| 12 | +use slog::Logger; |
| 13 | +use std::time::Instant; |
| 14 | +use PBAR; |
| 15 | + |
| 16 | +/// Everything required to configure and run the `wasm-pack init` command. |
| 17 | +pub(crate) struct Build { |
| 18 | + pub crate_path: String, |
| 19 | + pub scope: Option<String>, |
| 20 | + pub disable_dts: bool, |
| 21 | + pub target: String, |
| 22 | + pub debug: bool, |
| 23 | + // build_config: Option<BuildConfig>, |
| 24 | + pub crate_name: String, |
| 25 | +} |
| 26 | + |
| 27 | +/// The `BuildMode` determines which mode of initialization we are running, and |
| 28 | +/// what build and install steps we perform. |
| 29 | +pub enum BuildMode { |
| 30 | + /// Perform all the build and install steps. |
| 31 | + Normal, |
| 32 | + /// Don't install tools like `wasm-bindgen`, just use the global |
| 33 | + /// environment's existing versions to do builds. |
| 34 | + Noinstall, |
| 35 | +} |
| 36 | + |
| 37 | +/// Everything required to configure and run the `wasm-pack build` command. |
| 38 | +#[derive(Debug,StructOpt)] |
| 39 | +pub struct BuildOptions { |
| 40 | + /// The path to the Rust crate. |
| 41 | + pub path: Option<String>, |
| 42 | + |
| 43 | + /// The npm scope to use in package.json, if any. |
| 44 | + #[structopt(long = "scope", short = "s")] |
| 45 | + pub scope: Option<String>, |
| 46 | + |
| 47 | + #[structopt(long = "mode", short = "m", default_value = "normal")] |
| 48 | + /// Sets steps to be run. [possible values: no-install, normal] |
| 49 | + pub mode: String, |
| 50 | + |
| 51 | + #[structopt(long = "no-typescript")] |
| 52 | + /// By default a *.d.ts file is generated for the generated JS file, but |
| 53 | + /// this flag will disable generating this TypeScript file. |
| 54 | + pub disable_dts: bool, |
| 55 | + |
| 56 | + #[structopt(long = "target", short = "t", default_value = "browser")] |
| 57 | + /// Sets the target environment. [possible values: browser, nodejs] |
| 58 | + pub target: String, |
| 59 | + |
| 60 | + #[structopt(long = "debug")] |
| 61 | + /// Build without --release. |
| 62 | + debug: bool, |
| 63 | + |
| 64 | + // build config from manifest |
| 65 | + // build_config: Option<BuildConfig>, |
| 66 | +} |
| 67 | + |
| 68 | +impl From<BuildOptions> for Build { |
| 69 | + fn from(build_opts: BuildOptions) -> Self { |
| 70 | + let crate_path = set_crate_path(build_opts.path); |
| 71 | + let crate_name = manifest::get_crate_name(&crate_path).unwrap(); |
| 72 | + // let build_config = manifest::xxx(&crate_path).xxx(); |
| 73 | + Build { |
| 74 | + crate_path, |
| 75 | + scope:build_opts.scope, |
| 76 | + disable_dts:build_opts.disable_dts, |
| 77 | + target:build_opts.target, |
| 78 | + debug:build_opts.debug, |
| 79 | + // build_config, |
| 80 | + crate_name, |
| 81 | + } |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +type BuildStep = fn(&mut Build, &Step, &Logger) -> Result<(), Error>; |
| 86 | + |
| 87 | +impl Build { |
| 88 | + /// Execute this `Init` command. |
| 89 | + pub fn run(&mut self, log: &Logger, mode: BuildMode) -> Result<(), Error> { |
| 90 | + let process_steps = Build::get_process_steps(mode); |
| 91 | + |
| 92 | + let mut step_counter = Step::new(process_steps.len()); |
| 93 | + |
| 94 | + let started = Instant::now(); |
| 95 | + |
| 96 | + for (_, process_step) in process_steps { |
| 97 | + process_step(self, &step_counter, log)?; |
| 98 | + step_counter.inc(); |
| 99 | + } |
| 100 | + |
| 101 | + let duration = HumanDuration(started.elapsed()); |
| 102 | + info!(&log, "Done in {}.", &duration); |
| 103 | + info!( |
| 104 | + &log, |
| 105 | + "Your WASM pkg is ready to publish at {}/pkg.", &self.crate_path |
| 106 | + ); |
| 107 | + |
| 108 | + PBAR.message(&format!("{} Done in {}", emoji::SPARKLE, &duration)); |
| 109 | + |
| 110 | + PBAR.message(&format!( |
| 111 | + "{} Your WASM pkg is ready to publish at {}/pkg.", |
| 112 | + emoji::PACKAGE, |
| 113 | + &self.crate_path |
| 114 | + )); |
| 115 | + Ok(()) |
| 116 | + } |
| 117 | + |
| 118 | + fn get_process_steps(mode: BuildMode) -> Vec<(&'static str, BuildStep)> { |
| 119 | + macro_rules! steps { |
| 120 | + ($($name:ident),+) => { |
| 121 | + { |
| 122 | + let mut steps: Vec<(&'static str, BuildStep)> = Vec::new(); |
| 123 | + $(steps.push((stringify!($name), Build::$name));)* |
| 124 | + steps |
| 125 | + } |
| 126 | + }; |
| 127 | + ($($name:ident,)*) => (steps![$($name),*]) |
| 128 | + } |
| 129 | + match mode { |
| 130 | + BuildMode::Normal => steps![ |
| 131 | + step_check_crate_config, |
| 132 | + step_add_wasm_target, |
| 133 | + step_build_wasm, |
| 134 | + step_create_dir, |
| 135 | + step_create_json, |
| 136 | + step_copy_readme, |
| 137 | + step_install_wasm_bindgen, |
| 138 | + step_run_wasm_bindgen, |
| 139 | + ], |
| 140 | + BuildMode::Noinstall => steps![ |
| 141 | + step_check_crate_config, |
| 142 | + step_build_wasm, |
| 143 | + step_create_dir, |
| 144 | + step_create_json, |
| 145 | + step_copy_readme, |
| 146 | + step_run_wasm_bindgen |
| 147 | + ], |
| 148 | + } |
| 149 | + } |
| 150 | + |
| 151 | + |
| 152 | + fn step_check_crate_config(&mut self, step: &Step, log: &Logger) -> Result<(), Error> { |
| 153 | + info!(&log, "Checking crate configuration..."); |
| 154 | + manifest::check_crate_config(&self.crate_path, step)?; |
| 155 | + info!(&log, "Crate is correctly configured."); |
| 156 | + Ok(()) |
| 157 | + } |
| 158 | + |
| 159 | + fn step_add_wasm_target(&mut self, step: &Step, log: &Logger) -> Result<(), Error> { |
| 160 | + info!(&log, "Adding wasm-target..."); |
| 161 | + build::rustup_add_wasm_target(step)?; |
| 162 | + info!(&log, "Adding wasm-target was successful."); |
| 163 | + Ok(()) |
| 164 | + } |
| 165 | + |
| 166 | + fn step_build_wasm(&mut self, step: &Step, log: &Logger) -> Result<(), Error> { |
| 167 | + info!(&log, "Building wasm..."); |
| 168 | + build::cargo_build_wasm(&self.crate_path, self.debug, step)?; |
| 169 | + |
| 170 | + #[cfg(not(target_os = "windows"))] |
| 171 | + info!( |
| 172 | + &log, |
| 173 | + "wasm built at {}/target/wasm32-unknown-unknown/release.", &self.crate_path |
| 174 | + ); |
| 175 | + #[cfg(target_os = "windows")] |
| 176 | + info!( |
| 177 | + &log, |
| 178 | + "wasm built at {}\\target\\wasm32-unknown-unknown\\release.", &self.crate_path |
| 179 | + ); |
| 180 | + Ok(()) |
| 181 | + } |
| 182 | + |
| 183 | + fn step_create_dir(&mut self, step: &Step, log: &Logger) -> Result<(), Error> { |
| 184 | + info!(&log, "Creating a pkg directory..."); |
| 185 | + create_pkg_dir(&self.crate_path, step)?; |
| 186 | + info!(&log, "Created a pkg directory at {}.", &self.crate_path); |
| 187 | + Ok(()) |
| 188 | + } |
| 189 | + |
| 190 | + fn step_create_json(&mut self, step: &Step, log: &Logger) -> Result<(), Error> { |
| 191 | + info!(&log, "Writing a package.json..."); |
| 192 | + manifest::write_package_json( |
| 193 | + &self.crate_path, |
| 194 | + &self.scope, |
| 195 | + self.disable_dts, |
| 196 | + &self.target, |
| 197 | + step, |
| 198 | + )?; |
| 199 | + #[cfg(not(target_os = "windows"))] |
| 200 | + info!( |
| 201 | + &log, |
| 202 | + "Wrote a package.json at {}/pkg/package.json.", &self.crate_path |
| 203 | + ); |
| 204 | + #[cfg(target_os = "windows")] |
| 205 | + info!( |
| 206 | + &log, |
| 207 | + "Wrote a package.json at {}\\pkg\\package.json.", &self.crate_path |
| 208 | + ); |
| 209 | + Ok(()) |
| 210 | + } |
| 211 | + |
| 212 | + fn step_copy_readme(&mut self, step: &Step, log: &Logger) -> Result<(), Error> { |
| 213 | + info!(&log, "Copying readme from crate..."); |
| 214 | + readme::copy_from_crate(&self.crate_path, step)?; |
| 215 | + #[cfg(not(target_os = "windows"))] |
| 216 | + info!( |
| 217 | + &log, |
| 218 | + "Copied readme from crate to {}/pkg.", &self.crate_path |
| 219 | + ); |
| 220 | + #[cfg(target_os = "windows")] |
| 221 | + info!( |
| 222 | + &log, |
| 223 | + "Copied readme from crate to {}\\pkg.", &self.crate_path |
| 224 | + ); |
| 225 | + Ok(()) |
| 226 | + } |
| 227 | + |
| 228 | + fn step_install_wasm_bindgen(&mut self, step: &Step, log: &Logger) -> Result<(), Error> { |
| 229 | + info!(&log, "Installing wasm-bindgen-cli..."); |
| 230 | + bindgen::cargo_install_wasm_bindgen(step)?; |
| 231 | + info!(&log, "Installing wasm-bindgen-cli was successful."); |
| 232 | + |
| 233 | + info!(&log, "Getting the crate name from the manifest..."); |
| 234 | + self.crate_name = manifest::get_crate_name(&self.crate_path)?; |
| 235 | + #[cfg(not(target_os = "windows"))] |
| 236 | + info!( |
| 237 | + &log, |
| 238 | + "Got crate name {} from the manifest at {}/Cargo.toml.", |
| 239 | + &self.crate_name, |
| 240 | + &self.crate_path |
| 241 | + ); |
| 242 | + #[cfg(target_os = "windows")] |
| 243 | + info!( |
| 244 | + &log, |
| 245 | + "Got crate name {} from the manifest at {}\\Cargo.toml.", |
| 246 | + &self.crate_name, |
| 247 | + &self.crate_path |
| 248 | + ); |
| 249 | + Ok(()) |
| 250 | + } |
| 251 | + |
| 252 | + fn step_run_wasm_bindgen(&mut self, step: &Step, log: &Logger) -> Result<(), Error> { |
| 253 | + info!(&log, "Building the wasm bindings..."); |
| 254 | + bindgen::wasm_bindgen_build( |
| 255 | + &self.crate_path, |
| 256 | + &self.crate_name, |
| 257 | + self.disable_dts, |
| 258 | + &self.target, |
| 259 | + self.debug, |
| 260 | + step, |
| 261 | + )?; |
| 262 | + #[cfg(not(target_os = "windows"))] |
| 263 | + info!( |
| 264 | + &log, |
| 265 | + "wasm bindings were built at {}/pkg.", &self.crate_path |
| 266 | + ); |
| 267 | + #[cfg(target_os = "windows")] |
| 268 | + info!( |
| 269 | + &log, |
| 270 | + "wasm bindings were built at {}\\pkg.", &self.crate_path |
| 271 | + ); |
| 272 | + Ok(()) |
| 273 | + } |
| 274 | +} |
0 commit comments