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
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(())
}