execute method

Future<int> execute()

Executes the generation logic and returns the exit code. Does NOT call exit().

Implementation

Future<int> execute() async {
  final projectDir = findNitroProjectRoot();
  if (projectDir == null) {
    stderr.writeln(red('❌ No Nitro project found in . or its subdirectories (must have nitro dependency in pubspec.yaml).'));
    return 1;
  }

  // If we're not in the project root, let the user know we've found it
  if (projectDir.path != Directory.current.path) {
    stdout.writeln(gray('  📂 Found project in: ${projectDir.path}'));
  }

  stdout.writeln('');
  stdout.writeln(boldCyan('  ╔══════════════════════════╗'));
  stdout.writeln(boldCyan('  ║  nitrogen generate       ║'));
  stdout.writeln(boldCyan('  ╚══════════════════════════╝'));
  stdout.writeln('');

  // ── pub get ─────────────────────────────────────────────────────────────
  stdout.writeln(cyan('  › flutter pub get …'));
  var exitCode = await runStreaming('flutter', ['pub', 'get'], workingDirectory: projectDir.path);
  if (exitCode != 0) {
    stderr.writeln(red('  ✘  flutter pub get failed (exit $exitCode)'));
    return exitCode;
  }
  stdout.writeln('');

  // ── build_runner ─────────────────────────────────────────────────────────
  stdout.writeln(cyan('  › build_runner build …'));
  stdout.writeln('');
  exitCode = await runStreaming(
    'flutter',
    [
      'pub',
      'run',
      'build_runner',
      'build',
      '--delete-conflicting-outputs',
    ],
    workingDirectory: projectDir.path,
  );

  stdout.writeln('');
  if (exitCode != 0) {
    stderr.writeln(boldRed('  ✘  build_runner failed (exit $exitCode)'));
    stderr.writeln(gray('     Check the output above for details.'));
    return exitCode;
  }

  // ── Sync generated Swift bridges to ios/Classes/ ─────────────────────────
  _syncSwiftToIosClasses(projectDir.path);

  // ── pod install ──────────────────────────────────────────────────────────
  final podfileDirs = _findPodfileDirs(projectDir.path);
  for (final dir in podfileDirs) {
    stdout.writeln(cyan('  › pod install (${p.relative(dir, from: projectDir.path)}) …'));
    final podExitCode = await runStreaming(
      'pod',
      ['install'],
      workingDirectory: dir,
    );
    if (podExitCode != 0) {
      stderr.writeln(red('  ⚠  pod install failed in $dir (exit $podExitCode) — continuing'));
    }
  }

  stdout.writeln('');
  stdout.writeln(boldGreen('  ✨ Generation complete!'));
  stdout.writeln(gray('     Run nitrogen link to wire bridges into the build system.'));
  stdout.writeln('');
  return 0;
}