doRun method

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

Implementation

@override
Future<int> doRun() async {
  final config = findConfigAndSetWorkingDir();
  final l10nConfig = config.l10n;
  final args = argResults!;
  final locale = args.getLocale(_argLocale);
  final printFlutterOut = isVerbose || isVerboseFlutterCmd;

  final git = getGit(config);

  git.ensureCleanStatus(printChanges: true);

  final reports = <_CheckReport>[];
  try {
    printInfo('Get dependencies...');
    await flutter.pubGetOrFail(
      printStdOut: printFlutterOut,
      immediatePrint: printFlutterOut,
    );

    try {
      git.ensureCleanStatus();
    } catch (e) {
      printInfo('⚠️ Some files were changed after pub get. '
          'Check if your committed dependencies are correct.');
    }

    printInfo('Running extract to arb...');
    await extractLocalization(
      l10nConfig,
      printStdOut: printFlutterOut,
      prependWithPubGet: false,
    );

    printInfo('Starting checks...');

    if (locale == null) {
      printInfo('No locale specified, checking all locales.');
    } else {
      printInfo('Checking translations for locale: $locale');
    }

    final checks = <Future<void> Function()>[];

    Future<void> Function() check({
      required String successMessage,
      required String failMessage,
      required Future<_CheckReport> Function() check,
      String? noteForNotExpected,
    }) =>
        () async {
          final report = await check();
          final checkNum = reports.length + 1;
          final checksTotal = checks.length;

          if (report.isOk) {
            _printCheckSuccess(successMessage, checkNum, checksTotal);
          } else {
            _printCheckFailReport(
              failMessage,
              checkNum,
              checksTotal,
              report.results,
              noteForNotExpected: noteForNotExpected,
            );
          }

          reports.add(report);
        };

    checks.addAll([
      check(
        successMessage: 'All strings have translation in ARB',
        failMessage: 'Untranslated strings found in ARB',
        check: () => _checkForUntranslated(l10nConfig, locale).report(),
      ),
      check(
        successMessage: 'All strings were sent for translation',
        failMessage: 'Some strings probably were not sent for translation',
        check: () => _checkForUnsent(l10nConfig).report(),
      ),
      check(
        successMessage: 'All strings have translation in XML',
        failMessage: 'Untranslated or redundant strings found in XML',
        check: () => _checkForUntranslatedXml(l10nConfig, locale).report(),
        noteForNotExpected: 'If you see this message, '
            'it means that some strings are presented in XML for the locale, but not in the base XML. '
            'Before do anything about it, check that all required strings are present in the base XML file.',
      ),
      check(
        successMessage: 'No duplicated keys in XML',
        failMessage: 'Duplicated keys found in XML',
        check: () => _checkXmlForDuplicates(l10nConfig, locale).report(),
      ),
      check(
        successMessage: 'All strings are imported to ARB',
        failMessage: 'Some strings are not imported to ARB from XML',
        check: () => _checkForNotImportedToArb(l10nConfig, locale).report(),
      ),
      check(
        successMessage: 'Generated localization code is up to date',
        failMessage:
            'Localization code probably is not generated after last changes',
        check: () => _checkForNotGeneratedCode(
          git,
          l10nConfig,
          locale,
          printFlutterOut: printFlutterOut,
        ).report(),
      ),
    ]);

    // Run all checks in sequential order
    for (final check in checks) {
      await check();
    }

    printInfo('Checks completed.');
  } finally {
    // Reset all changes after command execution
    printVerbose('Resetting all changes in GIT repository...');
    git.resetHard();
  }

  printInfo('');

  final total = reports.length;
  if (total == 0) {
    return error(
      2,
      message: 'No checks were performed. This is probably a bug.',
    );
  } else if (reports.every((e) => e.isOk)) {
    return success(message: '🏆 All checks passed [$total/$total].');
  } else {
    final failed = reports.countWhere((e) => !e.isOk);
    return error(
      _kExitCodeCheckFailed,
      message: '🚨 $failed of $total checks failed. See details above',
    );
  }
}