1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
use crate::{
    anyhow::{ensure, Context, Result},
    camino, clap, default_build_command, metadata,
};
use lazy_static::lazy_static;
use std::{fs, path::PathBuf, process};
use wasm_bindgen_cli_support::Bindgen;
#[non_exhaustive]
#[derive(Debug, clap::Parser)]
pub struct Dist {
    
    #[clap(short, long)]
    pub quiet: bool,
    
    #[clap(short, long)]
    pub jobs: Option<String>,
    
    #[clap(long)]
    pub profile: Option<String>,
    
    #[clap(long)]
    pub release: bool,
    
    #[clap(long)]
    pub features: Vec<String>,
    
    #[clap(long)]
    pub all_features: bool,
    
    #[clap(long)]
    pub no_default_features: bool,
    
    #[clap(short, long)]
    pub verbose: bool,
    
    #[clap(long)]
    pub color: Option<String>,
    
    #[clap(long)]
    pub frozen: bool,
    
    #[clap(long)]
    pub locked: bool,
    
    #[clap(long)]
    pub offline: bool,
    
    #[clap(long)]
    pub ignore_rust_version: bool,
    
    #[clap(long)]
    pub example: Option<String>,
    
    #[clap(skip = default_build_command())]
    pub build_command: process::Command,
    
    #[clap(skip)]
    pub dist_dir_path: Option<PathBuf>,
    
    #[clap(skip)]
    pub static_dir_path: Option<PathBuf>,
    
    #[clap(skip)]
    pub app_name: Option<String>,
    
    #[clap(skip = true)]
    pub run_in_workspace: bool,
}
impl Dist {
    
    
    
    pub fn build_command(mut self, command: process::Command) -> Self {
        self.build_command = command;
        self
    }
    
    
    
    
    pub fn dist_dir_path(mut self, path: impl Into<PathBuf>) -> Self {
        self.dist_dir_path = Some(path.into());
        self
    }
    
    pub fn static_dir_path(mut self, path: impl Into<PathBuf>) -> Self {
        self.static_dir_path = Some(path.into());
        self
    }
    
    
    
    pub fn app_name(mut self, app_name: impl Into<String>) -> Self {
        self.app_name = Some(app_name.into());
        self
    }
    
    pub fn run_in_workspace(mut self, res: bool) -> Self {
        self.run_in_workspace = res;
        self
    }
    
    pub fn example(mut self, example: impl Into<String>) -> Self {
        self.example = Some(example.into());
        self
    }
    
    
    
    
    
    
    
    
    pub fn run(self, package_name: &str) -> Result<DistResult> {
        log::trace!("Getting package's metadata");
        let metadata = metadata();
        let dist_dir_path = self
            .dist_dir_path
            .unwrap_or_else(|| default_dist_dir(self.release).as_std_path().to_path_buf());
        log::trace!("Initializing dist process");
        let mut build_process = self.build_command;
        if self.run_in_workspace {
            build_process.current_dir(&metadata.workspace_root);
        }
        if self.quiet {
            build_process.arg("--quiet");
        }
        if let Some(number) = self.jobs {
            build_process.args(["--jobs", &number]);
        }
        if let Some(profile) = self.profile {
            build_process.args(["--profile", &profile]);
        }
        if self.release {
            build_process.arg("--release");
        }
        for feature in &self.features {
            build_process.args(["--features", feature]);
        }
        if self.all_features {
            build_process.arg("--all-features");
        }
        if self.no_default_features {
            build_process.arg("--no-default-features");
        }
        if self.verbose {
            build_process.arg("--verbose");
        }
        if let Some(color) = self.color {
            build_process.args(["--color", &color]);
        }
        if self.frozen {
            build_process.arg("--frozen");
        }
        if self.locked {
            build_process.arg("--locked");
        }
        if self.offline {
            build_process.arg("--offline");
        }
        if self.ignore_rust_version {
            build_process.arg("--ignore-rust-version");
        }
        build_process.args(["--package", package_name]);
        if let Some(example) = &self.example {
            build_process.args(["--example", example]);
        }
        let build_dir = metadata
            .target_directory
            .join("wasm32-unknown-unknown")
            .join(if self.release { "release" } else { "debug" });
        let input_path = if let Some(example) = &self.example {
            build_dir
                .join("examples")
                .join(&example.replace("-", "_"))
                .with_extension("wasm")
        } else {
            build_dir
                .join(&package_name.replace("-", "_"))
                .with_extension("wasm")
        };
        if input_path.exists() {
            log::trace!("Removing existing target directory");
            fs::remove_file(&input_path).context("cannot remove existing target")?;
        }
        log::trace!("Spawning build process");
        ensure!(
            build_process
                .status()
                .context("could not start cargo")?
                .success(),
            "cargo command failed"
        );
        let app_name = self.app_name.unwrap_or_else(|| "app".to_string());
        log::trace!("Generating wasm output");
        let mut output = Bindgen::new()
            .input_path(input_path)
            .out_name(&app_name)
            .web(true)
            .expect("web have panic")
            .debug(!self.release)
            .generate_output()
            .context("could not generate WASM bindgen file")?;
        let wasm_js = output.js().to_owned();
        let wasm_bin = output.wasm_mut().emit_wasm();
        let wasm_js_path = dist_dir_path.join(&app_name).with_extension("js");
        let wasm_bin_path = dist_dir_path.join(&app_name).with_extension("wasm");
        if dist_dir_path.exists() {
            log::trace!("Removing already existing dist directory");
            fs::remove_dir_all(&dist_dir_path)?;
        }
        log::trace!("Creating new dist directory");
        fs::create_dir_all(&dist_dir_path).context("cannot create build directory")?;
        log::trace!("Writing files into dist directory");
        fs::write(&wasm_js_path, wasm_js).with_context(|| "cannot write js file")?;
        fs::write(&wasm_bin_path, wasm_bin).with_context(|| "cannot write WASM file")?;
        let mut copy_options = fs_extra::dir::CopyOptions::new();
        copy_options.overwrite = true;
        copy_options.content_only = true;
        if let Some(static_dir) = self.static_dir_path {
            log::trace!("Copying static directory into dist directory");
            fs_extra::dir::copy(static_dir, &dist_dir_path, ©_options)
                .context("cannot copy static directory")?;
        }
        log::info!("Successfully built in {}", dist_dir_path.display());
        Ok(DistResult {
            dist_dir: dist_dir_path,
            js: wasm_js_path,
            wasm: wasm_bin_path,
        })
    }
}
pub struct DistResult {
    
    pub dist_dir: PathBuf,
    
    pub js: PathBuf,
    
    pub wasm: PathBuf,
}
pub fn default_dist_dir(release: bool) -> &'static camino::Utf8Path {
    lazy_static! {
        static ref DEFAULT_RELEASE_PATH: camino::Utf8PathBuf =
            metadata().target_directory.join("release").join("dist");
        static ref DEFAULT_DEBUG_PATH: camino::Utf8PathBuf =
            metadata().target_directory.join("debug").join("dist");
    }
    if release {
        &DEFAULT_RELEASE_PATH
    } else {
        &DEFAULT_DEBUG_PATH
    }
}