forked from argotorg/fe
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcheck.rs
More file actions
67 lines (54 loc) · 1.8 KB
/
Copy pathcheck.rs
File metadata and controls
67 lines (54 loc) · 1.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
use std::path::Path;
use clap::Args;
use fe_common::diagnostics::{print_diagnostics, Diagnostic};
use fe_driver::Db;
use super::utils::load_files_from_dir;
const DEFAULT_INGOT_NAME: &str = "main";
#[derive(Args)]
#[clap(about = "Analyze the current project and report errors, but don't build artifacts")]
pub struct CheckArgs {
input_path: String,
}
fn check_single_file(db: &mut Db, input_path: &str) -> Vec<Diagnostic> {
let content = match std::fs::read_to_string(input_path) {
Err(err) => {
eprintln!("Failed to load file: `{}`. Error: {}", &input_path, err);
std::process::exit(1)
}
Ok(content) => content,
};
fe_driver::check_single_file(db, input_path, &content)
}
fn check_ingot(db: &mut Db, input_path: &str) -> Vec<Diagnostic> {
if !Path::new(input_path).exists() {
eprintln!("Input directory does not exist: `{}`.", input_path);
std::process::exit(1)
}
let files = match load_files_from_dir(input_path) {
Ok(files) if files.is_empty() => {
eprintln!("Input directory is not an ingot: `{}`", input_path);
std::process::exit(1)
}
Ok(files) => files,
Err(err) => {
eprintln!("Failed to load project files. Error: {}", err);
std::process::exit(1)
}
};
fe_driver::check_ingot(db, DEFAULT_INGOT_NAME, &files)
}
pub fn check(args: CheckArgs) {
let mut db = fe_driver::Db::default();
let input_path = args.input_path;
// check project
let diags = if Path::new(&input_path).is_file() {
check_single_file(&mut db, &input_path)
} else {
check_ingot(&mut db, &input_path)
};
if !diags.is_empty() {
print_diagnostics(&db, &diags);
std::process::exit(1);
}
eprintln!("Finished");
}