Skip to main content

vertigo_testing/
build.rs

1//! Builds what a suite serves: the WASM apps, with the vertigo-cli library, and the backend,
2//! with cargo.
3//!
4//! The apps are built by the same vertigo-cli the backend or [`crate::serve`] uses for SSR, so
5//! a vertigo change is always tested on both sides at once.
6//!
7//! With `E2E_SKIP_BUILD` set, nothing is built, only checked to be there from the last run.
8
9use std::path::{Path, PathBuf};
10
11use anyhow::{Context, Result, anyhow, bail, ensure};
12use vertigo_cli::{
13    BuildOpts, CommonOpts,
14    build::{self, BuildOptsInner},
15};
16
17use crate::env::Settings;
18
19/// Builds the app `package` into `dest_dir`, as `vertigo build` does.
20pub async fn vertigo_app(settings: &Settings, package: &str, dest_dir: &Path) -> Result<()> {
21    if settings.skip_build {
22        println!("E2E_SKIP_BUILD is set, reusing {}", dest_dir.display());
23    } else {
24        println!("Building {package} into {}", dest_dir.display());
25
26        let opts = BuildOpts {
27            inner: BuildOptsInner {
28                package_name: Some(package.to_string()),
29                public_path: None,
30                wasm_opt: Some(settings.wasm_opt),
31                release_mode: Some(settings.release),
32                wasm_run_source_map: false,
33                cargo_opts: vec![],
34            },
35            common: CommonOpts {
36                dest_dir: dest_dir.to_string_lossy().into_owned(),
37                log_local_time: None,
38            },
39        };
40
41        let package = package.to_string();
42        tokio::task::spawn_blocking(move || {
43            build::run(opts).map_err(|err| anyhow!("building {package} failed: {err:?}"))
44        })
45        .await??;
46    }
47
48    let index = dest_dir.join("index.json");
49    ensure!(index.is_file(), "{} is missing", index.display());
50    Ok(())
51}
52
53/// Builds the binary `name` of the package `name` in the workspace and returns its path.
54pub async fn cargo_bin(settings: &Settings, name: &str) -> Result<PathBuf> {
55    let bin = settings.target_dir.join(settings.profile()).join(name);
56
57    if !settings.skip_build {
58        println!("Building {name}");
59
60        let cargo = std::env::var_os("CARGO").unwrap_or_else(|| "cargo".into());
61        let mut command = tokio::process::Command::new(cargo);
62        command.current_dir(&settings.workspace_root).args([
63            "build",
64            "--package",
65            name,
66            "--bin",
67            name,
68        ]);
69        if settings.release {
70            command.arg("--release");
71        }
72
73        let status = command.status().await.context("can't run cargo")?;
74        if !status.success() {
75            bail!("building {name} failed ({status})");
76        }
77    }
78
79    ensure!(bin.is_file(), "binary {} is missing", bin.display());
80    Ok(bin)
81}