Medusa/build.rs
2024-09-06 13:31:50 +02:00

87 lines
2.8 KiB
Rust

use std::env;
use std::fs;
use std::path::PathBuf;
use std::process::Command;
fn main() {
// Get the current build profile (debug or release)
let profile = env::var("PROFILE").expect("PROFILE environment variable not set");
// Determine the base directory for the build artifacts (target/debug or target/release)
let target_base_dir = PathBuf::from("target");
// Construct the target path based on the build profile
let target_profile_dir = target_base_dir.join(profile);
// Path to the Rocket.toml file you want to copy
let rocket_config_src = PathBuf::from("Rocket.toml");
// Path where the Rocket.toml file will be copied
let rocket_config_dest = target_profile_dir.join("Rocket.toml");
// Create the target profile directory if it does not exist
fs::create_dir_all(&target_profile_dir).expect("Failed to create directory");
// Copy the Rocket.toml configuration file to the target directory
fs::copy(rocket_config_src, rocket_config_dest).expect("Failed to copy Rocket.toml");
let install_cmd = "cd web && bun install";
// Execute the web build command and wait for it to finish
let _status = if cfg!(target_os = "windows") {
Command::new("cmd")
.arg("/C")
.arg(install_cmd)
.status()
.expect("Failed to execute web build command")
} else {
Command::new("sh")
.arg("-c")
.arg(install_cmd)
.status()
.expect("Failed to execute web build command")
};
let web_build_cmd = "cd web && bun run build";
// Execute the web build command and wait for it to finish
let status = if cfg!(target_os = "windows") {
Command::new("cmd")
.arg("/C")
.arg(web_build_cmd)
.status()
.expect("Failed to execute web build command")
} else {
Command::new("sh")
.arg("-c")
.arg(web_build_cmd)
.status()
.expect("Failed to execute web build command")
};
// Check if the build command was successful
if !status.success() {
panic!("Web build command failed with status: {:?}", status);
}
// Check if the build command was successful
if status.success() {
// Copy everything from web/dist to static
let web_dist_dir = PathBuf::from("web/dist");
let static_dir = PathBuf::from("static");
// Copy the contents of the web/dist directory to the static directory
let _ = fs_extra::dir::copy(
&web_dist_dir,
&static_dir,
&fs_extra::dir::CopyOptions {
overwrite: true,
content_only: true,
..Default::default()
},
);
} else {
eprintln!("Web build command failed");
}
}