detect method

Future<ProjectContext> detect(
  1. String directory,
  2. AppConfig config
)

Implementation

Future<ProjectContext> detect(String directory, AppConfig config) async {
  logger.startSpinner('Detecting project environment...');

  final pubspecFile = File(p.join(directory, 'pubspec.yaml'));
  if (!await pubspecFile.exists()) {
    logger.stopSpinner();
    throw ProjectDoctorException(
      'No pubspec.yaml found in $directory. Are you in a Flutter project?',
    );
  }

  Map<String, dynamic>? pubspecMap;
  bool isFlutterProject = false;

  try {
    final content = await pubspecFile.readAsString();
    final yaml = loadYaml(content);
    if (yaml is YamlMap) {
      pubspecMap = _convertYamlMap(yaml);
      final dependencies = pubspecMap['dependencies'];
      if (dependencies is Map && dependencies.containsKey('flutter')) {
        isFlutterProject = true;
      }
    }
  } catch (e) {
    logger.stopSpinner();
    throw ProjectDoctorException('Failed to parse pubspec.yaml: $e');
  }

  if (!isFlutterProject) {
    logger.stopSpinner();
    throw ProjectDoctorException(
      'Not a Flutter project (missing flutter dependency in pubspec.yaml).',
    );
  }

  // Detect SDKs
  final flutterVersion = await _getCommandOutput('flutter', ['--version']);
  final dartVersion = await _getCommandOutput('dart', ['--version']);
  final hasGit = await _checkGit(directory);

  logger.stopSpinner();
  logger.success('Project detected as a Flutter project.');

  return ProjectContext(
    directory: directory,
    config: config,
    logger: logger,
    isFlutterProject: isFlutterProject,
    flutterVersion: flutterVersion,
    dartVersion: dartVersion,
    hasGit: hasGit,
    pubspec: pubspecMap,
  );
}