-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.rs
More file actions
66 lines (52 loc) · 1.81 KB
/
build.rs
File metadata and controls
66 lines (52 loc) · 1.81 KB
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
use std::env;
use std::error::Error;
use std::fs;
use std::path::{Path, PathBuf};
use std::process::Command;
fn main() -> Result<(), Box<dyn Error>> {
println!("cargo:rerun-if-changed=src/kit.md");
if env::var_os("CARGO_FEATURE_TEST_RUNNER_BUNDLE").is_none() {
return Ok(());
}
let manifest_dir = env::var("CARGO_MANIFEST_DIR")?;
let test_runtime_dir = target_dir()?.join("test-runtime");
let bundled_runner = test_runtime_dir.join("runner.cjs");
bundle_test_runner(&manifest_dir, &bundled_runner)?;
println!(
"cargo:rustc-env=CLANKERFLOW_TEST_RUNNER_BUNDLE={}",
bundled_runner.display()
);
Ok(())
}
fn bundle_test_runner(manifest_dir: &str, bundled_runner: &Path) -> Result<(), Box<dyn Error>> {
if let Some(parent) = bundled_runner.parent() {
fs::create_dir_all(parent)?;
}
let esbuild = Path::new(manifest_dir).join("runtime/node_modules/esbuild/bin/esbuild");
if !esbuild.exists() {
println!(
"cargo:warning=Skipping test runner bundle; missing {}",
esbuild.display()
);
return Ok(());
}
let entrypoint = Path::new(manifest_dir).join("runtime/src/runner.ts");
let status = Command::new(esbuild)
.arg(entrypoint)
.args(["--bundle", "--platform=node", "--format=cjs"])
.arg(format!("--outfile={}", bundled_runner.display()))
.status()?;
if !status.success() {
return Err("failed to bundle test runner with esbuild".into());
}
Ok(())
}
fn target_dir() -> Result<PathBuf, Box<dyn Error>> {
let out_dir = env::var("OUT_DIR")?;
let out_path = Path::new(&out_dir);
let target = out_path
.ancestors()
.nth(4)
.ok_or("failed to derive Cargo target directory from OUT_DIR")?;
Ok(target.to_path_buf())
}