Factor out common project-logic between build and check.
This commit is contained in:
@@ -1,10 +1,7 @@
|
|||||||
use miette::IntoDiagnostic;
|
|
||||||
use project::{config::Config, Project};
|
|
||||||
use std::env;
|
|
||||||
use std::path::PathBuf;
|
use std::path::PathBuf;
|
||||||
|
|
||||||
#[derive(clap::Args)]
|
#[derive(clap::Args)]
|
||||||
/// Build an Aiken project at the given working directory.
|
/// Build an Aiken project
|
||||||
pub struct Args {
|
pub struct Args {
|
||||||
/// Path to project
|
/// Path to project
|
||||||
#[clap(short, long)]
|
#[clap(short, long)]
|
||||||
@@ -12,33 +9,5 @@ pub struct Args {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn exec(Args { directory }: Args) -> miette::Result<()> {
|
pub fn exec(Args { directory }: Args) -> miette::Result<()> {
|
||||||
let project_path = if let Some(d) = directory {
|
crate::with_project(directory, |p| p.build())
|
||||||
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(());
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,12 +1,7 @@
|
|||||||
use miette::IntoDiagnostic;
|
|
||||||
use project::{config::Config, Project};
|
|
||||||
use std::env;
|
|
||||||
use std::path::PathBuf;
|
use std::path::PathBuf;
|
||||||
|
|
||||||
// TODO: Refactor this to remove logic duplication with the 'build command'
|
|
||||||
|
|
||||||
#[derive(clap::Args)]
|
#[derive(clap::Args)]
|
||||||
/// Typecheck a project project
|
/// Type-check an Aiken project
|
||||||
pub struct Args {
|
pub struct Args {
|
||||||
/// Path to project
|
/// Path to project
|
||||||
#[clap(short, long)]
|
#[clap(short, long)]
|
||||||
@@ -14,33 +9,5 @@ pub struct Args {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn exec(Args { directory }: Args) -> miette::Result<()> {
|
pub fn exec(Args { directory }: Args) -> miette::Result<()> {
|
||||||
let project_path = if let Some(d) = directory {
|
crate::with_project(directory, |p| p.check())
|
||||||
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(());
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -16,5 +16,5 @@ pub fn exec(Args { name }: Args) -> miette::Result<()> {
|
|||||||
fs::create_dir_all(name.join("scripts")).into_diagnostic()?;
|
fs::create_dir_all(name.join("scripts")).into_diagnostic()?;
|
||||||
}
|
}
|
||||||
|
|
||||||
return Ok(());
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -121,5 +121,5 @@ pub fn exec(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return Ok(());
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -71,5 +71,5 @@ pub fn exec(Args { script, flat, args }: Args) -> miette::Result<()> {
|
|||||||
println!("\nLogs\n----\n{}", logs.join("\n"))
|
println!("\nLogs\n----\n{}", logs.join("\n"))
|
||||||
}
|
}
|
||||||
|
|
||||||
return Ok(());
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -79,5 +79,5 @@ pub fn exec(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return Ok(());
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -26,5 +26,5 @@ pub fn exec(Args { input, print }: Args) -> miette::Result<()> {
|
|||||||
fs::write(&input, pretty).into_diagnostic()?;
|
fs::write(&input, pretty).into_diagnostic()?;
|
||||||
}
|
}
|
||||||
|
|
||||||
return Ok(());
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -57,5 +57,5 @@ pub fn exec(
|
|||||||
|
|
||||||
fs::write(&out_name, pretty).into_diagnostic()?;
|
fs::write(&out_name, pretty).into_diagnostic()?;
|
||||||
}
|
}
|
||||||
return Ok(());
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|||||||
41
crates/cli/src/lib.rs
Normal file
41
crates/cli/src/lib.rs
Normal 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(())
|
||||||
|
}
|
||||||
@@ -1,7 +1,5 @@
|
|||||||
mod cmd;
|
use aiken::cmd::{build, check, new, tx, uplc};
|
||||||
|
|
||||||
use clap::Parser;
|
use clap::Parser;
|
||||||
use cmd::{build, check, new, tx, uplc};
|
|
||||||
|
|
||||||
/// Aiken: a smart-contract language and toolchain for Cardano
|
/// Aiken: a smart-contract language and toolchain for Cardano
|
||||||
#[derive(Parser)]
|
#[derive(Parser)]
|
||||||
|
|||||||
Reference in New Issue
Block a user