1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224
#[allow(unused)]
use std::{
collections::HashSet,
env, fs,
path::{Path, PathBuf},
process::Command,
time::Instant,
};
use crate::{
commands::{BuildOutput, FailedBuildOutput},
error_handling::emit_ezno_diagnostic,
utilities::print_to_cli,
};
use argh::FromArgs;
use parser::SourceId;
// use checker::{
// BuildOutput, Plugin, Project, TypeCheckSettings, TypeCheckingVisitorGenerators,
// TypeDefinitionModulePath,
// };
/// Ezno Compiler
#[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),
Check(CheckArguments),
// Run(RunArguments),
// Repl(repl::ReplArguments),
// #[cfg(debug_assertions)]
// Pack(Pack),
}
/// Display Ezno information
#[derive(FromArgs, Debug)]
#[argh(subcommand, name = "info")]
struct Info {}
// /// Generates binary form of a type definition module
// #[derive(FromArgs, Debug)]
// #[argh(subcommand, name = "pack")]
// struct Pack {
// /// path to module
// #[argh(positional)]
// input: PathBuf,
// /// output path
// #[argh(positional)]
// output: PathBuf,
// }
/// Build project
#[derive(FromArgs, PartialEq, Debug)]
#[argh(subcommand, name = "build")]
pub(crate) struct BuildArguments {
/// path to input file
#[argh(positional)]
pub input: PathBuf,
/// path to output
#[argh(positional)]
pub output: Option<PathBuf>,
/// whether to minify build output
#[argh(switch, short = 'm')]
pub minify: bool,
/// paths to definition files
#[argh(option, short = 'd')]
pub definition_file: Option<PathBuf>,
/// whether to include comments in the output
#[argh(switch)]
pub no_comments: bool,
/// build source maps
#[argh(switch)]
pub source_maps: bool,
#[cfg(not(target_family = "wasm"))]
/// whether to display compile times
#[argh(switch)]
pub timings: bool,
// /// whether to re-build on file changes
// #[argh(switch)]
// watch: bool,
}
/// Type check project
#[derive(FromArgs, PartialEq, Debug)]
#[argh(subcommand, name = "check")]
pub(crate) struct CheckArguments {
/// path to input file
#[argh(positional)]
pub input: PathBuf,
/// paths to definition files
#[argh(option, short = 'd')]
pub definition_file: Option<PathBuf>,
/// whether to re-check on file changes
#[argh(switch)]
pub watch: bool,
}
// /// Run project using Deno
// #[derive(FromArgs, PartialEq, Debug)]
// #[argh(subcommand, name = "run")]
// struct RunArguments {
// /// path to input file
// #[argh(positional)]
// input: PathBuf,
// /// path to output
// #[argh(positional)]
// output: PathBuf,
// /// whether to re-run on file changes
// #[argh(switch)]
// watch: bool,
// }
#[allow(unused)]
fn file_system_resolver(path: &Path) -> Option<String> {
// Cheaty
if path.to_str() == Some("BLANK") {
return Some(String::new());
}
match fs::read_to_string(path) {
Ok(source) => Some(source),
Err(_) => None,
}
}
pub fn run_cli<T: crate::FSResolver, U: crate::CLIInputResolver>(
cli_arguments: &[&str],
fs_resolver: T,
cli_input_resolver: U,
) {
let command = match FromArgs::from_args(&["ezno-cli"], cli_arguments) {
Ok(TopLevel { nested }) => nested,
Err(err) => {
print_to_cli(format_args!("{}", err.output));
return;
}
};
match command {
CompilerSubCommand::Info(_) => {
crate::utilities::print_info();
}
CompilerSubCommand::Build(build_config) => {
let output_path = build_config.output.unwrap_or("ezno_output.js".into());
let output = crate::commands::build(&fs_resolver, &build_config.input, build_config.definition_file.as_deref(), &output_path, build_config.minify);
match output {
Ok(BuildOutput { diagnostics, fs, outputs }) => {
for output in outputs {
std::fs::write(output.output_path, output.content).unwrap();
}
for diagnostic in diagnostics.into_iter() {
let source_id = diagnostic.sources().next().unwrap_or(SourceId::NULL);
emit_ezno_diagnostic(diagnostic, &fs, source_id).unwrap();
}
}
Err(FailedBuildOutput { fs, diagnostics }) => {
for diagnostic in diagnostics.into_iter() {
let source_id = diagnostic.sources().next().unwrap_or(SourceId::NULL);
emit_ezno_diagnostic(diagnostic, &fs, source_id).unwrap();
}
}
}
}
CompilerSubCommand::ASTExplorer(mut repl) => repl.run(fs_resolver, cli_input_resolver),
CompilerSubCommand::Check(check_arguments) => {
let CheckArguments { input, watch: _, definition_file } = check_arguments;
let (fs, diagnostics, _others) = crate::commands::check(&fs_resolver, &input, definition_file.as_deref());
for diagnostic in diagnostics.into_iter() {
let source_id = diagnostic.sources().next().unwrap_or(SourceId::NULL);
emit_ezno_diagnostic(diagnostic, &fs, source_id).unwrap();
}
}
// CompilerSubCommand::Run(run_arguments) => {
// let build_arguments = BuildArguments {
// input: run_arguments.input,
// output: Some(run_arguments.output.clone()),
// minify: true,
// no_comments: true,
// source_maps: false,
// watch: false,
// timings: false,
// };
// let output = build(build_arguments);
// if output.is_ok() {
// Command::new("deno")
// .args(["run", "--allow-all", run_arguments.output.to_str().unwrap()])
// .spawn()
// .unwrap()
// .wait()
// .unwrap();
// }
// }
// #[cfg(debug_assertions)]
// CompilerSubCommand::Pack(Pack { input, output }) => {
// let file = checker::definition_file_to_buffer(
// &file_system_resolver,
// &env::current_dir().unwrap(),
// &input,
// )
// .unwrap();
// std::fs::write(&output, &file).unwrap();
// // println!("Wrote binary context out to {}", output.display());
// let _root_ctx = checker::root_context_from_bytes(file);
// println!("Registered {} types", _root_ctx.types.len());
// }
// CompilerSubCommand::Repl(argument) => repl::run_deno_repl(argument),
}
}
/// TODO + deserialize
struct _Settings {
current_working_directory: Option<PathBuf>,
}