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 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;
  }

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

  for (final platform in project.config.platforms.values) {
    if (platform.format != 'arb') {
      // v1.0 ships only the ARB adapter. Other formats land in v1.1.
      stdout.writeln(
        '! ${platform.name} (format: ${platform.format}) — adapter '
        'lands in v1.1; skipping.',
      );
      totalSkipped++;
      continue;
    }
    final outcome = _syncPlatform(project, platform, force: force);
    totalWritten += outcome.filesWritten;
    if (outcome.unnamespacedKeys.isNotEmpty) {
      unnamespacedPerPlatform[platform.name] = outcome.unnamespacedKeys;
    }
    if (outcome.excludedNamespaces.isNotEmpty) {
      excludedPerPlatform[platform.name] = outcome.excludedNamespaces;
    }
  }

  _maybeWarnUnnamespaced(unnamespacedPerPlatform);
  _maybeWarnExcludedNamespaces(excludedPerPlatform);

  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;
}