use clap::{Parser as ClapParser, Subcommand};
use std::error::Error;
use std::path::PathBuf;
#[cfg(feature = "memprof")]
#[global_allocator]
static GLOBAL: runec::memprof::Counting = runec::memprof::Counting;
#[derive(ClapParser)]
#[command(author, version, about = "RuneScript Compiler")]
struct Cli {
#[command(subcommand)]
command: Commands,
}
#[derive(Subcommand)]
enum Commands {
Compile {
#[arg(short, long)]
source: String,
#[arg(short, long, default_value = "./data/pack/server")]
output: String,
#[arg(long)]
pack: Option<String>,
#[arg(long, default_value_t = false)]
lint: bool,
#[arg(long, visible_alias = "recompile", default_value_t = false)]
low_mem: bool,
},
Lint {
#[arg(short, long)]
source: String,
#[arg(long)]
pack: Option<String>,
},
}
fn main() -> Result<(), Box<dyn Error>> {
tracing_subscriber::fmt()
.with_env_filter(
tracing_subscriber::EnvFilter::try_from_default_env()
.unwrap_or_else(|_| tracing_subscriber::EnvFilter::new("info")),
)
.init();
let cli = Cli::parse();
match cli.command {
Commands::Compile {
source,
output,
pack,
lint,
low_mem,
} => {
let scripts_dir = PathBuf::from(source);
let pack_dir = pack.map(PathBuf::from);
let output_dir = PathBuf::from(output);
runec::compile_with_options(
&scripts_dir,
pack_dir.as_deref(),
&output_dir,
lint,
low_mem,
)?;
}
Commands::Lint { source, pack } => {
let scripts_dir = PathBuf::from(source);
let pack_dir = pack.map(PathBuf::from);
runec::lint(&scripts_dir, pack_dir.as_deref())?;
}
}
#[cfg(feature = "memprof")]
tracing::info!(
"[mem] PEAK HEAP {:.1} MB across {} allocations",
runec::memprof::peak_bytes() as f64 / 1_048_576.0,
runec::memprof::alloc_count(),
);
Ok(())
}