analyze method

Implementation

ProjectAnalysisResult analyze() {
  final pubspecFile = File(p.join(projectRoot, 'pubspec.yaml'));
  if (!pubspecFile.existsSync()) {
    throw Exception('❌ No pubspec.yaml found.\nPlease run flutter_testmap from a Dart/Flutter project.');
  }

  final pubspecContent = pubspecFile.readAsStringSync();
  final YamlMap pubspecYaml = loadYaml(pubspecContent) as YamlMap;
  final packageName = pubspecYaml['name'] as String? ?? 'unknown_package';
  final isFlutterProject = pubspecYaml.containsKey('flutter') ||
      (pubspecYaml['dependencies'] is YamlMap &&
          (pubspecYaml['dependencies'] as YamlMap).containsKey('flutter'));

  final importAnalyzer = ImportAnalyzer(packageName: packageName);
  final fileAnalyzer = DartFileAnalyzer(importAnalyzer: importAnalyzer);
  final testAnalyzer = TestAnalyzer(fileAnalyzer: fileAnalyzer);

  final List<String> warnings = [];
  final List<SourceFile> sourceFiles = [];

  final libDir = Directory(p.join(projectRoot, 'lib'));
  if (libDir.existsSync()) {
    final entities = libDir.listSync(recursive: true);
    for (final entity in entities) {
      if (entity is File && entity.path.endsWith('.dart')) {
        final relativePath = p.relative(entity.path, from: projectRoot).replaceAll('\\', '/');

        if (relativePath.endsWith('.g.dart') || relativePath.endsWith('.freezed.dart')) {
          continue;
        }

        try {
          final content = entity.readAsStringSync();
          final cached = cache?.get(relativePath, content);
          if (cached != null) {
            sourceFiles.add(cached);
          } else {
            final result = fileAnalyzer.analyzeFile(
              relativePath: relativePath,
              content: content,
            );
            warnings.addAll(result.parseWarnings);
            sourceFiles.add(result.sourceFile);
            cache?.put(relativePath, result.sourceFile);
          }
        } catch (e) {
          warnings.add('⚠ Could not fully analyze: ' + relativePath + '\nReason: ' + e.toString() + '\nContinuing with partial analysis...');
        }
      }
    }
  }

  final testFiles = testAnalyzer.discoverAndAnalyzeTests(projectRoot);
  for (final testFile in testFiles) {
    cache?.put(testFile.path, testFile);
  }

  final allFiles = [...sourceFiles, ...testFiles];
  final graph = DependencyGraphBuilder.buildGraph(allFiles);

  return ProjectAnalysisResult(
    graph: graph,
    packageName: packageName,
    isFlutterProject: isFlutterProject,
    sourceFiles: sourceFiles,
    testFiles: testFiles,
    warnings: warnings,
  );
}