use anyhow::Context;
use blockwatch::blocks;
use blockwatch::blocks::BlockSeverity;
use blockwatch::diff_parser;
use blockwatch::flags;
use blockwatch::language_parsers;
use blockwatch::validators;
use blockwatch::fs::FileSystem;
use blockwatch::validators::Violation;
use clap::Parser;
use globset::GlobSet;
use std::collections::HashMap;
use std::io::{IsTerminal, Read, Write};
use std::path::PathBuf;
use std::sync::Arc;
use std::{env, fs, process};
fn main() -> anyhow::Result<()> {
let args = flags::Args::parse();
match &args.command {
Some(flags::SubCommand::List { diff, .. }) => run_list(&args, *diff),
None => run_validators(&args),
}
}
fn run_list(args: &flags::Args, read_diff_flag: bool) -> anyhow::Result<()> {
let read_diff = read_diff_flag && !stdin_is_terminal();
let file_system = blockwatch::fs::FileSystemImpl::new(repository_root()?);
let context = build_context(args, read_diff, &file_system)?;
let report = context.to_serializable_report();
serde_json::to_writer_pretty(std::io::stdout(), &report).context("Failed to list blocks")
}
fn run_validators(args: &flags::Args) -> anyhow::Result<()> {
let file_system = Arc::new(blockwatch::fs::FileSystemImpl::new(repository_root()?));
let context = build_context(args, !stdin_is_terminal(), file_system.as_ref())?;
let (sync_validators, async_validators) = validators::detect_validators(
&context,
&validators::detector_factories::<blockwatch::fs::FileSystemImpl>(),
&args.disabled_validators(),
&args.enabled_validators(),
&file_system,
)?;
let violations = validators::run(Arc::new(context), sync_validators, async_validators)?;
if !violations.is_empty() {
process_violations(violations)?;
}
Ok(())
}
fn build_context(
args: &flags::Args,
read_diff: bool,
file_system: &impl FileSystem,
) -> anyhow::Result<validators::ValidationContext> {
let language_parsers = language_parsers::language_parsers()?;
let supported_extensions = language_parsers.keys().collect();
args.validate(&supported_extensions)?;
let modified_lines_by_file = if read_diff {
read_diff_from_stdin()?
} else {
HashMap::new()
};
let mut glob_set = args.globs()?;
if glob_set.is_empty() && !read_diff {
glob_set = GlobSet::new([globset::Glob::new("**")?])?;
}
let should_scan_files = !glob_set.is_empty();
let path_checker = blockwatch::fs::PathCheckerImpl::new(glob_set, args.ignored_globs()?);
let blocks = blocks::parse_blocks(
modified_lines_by_file,
should_scan_files,
file_system,
&path_checker,
&language_parsers,
args.extensions(),
)?;
Ok(validators::ValidationContext::new(blocks, language_parsers))
}
fn stdin_is_terminal() -> bool {
std::io::stdin().is_terminal()
}
fn read_diff_from_stdin() -> anyhow::Result<HashMap<PathBuf, Vec<diff_parser::LineChange>>> {
let mut diff = String::new();
std::io::stdin().read_to_string(&mut diff)?;
diff_parser::line_changes_from_diff(&diff)
}
fn process_violations(violations: HashMap<PathBuf, Vec<Violation>>) -> anyhow::Result<()> {
let mut has_error_severity = false;
let mut diagnostics: HashMap<PathBuf, Vec<serde_json::Value>> =
HashMap::with_capacity(violations.len());
for (file_path, file_violations) in violations {
let mut file_diagnostics = Vec::with_capacity(file_violations.len());
for violation in file_violations {
let diagnostic = violation.as_simple_diagnostic();
if diagnostic.severity() == BlockSeverity::Error {
has_error_severity = true;
}
file_diagnostics.push(serde_json::to_value(diagnostic)?);
}
diagnostics.insert(file_path, file_diagnostics);
}
let mut stderr = std::io::stderr().lock();
serde_json::to_writer_pretty(&mut stderr, &diagnostics)?;
writeln!(&mut stderr)?;
if has_error_severity {
process::exit(1);
}
Ok(())
}
fn repository_root_path(current_path: PathBuf) -> anyhow::Result<PathBuf> {
current_path
.ancestors()
.find(|path| path.join(".git").is_dir() || path.join(".hg").is_dir())
.map(|path| path.to_path_buf())
.ok_or_else(|| anyhow::anyhow!("Could not find the repository root directory"))
}
fn repository_root() -> anyhow::Result<PathBuf> {
repository_root_path(fs::canonicalize(env::current_dir()?)?)
}