cargo_autodd/
lib.rs

1pub mod dependency_manager;
2pub mod models;
3pub mod utils;
4
5use std::path::PathBuf;
6
7use anyhow::Result;
8
9pub struct CargoAutodd {
10    analyzer: dependency_manager::DependencyAnalyzer,
11    updater: dependency_manager::DependencyUpdater,
12    reporter: dependency_manager::DependencyReporter,
13    debug: bool,
14}
15
16impl CargoAutodd {
17    pub fn new(project_root: PathBuf) -> Self {
18        Self {
19            analyzer: dependency_manager::DependencyAnalyzer::new(project_root.clone()),
20            updater: dependency_manager::DependencyUpdater::new(project_root.clone()),
21            reporter: dependency_manager::DependencyReporter::new(project_root),
22            debug: false,
23        }
24    }
25
26    pub fn with_debug(project_root: PathBuf, debug: bool) -> Self {
27        Self {
28            analyzer: dependency_manager::DependencyAnalyzer::with_debug(
29                project_root.clone(),
30                debug,
31            ),
32            updater: dependency_manager::DependencyUpdater::new(project_root.clone()),
33            reporter: dependency_manager::DependencyReporter::new(project_root),
34            debug,
35        }
36    }
37
38    pub fn analyze_and_update(&self) -> Result<()> {
39        if self.debug {
40            println!("šŸ” Starting dependency analysis in debug mode...");
41        }
42        println!("šŸ” Analyzing project dependencies...");
43        let crate_refs = self.analyzer.analyze_dependencies()?;
44
45        if self.debug {
46            println!("\nšŸ“ Updating Cargo.toml with found dependencies...");
47        }
48        println!("šŸ“ Updating Cargo.toml...");
49        self.updater.update_cargo_toml(&crate_refs)?;
50
51        println!("āœ… Dependencies updated successfully!");
52        Ok(())
53    }
54
55    pub fn update_dependencies(&self) -> Result<()> {
56        println!("šŸ” Checking for dependency updates...");
57        let crate_refs = self.analyzer.analyze_dependencies()?;
58        self.updater.update_cargo_toml(&crate_refs)?;
59        println!("\nšŸ” Verifying dependencies...");
60        self.updater.verify_dependencies()?;
61        println!("āœ… Dependencies updated successfully!");
62        Ok(())
63    }
64
65    pub fn generate_report(&self) -> Result<()> {
66        println!("šŸ“Š Analyzing dependency usage...");
67        let crate_refs = self.analyzer.analyze_dependencies()?;
68        self.reporter.generate_dependency_report(&crate_refs)
69    }
70
71    pub fn check_security(&self) -> Result<()> {
72        println!("šŸ”’ Running security check...");
73        self.reporter.generate_security_report()
74    }
75}
76
77#[cfg(test)]
78mod tests {
79    use super::*;
80    use std::fs::File;
81    use std::io::Write;
82    use tempfile::TempDir;
83
84    fn create_test_environment() -> Result<TempDir> {
85        let temp_dir = TempDir::new()?;
86
87        // Create Cargo.toml
88        let cargo_toml = temp_dir.path().join("Cargo.toml");
89        let content = r#"
90[package]
91name = "test-package"
92version = "0.1.0"
93edition = "2021"
94
95[dependencies]
96serde = "1.0"
97"#;
98        let mut file = File::create(&cargo_toml)?;
99        writeln!(file, "{}", content)?;
100
101        // Create src directory and main.rs
102        std::fs::create_dir(temp_dir.path().join("src"))?;
103        let main_rs = temp_dir.path().join("src/main.rs");
104        let content = r#"
105use serde;
106use tokio;
107"#;
108        let mut file = File::create(main_rs)?;
109        writeln!(file, "{}", content)?;
110
111        Ok(temp_dir)
112    }
113
114    #[test]
115    fn test_analyze_and_update() -> Result<()> {
116        let temp_dir = create_test_environment()?;
117        let autodd = CargoAutodd::new(temp_dir.path().to_path_buf());
118        autodd.analyze_and_update()?;
119        Ok(())
120    }
121
122    #[test]
123    fn test_generate_report() -> Result<()> {
124        let temp_dir = create_test_environment()?;
125        let autodd = CargoAutodd::new(temp_dir.path().to_path_buf());
126        autodd.generate_report()?;
127        Ok(())
128    }
129
130    #[test]
131    fn test_check_security() -> Result<()> {
132        let temp_dir = create_test_environment()?;
133        let autodd = CargoAutodd::new(temp_dir.path().to_path_buf());
134        autodd.check_security()?;
135        Ok(())
136    }
137}