-
-
Notifications
You must be signed in to change notification settings - Fork 966
/
Copy pathwasm_bindgen.rs
454 lines (388 loc) · 13.4 KB
/
wasm_bindgen.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
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
use anyhow::{anyhow, Context};
use flate2::read::GzDecoder;
use std::{
path::{Path, PathBuf},
process::Stdio,
};
use tar::Archive;
use tempfile::TempDir;
use tokio::{fs, process::Command};
pub(crate) struct WasmBindgen {
version: String,
input_path: PathBuf,
out_dir: PathBuf,
out_name: String,
target: String,
debug: bool,
keep_debug: bool,
demangle: bool,
remove_name_section: bool,
remove_producers_section: bool,
}
impl WasmBindgen {
pub async fn run(&self) -> anyhow::Result<()> {
let binary = Self::final_binary(&self.version).await?;
let mut args = Vec::new();
// Target
args.push("--target");
args.push(&self.target);
// Options
if self.debug {
args.push("--debug");
}
if !self.demangle {
args.push("--no-demangle");
}
if self.keep_debug {
args.push("--keep-debug");
}
if self.remove_name_section {
args.push("--remove-name-section");
}
if self.remove_producers_section {
args.push("--remove-producers-section");
}
// Out name
args.push("--out-name");
args.push(&self.out_name);
// Out dir
let out_dir = self
.out_dir
.to_str()
.expect("input_path should be valid utf8");
args.push("--out-dir");
args.push(out_dir);
// Input path
let input_path = self
.input_path
.to_str()
.expect("input_path should be valid utf8");
args.push(input_path);
// Run bindgen
Command::new(binary)
.args(args)
.stdout(Stdio::piped())
.stderr(Stdio::piped())
.output()
.await?;
Ok(())
}
/// Verify that the required wasm-bindgen version is installed.
pub async fn verify_install(version: &str) -> anyhow::Result<bool> {
let binary_name = Self::installed_bin_name(version);
let path = Self::install_dir().await?.join(binary_name);
Ok(path.exists())
}
/// Install the specified wasm-bingen version.
///
/// This will overwrite any existing wasm-bindgen binaries of the same version.
///
/// This will attempt to install wasm-bindgen from:
/// 1. Direct GitHub release download.
/// 2. `cargo binstall` if installed.
/// 3. Compile from source with `cargo install`.
pub async fn install(version: &str) -> anyhow::Result<()> {
tracing::info!("Installing wasm-bindgen-cli@{version}...");
// Attempt installation from GitHub
if let Err(e) = Self::install_github(version).await {
tracing::error!("Failed to install wasm-bindgen-cli@{version}: {e}");
} else {
tracing::info!("wasm-bindgen-cli@{version} was successfully installed from GitHub.");
return Ok(());
}
// Attempt installation from binstall.
if let Err(e) = Self::install_binstall(version).await {
tracing::error!("Failed to install wasm-bindgen-cli@{version}: {e}");
tracing::info!("Failed to install prebuilt binary for wasm-bindgen-cli@{version}. Compiling from source instead. This may take a while.");
} else {
tracing::info!(
"wasm-bindgen-cli@{version} was successfully installed from cargo-binstall."
);
return Ok(());
}
// Attempt installation from cargo.
Self::install_cargo(version)
.await
.context("failed to install wasm-bindgen-cli from cargo")?;
tracing::info!("wasm-bindgen-cli@{version} was successfully installed from source.");
Ok(())
}
/// Try installing wasm-bindgen-cli from GitHub.
async fn install_github(version: &str) -> anyhow::Result<()> {
tracing::debug!("Attempting to install wasm-bindgen-cli@{version} from GitHub");
let url = git_install_url(version)
.ok_or_else(|| anyhow!("no available GitHub binary for wasm-bindgen-cli@{version}"))?;
// Get the final binary location.
let final_binary = Self::final_binary(version).await?;
// Download then extract wasm-bindgen-cli.
let bytes = reqwest::get(url).await?.bytes().await?;
// Unpack the first tar entry to the final binary location
Archive::new(GzDecoder::new(bytes.as_ref()))
.entries()?
.find(|entry| {
entry
.as_ref()
.map(|e| {
e.path_bytes()
.ends_with(Self::downloaded_bin_name().as_bytes())
})
.unwrap_or(false)
})
.context("Failed to find entry")??
.unpack(&final_binary)
.context("failed to unpack wasm-bindgen-cli binary")?;
Ok(())
}
/// Try installing wasm-bindgen-cli through cargo-binstall.
async fn install_binstall(version: &str) -> anyhow::Result<()> {
tracing::debug!("Attempting to install wasm-bindgen-cli@{version} from cargo-binstall");
let package = Self::cargo_bin_name(version);
let tempdir = TempDir::new()?;
// Run install command
Command::new("cargo")
.args([
"binstall",
&package,
"--no-confirm",
"--force",
"--no-track",
"--install-path",
])
.arg(tempdir.path())
.stdout(Stdio::piped())
.stderr(Stdio::piped())
.output()
.await?;
fs::copy(
tempdir.path().join(Self::downloaded_bin_name()),
Self::final_binary(version).await?,
)
.await?;
Ok(())
}
/// Try installing wasm-bindgen-cli from source using cargo install.
async fn install_cargo(version: &str) -> anyhow::Result<()> {
tracing::debug!("Attempting to install wasm-bindgen-cli@{version} from cargo-install");
let package = Self::cargo_bin_name(version);
let tempdir = TempDir::new()?;
// Run install command
Command::new("cargo")
.args([
"install",
&package,
"--bin",
"wasm-bindgen",
"--no-track",
"--force",
"--root",
])
.arg(tempdir.path())
.stdout(Stdio::piped())
.stderr(Stdio::piped())
.output()
.await
.context("failed to install wasm-bindgen-cli from cargo-install")?;
tracing::info!("Copying into path: {}", tempdir.path().display());
// copy the wasm-bindgen out of the tempdir to the final location
fs::copy(
tempdir.path().join("bin").join(Self::downloaded_bin_name()),
Self::final_binary(version).await?,
)
.await
.context("failed to copy wasm-bindgen binary")?;
Ok(())
}
/// Get the installation directory for the wasm-bindgen executable.
async fn install_dir() -> anyhow::Result<PathBuf> {
let bindgen_dir = dirs::data_local_dir()
.expect("user should be running on a compatible operating system")
.join("dioxus/wasm-bindgen/");
fs::create_dir_all(&bindgen_dir).await?;
Ok(bindgen_dir)
}
/// Get the name of a potentially installed wasm-bindgen binary.
fn installed_bin_name(version: &str) -> String {
let mut name = format!("wasm-bindgen-{version}");
if cfg!(windows) {
name = format!("{name}.exe");
}
name
}
/// Get the crates.io package name of wasm-bindgen-cli.
fn cargo_bin_name(version: &str) -> String {
format!("wasm-bindgen-cli@{version}")
}
async fn final_binary(version: &str) -> Result<PathBuf, anyhow::Error> {
let installed_name = Self::installed_bin_name(version);
let install_dir = Self::install_dir().await?;
Ok(install_dir.join(installed_name))
}
fn downloaded_bin_name() -> &'static str {
if cfg!(windows) {
"wasm-bindgen.exe"
} else {
"wasm-bindgen"
}
}
}
/// Get the GitHub installation URL for wasm-bindgen if it exists.
fn git_install_url(version: &str) -> Option<String> {
let platform = if cfg!(all(target_os = "windows", target_arch = "x86_64")) {
"x86_64-pc-windows-msvc"
} else if cfg!(all(target_os = "linux", target_arch = "x86_64")) {
"x86_64-unknown-linux-musl"
} else if cfg!(all(target_os = "linux", target_arch = "aarch64")) {
"aarch64-unknown-linux-gnu"
} else if cfg!(all(target_os = "macos", target_arch = "x86_64")) {
"x86_64-apple-darwin"
} else if cfg!(all(target_os = "macos", target_arch = "aarch64")) {
"aarch64-apple-darwin"
} else {
return None;
};
Some(format!("https://github.com/rustwasm/wasm-bindgen/releases/download/{version}/wasm-bindgen-{version}-{platform}.tar.gz"))
}
/// A builder for WasmBindgen options.
pub(crate) struct WasmBindgenBuilder {
version: String,
input_path: PathBuf,
out_dir: PathBuf,
out_name: String,
target: String,
debug: bool,
keep_debug: bool,
demangle: bool,
remove_name_section: bool,
remove_producers_section: bool,
}
impl WasmBindgenBuilder {
pub fn new(version: String) -> Self {
Self {
version,
input_path: PathBuf::new(),
out_dir: PathBuf::new(),
out_name: String::new(),
target: String::new(),
debug: true,
keep_debug: true,
demangle: true,
remove_name_section: false,
remove_producers_section: false,
}
}
pub fn build(self) -> WasmBindgen {
WasmBindgen {
version: self.version,
input_path: self.input_path,
out_dir: self.out_dir,
out_name: self.out_name,
target: self.target,
debug: self.debug,
keep_debug: self.keep_debug,
demangle: self.demangle,
remove_name_section: self.remove_name_section,
remove_producers_section: self.remove_producers_section,
}
}
pub fn input_path(self, input_path: &Path) -> Self {
Self {
input_path: input_path.to_path_buf(),
..self
}
}
pub fn out_dir(self, out_dir: &Path) -> Self {
Self {
out_dir: out_dir.to_path_buf(),
..self
}
}
pub fn out_name(self, out_name: &str) -> Self {
Self {
out_name: out_name.to_string(),
..self
}
}
pub fn target(self, target: &str) -> Self {
Self {
target: target.to_string(),
..self
}
}
pub fn debug(self, debug: bool) -> Self {
Self { debug, ..self }
}
pub fn keep_debug(self, keep_debug: bool) -> Self {
Self { keep_debug, ..self }
}
pub fn demangle(self, demangle: bool) -> Self {
Self { demangle, ..self }
}
pub fn remove_name_section(self, remove_name_section: bool) -> Self {
Self {
remove_name_section,
..self
}
}
pub fn remove_producers_section(self, remove_producers_section: bool) -> Self {
Self {
remove_producers_section,
..self
}
}
}
#[cfg(test)]
mod test {
use super::*;
const VERSION: &str = "0.2.99";
/// Test the github installer.
#[tokio::test]
async fn test_github_install() {
reset_test().await;
WasmBindgen::install_github(VERSION).await.unwrap();
test_verify_install().await;
verify_installation().await;
}
/// Test the cargo installer.
#[tokio::test]
async fn test_cargo_install() {
reset_test().await;
WasmBindgen::install_cargo(VERSION).await.unwrap();
test_verify_install().await;
verify_installation().await;
}
// CI doesn't have binstall.
// Test the binstall installer
// #[tokio::test]
// async fn test_binstall_install() {
// reset_test().await;
// WasmBindgen::install_binstall(VERSION).await.unwrap();
// test_verify_install().await;
// verify_installation().await;
// }
/// Helper to test `WasmBindgen::verify_install` after an installation.
async fn test_verify_install() {
// Test install verification
let is_installed = WasmBindgen::verify_install(VERSION).await.unwrap();
assert!(
is_installed,
"wasm-bingen install verification returned false after installation"
);
}
/// Helper to test that the installed binary actually exists.
async fn verify_installation() {
let path = WasmBindgen::install_dir().await.unwrap();
let name = WasmBindgen::installed_bin_name(VERSION);
let binary = path.join(name);
assert!(
binary.exists(),
"wasm-bindgen binary doesn't exist after installation"
);
}
/// Delete the installed binary. The temp folder should be automatically deleted.
async fn reset_test() {
let path = WasmBindgen::install_dir().await.unwrap();
let name = WasmBindgen::installed_bin_name(VERSION);
let binary = path.join(name);
let _ = fs::remove_file(binary).await;
}
}