use crate::path;
use clap::Parser;
use std::fs;
use std::path::{Path, PathBuf};
use toml_edit::DocumentMut;
#[derive(Parser)]
pub struct Args {
#[arg(long, default_value = "out/rom.bin")]
rom: String,
#[arg(long, default_value = ".")]
dir: String,
}
pub fn run(args: &Args) {
let rom_path = &args.rom;
let rom_src = Path::new(rom_path);
if !rom_src.exists() {
eprintln!("❌ ROM file not found: {}", rom_src.display());
std::process::exit(1);
}
let parent_dir = &args.dir;
let out_path = Path::new(parent_dir).join("web-export");
if !out_path.exists() {
fs::create_dir_all(&out_path).expect("Failed to create output directory");
}
let template_dir = get_template_directory();
match template_dir {
Some(template_dir) => {
println!("Using web template from: {}", template_dir.display());
copy_directory(&template_dir, &out_path).expect("Failed to copy template files");
}
None => {
eprintln!("❌ Web template not found. Please run `sgdkx setup-web` first.");
std::process::exit(1);
}
}
let rom_dest = out_path.join("rom.bin");
fs::copy(&rom_src, &rom_dest).expect("Failed to copy ROM file");
println!("✅ Web export complete!");
println!(" Output directory: {}", out_path.display());
println!(" Run sgdkx web-server");
}
fn get_template_directory() -> Option<PathBuf> {
let config_path = path::config_dir().join("config.toml");
if config_path.exists() {
let config_str = match fs::read_to_string(&config_path) {
Ok(s) => s,
Err(_) => return None,
};
let doc = match config_str.parse::<DocumentMut>() {
Ok(d) => d,
Err(_) => return None,
};
if let Some(web_export) = doc.get("web_export") {
if let Some(path_value) = web_export.get("template_path").and_then(|v| v.as_str()) {
let template_dir = PathBuf::from(path_value);
if template_dir.exists() {
return Some(template_dir);
}
}
}
}
None
}
fn copy_directory(src: &Path, dst: &Path) -> std::io::Result<()> {
if !dst.exists() {
fs::create_dir_all(dst)?;
}
for entry in fs::read_dir(src)? {
let entry = entry?;
let src_path = entry.path();
let dst_path = dst.join(entry.file_name());
if src_path.is_dir() {
copy_directory(&src_path, &dst_path)?;
} else {
fs::copy(&src_path, &dst_path)?;
}
}
Ok(())
}