use anyhow::Result;
use std::{env, process};
use crate::{cargo_command::CargoCommand, wasm_project::WasmProject};
mod builder_error;
mod cargo_command;
mod crate_info;
pub mod optimize;
mod stack_end;
mod wasm_project;
pub use stack_end::insert_stack_end_export;
pub const TARGET: &str = env!("TARGET");
pub struct WasmBuilder {
wasm_project: WasmProject,
cargo: CargoCommand,
}
impl WasmBuilder {
pub fn new() -> Self {
WasmBuilder {
wasm_project: WasmProject::new(),
cargo: CargoCommand::new(),
}
}
pub fn build(self) {
if env::var(self.cargo.skip_build_env()).is_ok() {
return;
}
if let Err(e) = self.build_project() {
eprintln!("error: {e}");
e.chain()
.skip(1)
.for_each(|cause| eprintln!("| {cause}"));
process::exit(1);
}
}
fn build_project(mut self) -> Result<()> {
self.wasm_project.generate()?;
self.cargo
.set_manifest_path(self.wasm_project.manifest_path());
self.cargo.set_target_dir(self.wasm_project.target_dir());
let profile = self.wasm_project.profile();
let profile = if profile == "debug" { "dev" } else { profile };
self.cargo.set_profile(profile.to_string());
self.cargo.run()?;
self.wasm_project.postprocess()
}
}
impl Default for WasmBuilder {
fn default() -> Self {
Self::new()
}
}
pub fn build() {
WasmBuilder::new().build();
}