-
Notifications
You must be signed in to change notification settings - Fork 8
/
dist.rs
422 lines (373 loc) · 13 KB
/
dist.rs
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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
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;
/// A helper to generate the distributed package.
///
/// # Usage
///
/// ```rust,no_run
/// use std::process;
/// use xtask_wasm::{anyhow::Result, clap};
///
/// #[derive(clap::Parser)]
/// enum Opt {
/// Dist(xtask_wasm::Dist),
/// }
///
/// fn main() -> Result<()> {
/// let opt: Opt = clap::Parser::parse();
///
/// match opt {
/// Opt::Dist(dist) => {
/// log::info!("Generating package...");
///
/// dist
/// .static_dir_path("my-project/static")
/// .app_name("my-project")
/// .run_in_workspace(true)
/// .run("my-project")?;
/// }
/// }
///
/// Ok(())
/// }
/// ```
///
/// In this example, we added a `dist` subcommand to build and package the
/// `my-project` crate. It will run the [`default_build_command`](crate::default_build_command)
/// at the workspace root, copy the content of the `project/static` directory,
/// generate JS bindings and output two files: `project.js` and `project.wasm`
/// into the dist directory.
#[non_exhaustive]
#[derive(Debug, clap::Parser)]
#[clap(
about = "Generate the distributed package.",
long_about = "Generate the distributed package.\n\
It will build and package the project for WASM."
)]
pub struct Dist {
/// No output printed to stdout.
#[clap(short, long)]
pub quiet: bool,
/// Number of parallel jobs, defaults to # of CPUs.
#[clap(short, long)]
pub jobs: Option<String>,
/// Build artifacts with the specified profile.
#[clap(long)]
pub profile: Option<String>,
/// Build artifacts in release mode, with optimizations.
#[clap(long)]
pub release: bool,
/// Space or comma separated list of features to activate.
#[clap(long)]
pub features: Vec<String>,
/// Activate all available features.
#[clap(long)]
pub all_features: bool,
/// Do not activate the `default` features.
#[clap(long)]
pub no_default_features: bool,
/// Use verbose output
#[clap(short, long)]
pub verbose: bool,
/// Coloring: auto, always, never.
#[clap(long)]
pub color: Option<String>,
/// Require Cargo.lock and cache are up to date.
#[clap(long)]
pub frozen: bool,
/// Require Cargo.lock is up to date.
#[clap(long)]
pub locked: bool,
/// Run without accessing the network.
#[clap(long)]
pub offline: bool,
/// Ignore `rust-version` specification in packages.
#[clap(long)]
pub ignore_rust_version: bool,
/// Name of the example target to run.
#[clap(long)]
pub example: Option<String>,
/// Command passed to the build process.
#[clap(skip = default_build_command())]
pub build_command: process::Command,
/// Directory of all generated artifacts.
#[clap(skip)]
pub dist_dir_path: Option<PathBuf>,
/// Directory of all static artifacts.
#[clap(skip)]
pub static_dir_path: Option<PathBuf>,
/// Set the resulting app name, default to `app`.
#[clap(skip)]
pub app_name: Option<String>,
/// Set the command's current directory as the workspace root.
#[clap(skip = true)]
pub run_in_workspace: bool,
/// Output style for SASS/SCSS
#[cfg(feature = "sass")]
#[clap(skip)]
pub sass_options: sass_rs::Options,
}
impl Dist {
/// Set the command used by the build process.
///
/// The default command is the result of the [`default_build_command`].
pub fn build_command(mut self, command: process::Command) -> Self {
self.build_command = command;
self
}
/// Set the directory for the generated artifacts.
///
/// The default for debug build is `target/debug/dist` and
/// `target/release/dist` for the release build.
pub fn dist_dir_path(mut self, path: impl Into<PathBuf>) -> Self {
self.dist_dir_path = Some(path.into());
self
}
/// Set the directory for the static artifacts (like `index.html`).
pub fn static_dir_path(mut self, path: impl Into<PathBuf>) -> Self {
self.static_dir_path = Some(path.into());
self
}
/// Set the resulting package name.
///
/// The default is `app`.
pub fn app_name(mut self, app_name: impl Into<String>) -> Self {
self.app_name = Some(app_name.into());
self
}
/// Set the dist process current directory as the workspace root.
pub fn run_in_workspace(mut self, res: bool) -> Self {
self.run_in_workspace = res;
self
}
#[cfg(feature = "sass")]
/// Set the output style for SCSS/SASS
pub fn sass_options(mut self, output_style: sass_rs::Options) -> Self {
self.sass_options = output_style;
self
}
/// Set the example to build.
pub fn example(mut self, example: impl Into<String>) -> Self {
self.example = Some(example.into());
self
}
/// Build the given package for Wasm.
///
/// This will generate JS bindings via [`wasm-bindgen`](https://docs.rs/wasm-bindgen/latest/wasm_bindgen/)
/// and copy files from a given static directory if any to finally return
/// the path of the generated artifacts.
///
/// Wasm optimizations can be achieved using [`crate::WasmOpt`] if the
/// feature `wasm-opt` is enabled.
pub fn run(self, package_name: &str) -> Result<PathBuf> {
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_command = self.build_command;
if self.run_in_workspace {
build_command.current_dir(&metadata.workspace_root);
}
if self.quiet {
build_command.arg("--quiet");
}
if let Some(number) = self.jobs {
build_command.args(["--jobs", &number]);
}
if let Some(profile) = self.profile {
build_command.args(["--profile", &profile]);
}
if self.release {
build_command.arg("--release");
}
for feature in &self.features {
build_command.args(["--features", feature]);
}
if self.all_features {
build_command.arg("--all-features");
}
if self.no_default_features {
build_command.arg("--no-default-features");
}
if self.verbose {
build_command.arg("--verbose");
}
if let Some(color) = self.color {
build_command.args(["--color", &color]);
}
if self.frozen {
build_command.arg("--frozen");
}
if self.locked {
build_command.arg("--locked");
}
if self.offline {
build_command.arg("--offline");
}
if self.ignore_rust_version {
build_command.arg("--ignore-rust-version");
}
build_command.args(["--package", package_name]);
if let Some(example) = &self.example {
build_command.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_command
.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")?;
if dist_dir_path.exists() {
log::trace!("Removing already existing dist directory");
fs::remove_dir_all(&dist_dir_path)?;
}
log::trace!("Writing outputs to dist directory");
output.emit(&dist_dir_path)?;
if let Some(static_dir) = self.static_dir_path {
#[cfg(feature = "sass")]
{
log::trace!("Generating CSS files from SASS/SCSS");
sass(&static_dir, &dist_dir_path, &self.sass_options)?;
}
#[cfg(not(feature = "sass"))]
{
let mut copy_options = fs_extra::dir::CopyOptions::new();
copy_options.overwrite = true;
copy_options.content_only = true;
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(dist_dir_path)
}
}
impl Default for Dist {
fn default() -> Dist {
Dist {
quiet: Default::default(),
jobs: Default::default(),
profile: Default::default(),
release: Default::default(),
features: Default::default(),
all_features: Default::default(),
no_default_features: Default::default(),
verbose: Default::default(),
color: Default::default(),
frozen: Default::default(),
locked: Default::default(),
offline: Default::default(),
ignore_rust_version: Default::default(),
example: Default::default(),
build_command: default_build_command(),
dist_dir_path: Default::default(),
static_dir_path: Default::default(),
app_name: Default::default(),
run_in_workspace: Default::default(),
#[cfg(feature = "sass")]
sass_options: Default::default(),
}
}
}
#[cfg(feature = "sass")]
fn sass(
static_dir: &std::path::Path,
dist_dir: &std::path::Path,
options: &sass_rs::Options,
) -> Result<()> {
fn is_sass(path: &std::path::Path) -> bool {
matches!(
path.extension()
.and_then(|x| x.to_str().map(|x| x.to_lowercase()))
.as_deref(),
Some("sass") | Some("scss")
)
}
fn should_ignore(path: &std::path::Path) -> bool {
path.file_name()
.expect("WalkDir does not yield paths ending with `..` or `.`")
.to_str()
.map(|x| x.starts_with('_'))
.unwrap_or(false)
}
log::trace!("Generating dist artifacts");
let walker = walkdir::WalkDir::new(static_dir);
for entry in walker {
let entry = entry
.with_context(|| format!("cannot walk into directory `{}`", &static_dir.display()))?;
let source = entry.path();
let dest = dist_dir.join(source.strip_prefix(static_dir).unwrap());
let _ = fs::create_dir_all(dest.parent().unwrap());
if !source.is_file() {
continue;
} else if is_sass(source) {
if !should_ignore(source) {
let dest = dest.with_extension("css");
let css = sass_rs::compile_file(source, options.clone())
.expect("could not convert SASS/ file");
fs::write(&dest, css)
.with_context(|| format!("could not write CSS to file `{}`", dest.display()))?;
}
} else {
fs::copy(source, &dest).with_context(|| {
format!("cannot move `{}` to `{}`", source.display(), dest.display())
})?;
}
}
Ok(())
}
/// Get the default dist directory.
///
/// The default for debug build is `target/debug/dist` and `target/release/dist`
/// for the release build.
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
}
}