Rework 'with_project' to avoid early process exit.
This commit is contained in:
parent
4adedaac15
commit
777d30b8ac
|
@ -4,16 +4,34 @@ use aiken_project::{
|
||||||
telemetry::{self, DownloadSource},
|
telemetry::{self, DownloadSource},
|
||||||
Project,
|
Project,
|
||||||
};
|
};
|
||||||
|
use miette::Diagnostic;
|
||||||
use miette::IntoDiagnostic;
|
use miette::IntoDiagnostic;
|
||||||
use owo_colors::{
|
use owo_colors::{
|
||||||
OwoColorize,
|
OwoColorize,
|
||||||
Stream::{self, Stderr},
|
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;
|
use uplc::machine::cost_model::ExBudget;
|
||||||
|
|
||||||
pub mod cmd;
|
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<()>
|
pub fn with_project<A>(directory: Option<PathBuf>, deny: bool, mut action: A) -> miette::Result<()>
|
||||||
where
|
where
|
||||||
A: FnMut(&mut Project<Terminal>) -> Result<(), Vec<aiken_project::error::Error>>,
|
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) {
|
let mut project = match Project::new(project_path, Terminal) {
|
||||||
Ok(p) => p,
|
Ok(p) => Ok(p),
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
e.report();
|
e.report();
|
||||||
process::exit(1);
|
Err(ExitFailure::into_report())
|
||||||
}
|
}
|
||||||
};
|
}?;
|
||||||
|
|
||||||
let build_result = action(&mut project);
|
let build_result = action(&mut project);
|
||||||
|
|
||||||
|
@ -42,8 +60,6 @@ where
|
||||||
warning.report()
|
warning.report()
|
||||||
}
|
}
|
||||||
|
|
||||||
let plural = if warning_count == 1 { "" } else { "s" };
|
|
||||||
|
|
||||||
if let Err(errs) = build_result {
|
if let Err(errs) = build_result {
|
||||||
for err in &errs {
|
for err in &errs {
|
||||||
err.report()
|
err.report()
|
||||||
|
@ -51,133 +67,58 @@ where
|
||||||
|
|
||||||
eprintln!(
|
eprintln!(
|
||||||
"\n{}",
|
"\n{}",
|
||||||
"Summary"
|
Summary {
|
||||||
.if_supports_color(Stderr, |s| s.purple())
|
warning_count,
|
||||||
.if_supports_color(Stderr, |s| s.bold())
|
error_count: errs.len(),
|
||||||
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
let warning_text = format!("{warning_count} warning{plural}");
|
return Err(ExitFailure::into_report());
|
||||||
|
|
||||||
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);
|
|
||||||
} else {
|
} else {
|
||||||
eprintln!(
|
eprintln!(
|
||||||
"\n{}",
|
"\n{}",
|
||||||
"Summary"
|
Summary {
|
||||||
.if_supports_color(Stderr, |s| s.purple())
|
error_count: 0,
|
||||||
.if_supports_color(Stderr, |s| s.bold())
|
warning_count
|
||||||
);
|
}
|
||||||
|
|
||||||
let warning_text = format!("{warning_count} warning{plural}");
|
|
||||||
|
|
||||||
eprintln!(
|
|
||||||
" 0 errors, {}",
|
|
||||||
warning_text.if_supports_color(Stderr, |s| s.yellow()),
|
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
if warning_count > 0 && deny {
|
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,
|
struct Summary {
|
||||||
// but this is a quick hack to get the aiken watch working
|
warning_count: usize,
|
||||||
pub fn with_project_ok<A>(
|
error_count: usize,
|
||||||
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()?
|
|
||||||
};
|
|
||||||
|
|
||||||
let mut project = match Project::new(project_path, Terminal) {
|
impl Display for Summary {
|
||||||
Ok(p) => p,
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||||
Err(e) => {
|
f.write_str(&format!(
|
||||||
e.report();
|
"{}\n {} {}, {} {}",
|
||||||
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{}",
|
|
||||||
"Summary"
|
"Summary"
|
||||||
.if_supports_color(Stderr, |s| s.purple())
|
.if_supports_color(Stderr, |s| s.purple())
|
||||||
.if_supports_color(Stderr, |s| s.bold())
|
.if_supports_color(Stderr, |s| s.bold()),
|
||||||
);
|
self.error_count,
|
||||||
|
if self.error_count == 1 {
|
||||||
let warning_text = format!("{warning_count} warning{plural}");
|
"error"
|
||||||
|
} else {
|
||||||
let plural = if errs.len() == 1 { "" } else { "s" };
|
"errors"
|
||||||
|
}
|
||||||
let error_text = format!("{} error{}", errs.len(), plural);
|
.if_supports_color(Stderr, |s| s.red()),
|
||||||
|
self.warning_count,
|
||||||
let full_summary = format!(
|
if self.warning_count == 1 {
|
||||||
" {}, {}",
|
"warning"
|
||||||
error_text.if_supports_color(Stderr, |s| s.red()),
|
} else {
|
||||||
warning_text.if_supports_color(Stderr, |s| s.yellow())
|
"warnings"
|
||||||
);
|
}
|
||||||
|
.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 warning_count > 0 && deny {
|
|
||||||
return Ok(());
|
|
||||||
}
|
|
||||||
|
|
||||||
Ok(())
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Default, Clone, Copy)]
|
#[derive(Debug, Default, Clone, Copy)]
|
||||||
|
|
|
@ -5,13 +5,13 @@ use aiken::cmd::{
|
||||||
tx, uplc, Cmd,
|
tx, uplc, Cmd,
|
||||||
};
|
};
|
||||||
use aiken_project::{config, pretty};
|
use aiken_project::{config, pretty};
|
||||||
|
|
||||||
use owo_colors::OwoColorize;
|
use owo_colors::OwoColorize;
|
||||||
|
use std::process;
|
||||||
|
|
||||||
fn main() -> miette::Result<()> {
|
fn main() {
|
||||||
panic_handler();
|
panic_handler();
|
||||||
|
|
||||||
match Cmd::default() {
|
let result = match Cmd::default() {
|
||||||
Cmd::New(args) => new::exec(args),
|
Cmd::New(args) => new::exec(args),
|
||||||
Cmd::Fmt(args) => fmt::exec(args),
|
Cmd::Fmt(args) => fmt::exec(args),
|
||||||
Cmd::Build(args) => build::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::Tx(sub_cmd) => tx::exec(sub_cmd),
|
||||||
Cmd::Uplc(sub_cmd) => uplc::exec(sub_cmd),
|
Cmd::Uplc(sub_cmd) => uplc::exec(sub_cmd),
|
||||||
Cmd::Completion(sub_cmd) => completion::exec(sub_cmd),
|
Cmd::Completion(sub_cmd) => completion::exec(sub_cmd),
|
||||||
|
};
|
||||||
|
|
||||||
|
match result {
|
||||||
|
Ok(()) => (),
|
||||||
|
Err(_) => process::exit(1),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue