#[allow(unused)]
use std::{
collections::HashSet,
env, fs,
path::{Path, PathBuf},
process::Command,
time::Instant,
};
use crate::{error_handling::emit_ezno_diagnostic, temp::Output, utilities};
use crate::{temp::BuildOutput, utilities::print_to_cli};
use argh::FromArgs;
#[derive(FromArgs, Debug)]
struct TopLevel {
#[argh(subcommand)]
nested: CompilerSubCommand,
}
#[derive(FromArgs, Debug)]
#[argh(subcommand)]
enum CompilerSubCommand {
Info(Info),
Build(BuildArguments),
ASTExplorer(crate::ast_explorer::ExplorerArguments),
}
#[derive(FromArgs, Debug)]
#[argh(subcommand, name = "info")]
struct Info {}
#[derive(FromArgs, PartialEq, Debug)]
#[argh(subcommand, name = "build")]
struct BuildArguments {
#[argh(positional)]
input: PathBuf,
#[argh(positional)]
output: Option<PathBuf>,
#[argh(switch, short = 'm')]
minify: bool,
#[argh(switch)]
no_comments: bool,
#[argh(switch)]
source_maps: bool,
#[cfg(not(target_family = "wasm"))]
#[argh(switch)]
timings: bool,
}
#[allow(unused)]
fn file_system_resolver(path: &Path) -> Option<(String, Vec<(usize, parser::EmptyCursorId)>)> {
if path.to_str() == Some("BLANK") {
return Some((String::new(), Vec::new()));
}
match fs::read_to_string(path) {
Ok(source) => Some((source, Vec::new())),
Err(_) => None,
}
}
#[cfg_attr(target_family = "wasm", wasm_bindgen::prelude::wasm_bindgen)]
pub fn run_cli() {
let get_cli_args = crate::utilities::get_cli_args();
let arguments: Vec<&str> = get_cli_args.iter().map(AsRef::as_ref).collect();
if arguments.is_empty() {
utilities::print_info();
return;
}
let command = match FromArgs::from_args(&["ezno-cli"], arguments.as_slice()) {
Ok(TopLevel { nested }) => nested,
Err(err) => {
print_to_cli(format_args!("{}", err.output));
return;
}
};
match command {
CompilerSubCommand::Info(_) => {
utilities::print_info();
}
CompilerSubCommand::Build(build_config) => {
let _output = build(build_config);
}
CompilerSubCommand::ASTExplorer(mut repl) => repl.run(),
}
}
struct _Settings {
current_working_directory: Option<PathBuf>,
}
fn build(build_arguments: BuildArguments) -> Result<(), ()> {
let BuildArguments {
minify,
no_comments,
input: entry_path,
output: output_path,
source_maps: _,
#[cfg(not(target_family = "wasm"))]
timings,
} = build_arguments;
let _output_settings = parser::ToStringSettings {
include_comments: !no_comments,
pretty: !minify,
..Default::default()
};
#[cfg(not(target_family = "wasm"))]
let now = Instant::now();
let (fs, result) = crate::temp::build(
utilities::read_fs_path_to_string(entry_path.clone()).expect("Could not read path"),
entry_path.display().to_string(),
output_path
.map(|path| path.into_os_string().into_string().expect("Invalid path"))
.unwrap_or_default(),
);
#[cfg(not(target_family = "wasm"))]
if timings {
let elapsed = now.elapsed();
eprintln!("Project built in {:?}", elapsed);
}
match result {
Ok(BuildOutput { outputs, temp_warnings_and_infos }) => {
for error in temp_warnings_and_infos {
emit_ezno_diagnostic(&fs, error).unwrap();
}
for Output { content, .. } in outputs {
print_to_cli(format_args!("{content}"));
}
Ok(())
}
Err(errors) => {
for error in errors {
emit_ezno_diagnostic(&fs, error).unwrap();
}
Err(())
}
}
}