fe/task/
check.rs

1use std::path::Path;
2
3use clap::Args;
4use fe_common::{
5    diagnostics::{print_diagnostics, Diagnostic},
6    utils::files::get_project_root,
7    utils::files::BuildFiles,
8};
9use fe_driver::Db;
10
11#[derive(Args)]
12#[clap(about = "Analyze the current project and report errors, but don't build artifacts")]
13pub struct CheckArgs {
14    #[clap(default_value_t = get_project_root().unwrap_or(".".to_string()))]
15    input_path: String,
16}
17
18fn check_single_file(db: &mut Db, input_path: &str) -> Vec<Diagnostic> {
19    let content = match std::fs::read_to_string(input_path) {
20        Err(err) => {
21            eprintln!("Failed to load file: `{}`. Error: {}", &input_path, err);
22            std::process::exit(1)
23        }
24        Ok(content) => content,
25    };
26
27    fe_driver::check_single_file(db, input_path, &content)
28}
29
30fn check_ingot(db: &mut Db, input_path: &str) -> Vec<Diagnostic> {
31    let build_files = match BuildFiles::load_fs(input_path) {
32        Ok(files) => files,
33        Err(err) => {
34            eprintln!("Failed to load project files.\nError: {err}");
35            std::process::exit(1)
36        }
37    };
38
39    fe_driver::check_ingot(db, &build_files)
40}
41
42pub fn check(args: CheckArgs) {
43    let mut db = fe_driver::Db::default();
44    let input_path = args.input_path;
45
46    // check project
47    let diags = if Path::new(&input_path).is_file() {
48        check_single_file(&mut db, &input_path)
49    } else {
50        check_ingot(&mut db, &input_path)
51    };
52
53    if !diags.is_empty() {
54        print_diagnostics(&db, &diags);
55        std::process::exit(1);
56    }
57
58    eprintln!("Finished");
59}