use std::path::PathBuf;
use crate::prelude::*;
use clap::{arg, command, Parser};
use colored::Colorize;
use walkdir::WalkDir;
#[derive(clap::Parser, Debug)]
#[command(author, version, about, long_about = None)]
pub struct CompilerArgs {
#[arg(short, long)]
directory: Option<PathBuf>,
#[arg(short, long = "module", num_args(0..))]
module_files: Vec<PathBuf>,
#[arg(short, long, default_value = ".")]
output_path: PathBuf,
#[arg(short, long, default_value = "rasn")]
backend: String,
}
impl CompilerArgs {
pub fn drive() {
let args = CompilerArgs::parse();
let mut modules = args.module_files;
if let Some(dir) = args.directory {
for entry in WalkDir::new(dir)
.follow_links(true)
.into_iter()
.filter_map(|e| e.ok())
{
let file_name = entry.file_name().to_string_lossy();
if file_name.ends_with(".asn") || file_name.ends_with(".asn1") {
println!("Found ASN1 module {} in directory", file_name);
modules.push(entry.into_path());
}
}
}
if modules.is_empty() {
panic!(
"{}",
"Please provide either a valid path to a module or to a directory containing modules."
.red()
)
}
let results = if args.backend == "typescript" {
Compiler::<TypescriptBackend, _>::new()
.add_asn_sources_by_path(modules.into_iter())
.set_output_path(args.output_path)
.compile()
} else {
Compiler::<RasnBackend, _>::new()
.add_asn_sources_by_path(modules.into_iter())
.set_output_path(args.output_path)
.compile()
};
match results {
Ok(warnings) => {
for warning in warnings {
println!(
"{}\n{}",
"Rasn compiler warning:".yellow(),
warning.to_string().yellow()
)
}
}
Err(error) => {
println!(
"{}\n{}",
"Rasn compiler error:".red(),
error.to_string().red()
)
}
}
}
}