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 {
  final currentDir = Directory.current.path;
  final supportLangFile = File(
    p.join(currentDir, 'lib', 'core', 'constants', 'support_language.dart'),
  );
  final appConstantsFile = File(
    p.join(currentDir, 'lib', 'core', 'constants', 'app_constants.dart'),
  );
  final translationsDir = Directory(
    p.join(currentDir, 'lib', 'core', 'translations'),
  );

  if (!supportLangFile.existsSync()) {
    logger.err('Support language file not found at ${supportLangFile.path}');
    return ExitCode.noInput.code;
  }

  if (!appConstantsFile.existsSync()) {
    logger.err('App constants file not found at ${appConstantsFile.path}');
    return ExitCode.noInput.code;
  }

  final content = supportLangFile.readAsStringSync();
  final localeRegex = RegExp(r"Locale\('([a-z]{2})',?\s*'?([A-Z]{2})?'?\)");
  final matches = localeRegex.allMatches(content);

  if (matches.isEmpty) {
    logger.warn('No locales found in support_language.dart');
    return ExitCode.success.code;
  }

  final locales = matches
      .map((m) => {'code': m.group(1)!, 'country': m.group(2)})
      .toList();

  // 1. Update AppConstants
  _updateAppConstants(appConstantsFile, locales);

  // 2. Generate missing translation files
  _generateTranslationFiles(translationsDir, locales);

  // 3. Update app_translations.dart
  _updateAppTranslations(currentDir, locales);

  // 4. Update translations.dart (barrel)
  _updateTranslationsBarrel(currentDir, locales);

  logger.success('Successfully updated language configurations.');
  return ExitCode.success.code;
}