Factor out common project-logic between build and check.

This commit is contained in:
KtorZ 2022-10-28 16:12:28 +02:00
parent 8d45b2a2f5
commit 4316d5c382
No known key found for this signature in database
GPG Key ID: 33173CB6F77F4277
10 changed files with 52 additions and 77 deletions

View File

@ -1,10 +1,7 @@
use miette::IntoDiagnostic;
use project::{config::Config, Project};
use std::env;
use std::path::PathBuf;
#[derive(clap::Args)]
/// Build an Aiken project at the given working directory.
/// Build an Aiken project
pub struct Args {
/// Path to project
#[clap(short, long)]
@ -12,33 +9,5 @@ pub struct Args {
}
pub fn exec(Args { directory }: Args) -> miette::Result<()> {
let project_path = if let Some(d) = directory {
d
} else {
env::current_dir().into_diagnostic()?
};
let config = Config::load(project_path.clone()).into_diagnostic()?;
let mut project = Project::new(config, project_path);
let build_result = project.build();
let warning_count = project.warnings.len();
for warning in project.warnings {
warning.report()
}
if let Err(err) = build_result {
err.report();
miette::bail!(
"failed: {} error(s), {warning_count} warning(s)",
err.total(),
);
};
println!("finished with {warning_count} warning(s)");
return Ok(());
crate::with_project(directory, |p| p.build())
}

View File

@ -1,12 +1,7 @@
use miette::IntoDiagnostic;
use project::{config::Config, Project};
use std::env;
use std::path::PathBuf;
// TODO: Refactor this to remove logic duplication with the 'build command'
#[derive(clap::Args)]
/// Typecheck a project project
/// Type-check an Aiken project
pub struct Args {
/// Path to project
#[clap(short, long)]
@ -14,33 +9,5 @@ pub struct Args {
}
pub fn exec(Args { directory }: Args) -> miette::Result<()> {
let project_path = if let Some(d) = directory {
d
} else {
env::current_dir().into_diagnostic()?
};
let config = Config::load(project_path.clone()).into_diagnostic()?;
let mut project = Project::new(config, project_path);
let build_result = project.check();
let warning_count = project.warnings.len();
for warning in project.warnings {
warning.report()
}
if let Err(err) = build_result {
err.report();
miette::bail!(
"failed: {} error(s), {warning_count} warning(s)",
err.total(),
);
};
println!("finished with {warning_count} warning(s)");
return Ok(());
crate::with_project(directory, |p| p.check())
}

View File

@ -16,5 +16,5 @@ pub fn exec(Args { name }: Args) -> miette::Result<()> {
fs::create_dir_all(name.join("scripts")).into_diagnostic()?;
}
return Ok(());
Ok(())
}

View File

@ -121,5 +121,5 @@ pub fn exec(
}
}
return Ok(());
Ok(())
}

View File

@ -71,5 +71,5 @@ pub fn exec(Args { script, flat, args }: Args) -> miette::Result<()> {
println!("\nLogs\n----\n{}", logs.join("\n"))
}
return Ok(());
Ok(())
}

View File

@ -79,5 +79,5 @@ pub fn exec(
}
}
return Ok(());
Ok(())
}

View File

@ -26,5 +26,5 @@ pub fn exec(Args { input, print }: Args) -> miette::Result<()> {
fs::write(&input, pretty).into_diagnostic()?;
}
return Ok(());
Ok(())
}

View File

@ -57,5 +57,5 @@ pub fn exec(
fs::write(&out_name, pretty).into_diagnostic()?;
}
return Ok(());
Ok(())
}

41
crates/cli/src/lib.rs Normal file
View File

@ -0,0 +1,41 @@
pub mod cmd;
use miette::IntoDiagnostic;
use project::{config::Config, Project};
use std::env;
use std::path::PathBuf;
pub fn with_project<A>(directory: Option<PathBuf>, mut action: A) -> miette::Result<()>
where
A: FnMut(&mut Project) -> Result<(), project::error::Error>,
{
let project_path = if let Some(d) = directory {
d
} else {
env::current_dir().into_diagnostic()?
};
let config = Config::load(project_path.clone()).into_diagnostic()?;
let mut project = Project::new(config, project_path);
let build_result = action(&mut project);
let warning_count = project.warnings.len();
for warning in project.warnings {
warning.report()
}
if let Err(err) = build_result {
err.report();
miette::bail!(
"failed: {} error(s), {warning_count} warning(s)",
err.total(),
);
};
println!("finished with {warning_count} warning(s)");
Ok(())
}

View File

@ -1,7 +1,5 @@
mod cmd;
use aiken::cmd::{build, check, new, tx, uplc};
use clap::Parser;
use cmd::{build, check, new, tx, uplc};
/// Aiken: a smart-contract language and toolchain for Cardano
#[derive(Parser)]