|
| 1 | +mod cmd_args; |
| 2 | +mod init; |
| 3 | +mod output; |
| 4 | +mod terminal_display; |
| 5 | + |
| 6 | +use cmd_args::CmdArgs; |
| 7 | +use fern::Dispatch; |
| 8 | +use log::LevelFilter; |
| 9 | +use output::output_result; |
| 10 | +use std::{error::Error, sync::Arc}; |
| 11 | +use tokio_util::sync::CancellationToken; |
| 12 | + |
| 13 | +use crate::init::get_need_check_ids; |
| 14 | + |
| 15 | +#[allow(unused)] |
| 16 | +async fn run_check(cmd_args: CmdArgs) -> Result<(), Box<dyn Error + Sync + Send>> { |
| 17 | + let mut workspace = cmd_args.workspace; |
| 18 | + if !workspace.is_absolute() { |
| 19 | + workspace = std::env::current_dir()?.join(workspace); |
| 20 | + } |
| 21 | + |
| 22 | + let verbose = cmd_args.verbose; |
| 23 | + let logger = Dispatch::new() |
| 24 | + .format(move |out, message, record| { |
| 25 | + let (color, reset) = match record.level() { |
| 26 | + log::Level::Error => ("\x1b[31m", "\x1b[0m"), // Red |
| 27 | + log::Level::Warn => ("\x1b[33m", "\x1b[0m"), // Yellow |
| 28 | + log::Level::Info | log::Level::Debug | log::Level::Trace => ("", ""), |
| 29 | + }; |
| 30 | + out.finish(format_args!( |
| 31 | + "{}{}: {}{}", |
| 32 | + color, |
| 33 | + record.level(), |
| 34 | + if verbose { |
| 35 | + format!("({}) {}", record.target(), message) |
| 36 | + } else { |
| 37 | + message.to_string() |
| 38 | + }, |
| 39 | + reset |
| 40 | + )) |
| 41 | + }) |
| 42 | + .level(if verbose { |
| 43 | + LevelFilter::Info |
| 44 | + } else { |
| 45 | + LevelFilter::Warn |
| 46 | + }) |
| 47 | + .chain(std::io::stderr()); |
| 48 | + |
| 49 | + if let Err(e) = logger.apply() { |
| 50 | + eprintln!("Failed to apply logger: {:?}", e); |
| 51 | + } |
| 52 | + |
| 53 | + let analysis = match init::load_workspace(workspace.clone(), cmd_args.config, cmd_args.ignore) { |
| 54 | + Some(analysis) => analysis, |
| 55 | + None => { |
| 56 | + return Err("Failed to load workspace".into()); |
| 57 | + } |
| 58 | + }; |
| 59 | + |
| 60 | + let files = analysis.compilation.get_db().get_vfs().get_all_file_ids(); |
| 61 | + let db = analysis.compilation.get_db(); |
| 62 | + let need_check_files = get_need_check_ids(db, files, &workspace); |
| 63 | + |
| 64 | + let (sender, receiver) = tokio::sync::mpsc::channel(100); |
| 65 | + let analysis = Arc::new(analysis); |
| 66 | + let db = analysis.compilation.get_db(); |
| 67 | + for file_id in need_check_files.clone() { |
| 68 | + let sender = sender.clone(); |
| 69 | + let analysis = analysis.clone(); |
| 70 | + tokio::spawn(async move { |
| 71 | + let cancel_token = CancellationToken::new(); |
| 72 | + let diagnostics = analysis.diagnose_file(file_id, cancel_token); |
| 73 | + sender.send((file_id, diagnostics)).await.unwrap(); |
| 74 | + }); |
| 75 | + } |
| 76 | + |
| 77 | + let exit_code = output_result( |
| 78 | + need_check_files.len(), |
| 79 | + db, |
| 80 | + workspace, |
| 81 | + receiver, |
| 82 | + cmd_args.output_format, |
| 83 | + cmd_args.output, |
| 84 | + cmd_args.warnings_as_errors, |
| 85 | + ) |
| 86 | + .await; |
| 87 | + |
| 88 | + if exit_code != 0 { |
| 89 | + return Err(format!("exit code: {}", exit_code).into()); |
| 90 | + } |
| 91 | + |
| 92 | + eprintln!("Check finished"); |
| 93 | + Ok(()) |
| 94 | +} |
0 commit comments