mod cpp;
mod python;
pub use cpp::CppDepAnalyzer;
pub use python::PythonDepAnalyzer;
use anyhow::Result;
use std::collections::HashSet;
use std::path::{Path, PathBuf};
use crate::deps_cache::DepsCache;
use crate::file_index::FileIndex;
use crate::graph::BuildGraph;
use crate::progress;
pub trait DepAnalyzer: Sync + Send {
fn description(&self) -> &str;
fn auto_detect(&self, file_index: &FileIndex) -> bool;
fn analyze(&self, graph: &mut BuildGraph, deps_cache: &mut DepsCache, file_index: &FileIndex, verbose: bool) -> Result<()>;
}
pub fn analyze_with_scanner<F, G>(
graph: &mut BuildGraph,
deps_cache: &mut DepsCache,
analyzer_name: &str,
match_product: F,
scan_deps: G,
verbose: bool,
) -> Result<()>
where
F: Fn(&crate::graph::Product) -> Option<PathBuf>,
G: Fn(&Path) -> Result<Vec<PathBuf>>,
{
let products: Vec<(usize, PathBuf)> = graph.products()
.iter()
.filter_map(|p| {
match_product(p).map(|source| (p.id, source))
})
.collect();
if products.is_empty() {
return Ok(());
}
let pb = progress::create_bar(
products.len() as u64,
verbose || crate::json_output::is_json_mode(),
);
for (id, source) in &products {
pb.set_message(format!("[{}] {}", analyzer_name, source.display()));
let deps = if let Some(cached) = deps_cache.get(source) {
cached
} else {
let scanned = scan_deps(source)?;
if let Err(e) = deps_cache.set(source, &scanned, analyzer_name) {
eprintln!("Warning: failed to cache dependencies for {}: {}", source.display(), e);
}
scanned
};
if !deps.is_empty()
&& let Some(product) = graph.get_product_mut(*id) {
let existing: HashSet<&PathBuf> = product.inputs.iter().collect();
let new_deps: Vec<PathBuf> = deps.into_iter()
.filter(|dep| !existing.contains(dep))
.collect();
product.inputs.extend(new_deps);
}
pb.inc(1);
}
pb.finish_and_clear();
if verbose {
let stats = deps_cache.stats();
if stats.hits > 0 || stats.misses > 0 {
eprintln!("[{}] Dependency cache: {} hits, {} recalculated",
analyzer_name, stats.hits, stats.misses);
}
}
Ok(())
}