run method

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

Runs this command.

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

Implementation

@override
Future<int> run() async {
  // 1. Verify environment
  if (!await _checkCommand('flutter') || !await _checkCommand('git')) {
    logger.err('Flutter and Git must be installed.');
    return ExitCode.software.code;
  }

  final String? projectName = argResults!.rest.isNotEmpty
      ? argResults!.rest.first
      : null;
  final String routerType = argResults!['router'] as String;
  final String storageType = argResults!['storage'] as String;
  final bool genFirebaseOptions = argResults!['firebaseop'] as bool;

  logger.info('Using router: $routerType');
  logger.info('Using storage: $storageType');
  logger.info('Generate firebase_options.dart: $genFirebaseOptions');

  // Determine base path - convert to absolute path for new projects
  String basePath;
  String importPrefix;

  if (projectName != null) {
    logger.info('Creating new Flutter project: $projectName...');
    final createResult = await Process.run('flutter', [
      'create',
      projectName,
    ]);
    if (createResult.exitCode != 0) {
      logger.err(createResult.stderr.toString());
      return createResult.exitCode;
    }
    logger.success('Flutter project created.');

    // Use absolute path for the new project
    basePath = Directory(projectName).absolute.path;
    importPrefix = projectName;
  } else {
    logger.info('Initializing in current directory...');
    basePath = Directory.current.path;
    importPrefix = await _getPackageNameFromPubspec(basePath);
  }

  logger.detail('Working directory: $basePath');

  // 2. Add dependencies
  await _addDependencies(basePath, routerType, storageType);

  // 3. Create Firebase Config (Placeholders and options needed by Flavorizr)
  await _createFirebaseConfig(basePath, importPrefix, genFirebaseOptions);

  // 4. Setup and Run Flavorizr FIRST to avoid overwriting our architecture files
  await _setupFlavorizrConfig(basePath);
  await _runFlavorizr(basePath, genFirebaseOptions);

  // 5. Setup Android Build Config
  await _setupAndroidBuild(basePath);

  // 6. Create Core Module structure (includes routes, theme, and bindings)
  _createCoreStructure(basePath, importPrefix, routerType, storageType);

  // 7. Create App Entry Point (app.dart and main.dart)
  _createAppEntryPoint(basePath, importPrefix, routerType);

  // 10. Setup Flutter Launcher Icons
  await _setupFlutterLauncherIconsConfig(basePath);

  // 11. Run Lang Command to generate language constants
  logger.info('Running lang command to finalize localization...');
  // We need to change the current directory to the project path for LangCommand to work correctly
  final originalDir = Directory.current;
  Directory.current = basePath;
  try {
    await LangCommand(logger: logger).run();
  } finally {
    Directory.current = originalDir;
  }

  logger.success('Project initialized successfully!');
  return ExitCode.success.code;
}