diff --git a/crates/aiken/src/lib.rs b/crates/aiken/src/lib.rs index d730ee9f..a2627f4c 100644 --- a/crates/aiken/src/lib.rs +++ b/crates/aiken/src/lib.rs @@ -4,16 +4,34 @@ use aiken_project::{ telemetry::{self, DownloadSource}, Project, }; +use miette::Diagnostic; use miette::IntoDiagnostic; use owo_colors::{ OwoColorize, Stream::{self, Stderr}, }; -use std::{collections::BTreeMap, env, path::PathBuf, process}; +use std::{ + collections::BTreeMap, + env, + fmt::{self, Display}, + path::PathBuf, +}; use uplc::machine::cost_model::ExBudget; pub mod cmd; +#[derive(Debug, Diagnostic, thiserror::Error)] +enum ExitFailure { + #[error("")] + ExitFailure, +} + +impl ExitFailure { + fn into_report() -> miette::Report { + ExitFailure::ExitFailure.into() + } +} + pub fn with_project(directory: Option, deny: bool, mut action: A) -> miette::Result<()> where A: FnMut(&mut Project) -> Result<(), Vec>, @@ -25,12 +43,12 @@ where }; let mut project = match Project::new(project_path, Terminal) { - Ok(p) => p, + Ok(p) => Ok(p), Err(e) => { e.report(); - process::exit(1); + Err(ExitFailure::into_report()) } - }; + }?; let build_result = action(&mut project); @@ -42,8 +60,6 @@ where warning.report() } - let plural = if warning_count == 1 { "" } else { "s" }; - if let Err(errs) = build_result { for err in &errs { err.report() @@ -51,133 +67,58 @@ where eprintln!( "\n{}", - "Summary" - .if_supports_color(Stderr, |s| s.purple()) - .if_supports_color(Stderr, |s| s.bold()) + Summary { + warning_count, + error_count: errs.len(), + } ); - let warning_text = format!("{warning_count} warning{plural}"); - - let plural = if errs.len() == 1 { "" } else { "s" }; - - let error_text = format!("{} error{}", errs.len(), plural); - - let full_summary = format!( - " {}, {}", - error_text.if_supports_color(Stderr, |s| s.red()), - warning_text.if_supports_color(Stderr, |s| s.yellow()) - ); - - eprintln!("{full_summary}"); - - process::exit(1); + return Err(ExitFailure::into_report()); } else { eprintln!( "\n{}", - "Summary" - .if_supports_color(Stderr, |s| s.purple()) - .if_supports_color(Stderr, |s| s.bold()) - ); - - let warning_text = format!("{warning_count} warning{plural}"); - - eprintln!( - " 0 errors, {}", - warning_text.if_supports_color(Stderr, |s| s.yellow()), + Summary { + error_count: 0, + warning_count + } ); } if warning_count > 0 && deny { - process::exit(1); + Err(ExitFailure::into_report()) + } else { + Ok(()) } - - Ok(()) } -// TODO: we probably want to rework with_project slightly to avoid duplication here, -// but this is a quick hack to get the aiken watch working -pub fn with_project_ok( - directory: Option, - deny: bool, - mut action: A, -) -> miette::Result<()> -where - A: FnMut(&mut Project) -> Result<(), Vec>, -{ - let project_path = if let Some(d) = directory { - d - } else { - env::current_dir().into_diagnostic()? - }; +struct Summary { + warning_count: usize, + error_count: usize, +} - let mut project = match Project::new(project_path, Terminal) { - Ok(p) => p, - Err(e) => { - e.report(); - return Ok(()); - } - }; - - let build_result = action(&mut project); - - let warnings = project.warnings(); - - let warning_count = warnings.len(); - - for warning in &warnings { - warning.report() - } - - let plural = if warning_count == 1 { "" } else { "s" }; - - if let Err(errs) = build_result { - for err in &errs { - err.report() - } - - eprintln!( - "\n{}", +impl Display for Summary { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + f.write_str(&format!( + "{}\n {} {}, {} {}", "Summary" .if_supports_color(Stderr, |s| s.purple()) - .if_supports_color(Stderr, |s| s.bold()) - ); - - let warning_text = format!("{warning_count} warning{plural}"); - - let plural = if errs.len() == 1 { "" } else { "s" }; - - let error_text = format!("{} error{}", errs.len(), plural); - - let full_summary = format!( - " {}, {}", - error_text.if_supports_color(Stderr, |s| s.red()), - warning_text.if_supports_color(Stderr, |s| s.yellow()) - ); - - eprintln!("{full_summary}"); - - return Ok(()); - } else { - eprintln!( - "\n{}", - "Summary" - .if_supports_color(Stderr, |s| s.purple()) - .if_supports_color(Stderr, |s| s.bold()) - ); - - let warning_text = format!("{warning_count} warning{plural}"); - - eprintln!( - " 0 errors, {}", - warning_text.if_supports_color(Stderr, |s| s.yellow()), - ); + .if_supports_color(Stderr, |s| s.bold()), + self.error_count, + if self.error_count == 1 { + "error" + } else { + "errors" + } + .if_supports_color(Stderr, |s| s.red()), + self.warning_count, + if self.warning_count == 1 { + "warning" + } else { + "warnings" + } + .if_supports_color(Stderr, |s| s.yellow()), + )) } - - if warning_count > 0 && deny { - return Ok(()); - } - - Ok(()) } #[derive(Debug, Default, Clone, Copy)] diff --git a/crates/aiken/src/main.rs b/crates/aiken/src/main.rs index 777766c8..7ca09dbe 100644 --- a/crates/aiken/src/main.rs +++ b/crates/aiken/src/main.rs @@ -5,13 +5,13 @@ use aiken::cmd::{ tx, uplc, Cmd, }; use aiken_project::{config, pretty}; - use owo_colors::OwoColorize; +use std::process; -fn main() -> miette::Result<()> { +fn main() { panic_handler(); - match Cmd::default() { + let result = match Cmd::default() { Cmd::New(args) => new::exec(args), Cmd::Fmt(args) => fmt::exec(args), Cmd::Build(args) => build::exec(args), @@ -25,6 +25,11 @@ fn main() -> miette::Result<()> { Cmd::Tx(sub_cmd) => tx::exec(sub_cmd), Cmd::Uplc(sub_cmd) => uplc::exec(sub_cmd), Cmd::Completion(sub_cmd) => completion::exec(sub_cmd), + }; + + match result { + Ok(()) => (), + Err(_) => process::exit(1), } }