Rework 'with_project' to avoid early process exit.

This commit is contained in:
KtorZ 2023-11-25 13:26:24 +01:00
parent 4adedaac15
commit 777d30b8ac
No known key found for this signature in database
GPG Key ID: 33173CB6F77F4277
2 changed files with 66 additions and 120 deletions

View File

@ -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<A>(directory: Option<PathBuf>, deny: bool, mut action: A) -> miette::Result<()>
where
A: FnMut(&mut Project<Terminal>) -> Result<(), Vec<aiken_project::error::Error>>,
@ -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(())
}
}
// 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<A>(
directory: Option<PathBuf>,
deny: bool,
mut action: A,
) -> miette::Result<()>
where
A: FnMut(&mut Project<Terminal>) -> Result<(), Vec<aiken_project::error::Error>>,
{
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(());
.if_supports_color(Stderr, |s| s.bold()),
self.error_count,
if self.error_count == 1 {
"error"
} 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()),
);
"errors"
}
if warning_count > 0 && deny {
return Ok(());
.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()),
))
}
Ok(())
}
#[derive(Debug, Default, Clone, Copy)]

View File

@ -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),
}
}