use std::path::{Path, PathBuf};
use anyhow::{Context, Result};
use walkdir::WalkDir;
pub struct SourceDiscovery;
impl SourceDiscovery {
pub fn discover_java_sources(source_root: &Path) -> Result<Vec<PathBuf>> {
let mut sources = Vec::new();
if !source_root.exists() {
return Ok(sources);
}
for entry in WalkDir::new(source_root)
.into_iter()
.filter_map(|e| e.ok())
{
let path = entry.path();
if path.is_file() && path.extension().is_some_and(|ext| ext == "java") {
sources.push(path.to_path_buf());
}
}
Ok(sources)
}
pub fn discover_from_roots(source_roots: &[PathBuf]) -> Result<Vec<PathBuf>> {
let mut all_sources = Vec::new();
for root in source_roots {
let sources = Self::discover_java_sources(root)
.with_context(|| format!("Failed to discover sources in {root:?}"))?;
all_sources.extend(sources);
}
Ok(all_sources)
}
pub fn get_source_roots(build: &crate::model::build::Build) -> Vec<PathBuf> {
let mut roots = Vec::new();
if let Some(source_dir) = &build.source_directory {
roots.push(PathBuf::from(source_dir));
} else {
roots.push(PathBuf::from("src/main/java"));
}
roots
}
pub fn get_test_source_roots(build: &crate::model::build::Build) -> Vec<PathBuf> {
let mut roots = Vec::new();
if let Some(test_source_dir) = &build.test_source_directory {
roots.push(PathBuf::from(test_source_dir));
} else {
roots.push(PathBuf::from("src/test/java"));
}
roots
}
}