use std::path::Path;
use anyhow::Result;
use crate::config::{CheckerConfigWithCommand, SimpleCheckerParams};
use crate::file_index::FileIndex;
use crate::graph::{BuildGraph, Product};
use crate::processors::{ProductDiscovery, run_checker, execute_checker_batch,
discover_checker_products, config_file_inputs};
pub struct SimpleChecker {
config: CheckerConfigWithCommand,
params: SimpleCheckerParams,
}
impl SimpleChecker {
pub fn new(config: CheckerConfigWithCommand, params: SimpleCheckerParams) -> Self {
Self { config, params }
}
fn check_files(&self, files: &[&Path]) -> Result<()> {
let tool = &self.config.command;
if self.params.prepend_args.is_empty() {
run_checker(tool, self.params.subcommand, &self.config.args, files)
} else {
let mut combined_args: Vec<String> = self.params.prepend_args.iter().map(|s| s.to_string()).collect();
combined_args.extend_from_slice(&self.config.args);
run_checker(tool, self.params.subcommand, &combined_args, files)
}
}
}
impl ProductDiscovery for SimpleChecker {
fn scan_config(&self) -> &crate::config::ScanConfig {
&self.config.scan
}
fn standard_config(&self) -> Option<&crate::config::StandardConfig> {
Some(&self.config)
}
fn description(&self) -> &str {
self.params.description
}
fn auto_detect(&self, file_index: &FileIndex) -> bool {
!file_index.scan(&self.config.scan, true).is_empty()
}
fn required_tools(&self) -> Vec<String> {
let mut tools = vec![self.config.command.clone()];
for t in self.params.extra_tools {
tools.push(t.to_string());
}
tools
}
fn discover(
&self,
graph: &mut BuildGraph,
file_index: &FileIndex,
instance_name: &str,
) -> Result<()> {
let mut dep_inputs = self.config.dep_inputs.clone();
for ai in &self.config.dep_auto {
dep_inputs.extend(config_file_inputs(ai));
}
discover_checker_products(
graph, &self.config.scan, file_index, &dep_inputs, &self.config, instance_name,
)
}
fn execute(&self, product: &Product) -> Result<()> {
self.check_files(&[product.primary_input()])
}
fn supports_batch(&self) -> bool {
self.config.batch
}
fn execute_batch(&self, products: &[&Product]) -> Vec<Result<()>> {
execute_checker_batch(products, |files| self.check_files(files))
}
}