use crate::import_detector::{ImportStatement, resolve_import_path};
use crate::models::{Dependency, DependencyType};
use std::collections::HashMap;
use std::path::{Path, PathBuf};
#[derive(Debug, Clone)]
pub struct FeatureInfo {
pub name: String,
pub path: PathBuf,
}
pub fn build_file_to_feature_map(
features: &[FeatureInfo],
base_path: &Path,
) -> HashMap<PathBuf, String> {
let mut map = HashMap::new();
let mut sorted_features = features.to_vec();
sorted_features.sort_by(|a, b| {
b.path
.to_string_lossy()
.len()
.cmp(&a.path.to_string_lossy().len())
});
for feature in sorted_features {
let feature_path = base_path.join(&feature.path);
if std::fs::read_dir(&feature_path).is_ok() {
map_directory_files(&feature_path, &feature.path.to_string_lossy(), &mut map);
}
}
map
}
fn map_directory_files(dir: &Path, feature_path: &str, map: &mut HashMap<PathBuf, String>) {
if let Ok(entries) = std::fs::read_dir(dir) {
for entry in entries.flatten() {
let path = entry.path();
if path.is_file() {
let canonical_path = if let Ok(canonical) = path.canonicalize() {
canonical
} else {
path.clone()
};
map.entry(canonical_path)
.or_insert_with(|| feature_path.to_string());
} else if path.is_dir()
&& let Some(dir_name) = path.file_name().and_then(|n| n.to_str())
&& !should_skip_directory(dir_name)
&& !is_nested_feature_directory(&path)
{
map_directory_files(&path, feature_path, map);
}
}
}
}
fn is_nested_feature_directory(dir: &Path) -> bool {
if dir.join("features.toml").exists() {
return true;
}
if let Some(parent) = dir.parent()
&& let Some(parent_name) = parent.file_name()
&& parent_name == "features"
{
return true;
}
false
}
fn should_skip_directory(dir_name: &str) -> bool {
matches!(
dir_name,
"node_modules" | "target" | "dist" | "build" | ".git" | "__pycache__" | "coverage"
)
}
pub fn determine_dependency_type(
source_feature_path: &Path,
target_feature_path: &Path,
) -> DependencyType {
if target_feature_path.starts_with(source_feature_path) {
return DependencyType::Child;
}
if source_feature_path.starts_with(target_feature_path) {
return DependencyType::Parent;
}
DependencyType::Sibling
}
pub fn resolve_feature_dependencies(
_feature_name: &str,
feature_path: &Path,
base_path: &Path,
imports: &[ImportStatement],
file_to_feature_map: &HashMap<PathBuf, String>,
feature_path_to_name_map: &HashMap<String, String>,
file_map: &HashMap<String, PathBuf>,
) -> Vec<Dependency> {
let mut dependencies = Vec::new();
let mut seen = std::collections::HashSet::new();
for import in imports {
let source_file = Path::new(&import.file_path);
if let Some(resolved_path) =
resolve_import_path(&import.imported_path, source_file, base_path, file_map)
{
if let Some(target_feature_path_str) = file_to_feature_map.get(&resolved_path) {
if target_feature_path_str == feature_path.to_string_lossy().as_ref() {
continue;
}
let dep_key = format!(
"{}:{}:{}",
resolved_path.display(),
import.line_number,
target_feature_path_str
);
if seen.contains(&dep_key) {
continue;
}
seen.insert(dep_key);
if feature_path_to_name_map
.get(target_feature_path_str)
.is_some()
{
let target_path = PathBuf::from(target_feature_path_str);
let full_target_path = base_path.join(&target_path);
let full_source_path = base_path.join(feature_path);
let dependency_type =
determine_dependency_type(&full_source_path, &full_target_path);
let relative_source_filename = if let Ok(canonical_base) =
base_path.canonicalize()
{
let source_path = Path::new(&import.file_path);
if let Ok(canonical_source) = source_path.canonicalize() {
if let Ok(rel_path) = canonical_source.strip_prefix(&canonical_base) {
rel_path.to_string_lossy().to_string()
} else {
import.file_path.clone()
}
} else {
import.file_path.clone()
}
} else {
import.file_path.clone()
};
let relative_target_filename =
if let Ok(canonical_base) = base_path.canonicalize() {
if let Ok(rel_path) = resolved_path.strip_prefix(&canonical_base) {
rel_path.to_string_lossy().to_string()
} else {
resolved_path.to_string_lossy().to_string()
}
} else {
resolved_path.to_string_lossy().to_string()
};
dependencies.push(Dependency {
source_filename: relative_source_filename,
target_filename: relative_target_filename,
line: import.line_number,
content: import.line_content.clone(),
feature_path: target_feature_path_str.to_string(),
dependency_type,
});
}
}
}
}
dependencies
}
pub fn collect_feature_info(
features: &[crate::models::Feature],
_parent_path: Option<&Path>,
result: &mut Vec<FeatureInfo>,
) {
for feature in features {
let feature_path = PathBuf::from(&feature.path);
result.push(FeatureInfo {
name: feature.name.clone(),
path: feature_path.clone(),
});
if !feature.features.is_empty() {
collect_feature_info(&feature.features, None, result);
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_determine_dependency_type_child() {
let source = Path::new("/project/features/parent");
let target = Path::new("/project/features/parent/child");
assert!(matches!(
determine_dependency_type(source, target),
DependencyType::Child
));
}
#[test]
fn test_determine_dependency_type_parent() {
let source = Path::new("/project/features/parent/child");
let target = Path::new("/project/features/parent");
assert!(matches!(
determine_dependency_type(source, target),
DependencyType::Parent
));
}
#[test]
fn test_determine_dependency_type_sibling() {
let source = Path::new("/project/features/feature-a");
let target = Path::new("/project/features/feature-b");
assert!(matches!(
determine_dependency_type(source, target),
DependencyType::Sibling
));
}
}