run method

  1. @override
Future<void> run()
override

Runs this command.

The return value is wrapped in a Future if necessary and returned by CommandRunner.runCommand.

Implementation

@override
Future<void> run() async {
  print('\nFlutterBlueprint Doctor\n');
  print('─────────────────────────');

  await _check('Flutter SDK', () async {
    final result = await Process.run('flutter', ['--version']);
    if (result.exitCode != 0) throw 'flutter not found';
    final output = result.stdout as String;
    final match = RegExp(r'Flutter (\S+)').firstMatch(output);
    return match?.group(1) ?? 'found';
  });

  await _check('Dart SDK', () async {
    final result = await Process.run('dart', ['--version']);
    if (result.exitCode != 0) throw 'dart not found';
    final output = (result.stdout as String).isNotEmpty
        ? result.stdout as String
        : result.stderr as String;
    final match = RegExp(r'Dart SDK version: (\S+)').firstMatch(output);
    return match?.group(1) ?? 'found';
  });

  await _check('Git', () async {
    final result = await Process.run('git', ['--version']);
    if (result.exitCode != 0) throw 'git not found';
    final match = RegExp(r'git version (\S+)').firstMatch(result.stdout as String);
    return match?.group(1) ?? 'found';
  });

  await _check('Engine binary (flutter_blueprint_engine)', () async {
    final binaryName = Platform.isWindows
        ? 'flutter_blueprint_engine.exe'
        : 'flutter_blueprint_engine';

    // Check ~/.flutter_blueprint/bin/
    final home = Platform.environment['HOME'] ??
        Platform.environment['USERPROFILE'] ?? '';
    final homeBin = '$home/.flutter_blueprint/bin/$binaryName';
    if (File(homeBin).existsSync()) return homeBin;

    // Check PATH
    final which = await Process.run(
        Platform.isWindows ? 'where' : 'which', [binaryName]);
    if (which.exitCode == 0) {
      return (which.stdout as String).trim().split('\n').first.trim();
    }

    throw 'Not found. Run `flutter_blueprint upgrade` to install.';
  });

  if (Platform.isMacOS) {
    await _check('Xcode (optional)', () async {
      final result = await Process.run('xcodebuild', ['-version']);
      if (result.exitCode != 0) throw 'not found (install from App Store)';
      final match = RegExp(r'Xcode (\S+)').firstMatch(result.stdout as String);
      return match?.group(1) ?? 'found';
    }, required: false);
  }

  await _check('Android SDK (optional)', () async {
    final sdkRoot = Platform.environment['ANDROID_HOME'] ??
        Platform.environment['ANDROID_SDK_ROOT'];
    if (sdkRoot == null || !Directory(sdkRoot).existsSync()) {
      throw 'ANDROID_HOME not set';
    }
    return sdkRoot;
  }, required: false);

  print('');
}