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 results = argResults!;
  final force = results['force'] as bool;
  final dryRun = results['dry-run'] as bool;
  final onlyPlatform = results.option('platform');
  final rest = results.rest;
  if (rest.length > 1) {
    stderr.writeln('sync takes at most one positional argument.');
    return 64;
  }
  final root = rest.isEmpty ? Directory.current.path : rest.first;

  final DialectProject project;
  try {
    project = DialectProject.load(root);
  } on FileSystemException catch (e) {
    stderr.writeln(e.message);
    stderr.writeln(
      'Run `dialect init` first, or pass the project root as an argument.',
    );
    return 66;
  } on FormatException catch (e) {
    stderr.writeln('dialect.yaml or an ARB file is malformed:');
    stderr.writeln('  ${e.message}');
    return 65;
  }

  if (project.config.platforms.isEmpty) {
    stdout.writeln(
      '! dialect sync: no `platforms:` configured in dialect.yaml.',
    );
    stdout.writeln(
      '  Add a platform block (e.g. flutter:) to start emitting files.',
    );
    return 0;
  }

  final platforms = project.config.platforms.values.toList();
  if (onlyPlatform != null) {
    final match = platforms.where((p) => p.name == onlyPlatform).toList();
    if (match.isEmpty) {
      stderr.writeln(
        'No platform named `$onlyPlatform` in dialect.yaml. '
        'Configured: ${platforms.map((p) => p.name).join(", ")}.',
      );
      return 64;
    }
    platforms
      ..clear()
      ..addAll(match);
  }

  var totalWritten = 0;
  var totalSkipped = 0;
  final unnamespacedPerPlatform = <String, Set<String>>{};
  final excludedPerPlatform = <String, Set<String>>{};

  for (final platform in platforms) {
    final _PlatformOutcome outcome;
    try {
      if (platform.format == 'arb') {
        outcome = _syncArbPlatform(
          project,
          platform,
          force: force,
          dryRun: dryRun,
        );
      } else if (JsonAdapter.handles(platform.format)) {
        outcome = _syncJsonPlatform(
          project,
          platform,
          force: force,
          dryRun: dryRun,
        );
      } else {
        stdout.writeln(
          '! ${platform.name} (format: ${platform.format}) — unknown '
          'format; expected one of arb, icu-json, flat-json. Skipping.',
        );
        totalSkipped++;
        continue;
      }
    } on FormatException catch (e) {
      stderr.writeln('✗ ${platform.name}: ${e.message}');
      return 65;
    }
    totalWritten += outcome.filesWritten;
    if (outcome.unnamespacedKeys.isNotEmpty) {
      unnamespacedPerPlatform[platform.name] = outcome.unnamespacedKeys;
    }
    if (outcome.excludedNamespaces.isNotEmpty) {
      excludedPerPlatform[platform.name] = outcome.excludedNamespaces;
    }
    if (outcome.pluralStrippedKeys.isNotEmpty) {
      _warnPluralStripped(platform, outcome.pluralStrippedKeys);
    }
  }

  _maybeWarnUnnamespaced(unnamespacedPerPlatform);
  _maybeWarnExcludedNamespaces(excludedPerPlatform);

  if (dryRun) {
    if (totalWritten == 0) {
      stdout.writeln('✓ dialect sync --dry-run: every output is up to date.');
      return 0;
    }
    stdout.writeln(
      '✗ dialect sync --dry-run: $totalWritten file(s) would change. '
      'Run `dialect sync` to write them.',
    );
    return 1;
  }

  if (totalWritten == 0 && totalSkipped == 0) {
    stdout.writeln(
      '✓ dialect sync: nothing to do (every output is already up to date).',
    );
  } else if (totalWritten == 0) {
    stdout.writeln(
      '✓ dialect sync: $totalSkipped platform(s) skipped, no ARB output.',
    );
  } else {
    stdout.writeln('✓ dialect sync: wrote $totalWritten file(s).');
  }
  return 0;
}