构建基于petgraph的代码图(增量构建)
(&mut self, dir: &Path)
| 752 | |
| 753 | /// 构建基于petgraph的代码图(增量构建) |
| 754 | pub fn build_petgraph_code_graph(&mut self, dir: &Path) -> Result<PetCodeGraph, String> { |
| 755 | // 1. 尝试从本地数据库加载现有的图 |
| 756 | let mut code_graph = self._load_existing_graph(dir)?; |
| 757 | let has_existing_data = code_graph.is_some(); |
| 758 | |
| 759 | if let Some(ref mut existing_graph) = code_graph { |
| 760 | info!("Loaded existing graph with {} functions", existing_graph.get_stats().total_functions); |
| 761 | } else { |
| 762 | info!("No existing graph found, starting fresh analysis"); |
| 763 | code_graph = Some(PetCodeGraph::new()); |
| 764 | } |
| 765 | |
| 766 | let mut code_graph = code_graph.unwrap(); |
| 767 | |
| 768 | // 2. 扫描目录下的所有文件 |
| 769 | let files = self.scan_directory(dir); |
| 770 | info!("Found {} files to process", files.len()); |
| 771 | |
| 772 | // 3. 加载文件哈希值(如果存在) |
| 773 | let mut file_hashes = self._load_file_hashes(dir)?; |
| 774 | |
| 775 | // 4. 逐个处理文件,检查是否需要重新解析 |
| 776 | let mut processed_files = 0; |
| 777 | let mut skipped_files = 0; |
| 778 | |
| 779 | for file_path in files { |
| 780 | if self._should_skip_file(&file_path, &mut file_hashes)? { |
| 781 | skipped_files += 1; |
| 782 | continue; |
| 783 | } |
| 784 | |
| 785 | if let Err(e) = self.parse_file(&file_path) { |
| 786 | warn!("Failed to parse {}: {}", file_path.display(), e); |
| 787 | } else { |
| 788 | processed_files += 1; |
| 789 | } |
| 790 | } |
| 791 | |
| 792 | info!("File processing completed: {} processed, {} skipped", processed_files, skipped_files); |
| 793 | |
| 794 | // 5. 如果这是增量构建,需要合并新解析的函数 |
| 795 | if has_existing_data { |
| 796 | self._merge_new_functions(&mut code_graph); |
| 797 | } else { |
| 798 | // 全量构建:直接添加所有函数 |
| 799 | for (_file_path, functions) in &self.file_functions { |
| 800 | for function in functions { |
| 801 | code_graph.add_function(function.clone()); |
| 802 | } |
| 803 | } |
| 804 | } |
| 805 | |
| 806 | // 6. 分析调用关系 |
| 807 | self._analyze_petgraph_call_relations(&mut code_graph); |
| 808 | |
| 809 | // 7. 更新统计信息 |
| 810 | code_graph.update_stats(); |
| 811 |