run method

Future<int> run(
  1. LiveOptions options,
  2. Future<Object?> action(
    1. LiveApp app,
    2. LiveBlock block
    )
)

Runs action on every target app in parallel, then prints each app's output (or the --json document) and returns the exit code: 0 when every app succeeded, 1 when any failed.

Implementation

Future<int> run(
  LiveOptions options,
  Future<Object?> Function(LiveApp app, LiveBlock block) action,
) async {
  final ({List<LiveApp> targets, int exitCode}) resolved =
      await resolveTargets(options);
  if (resolved.targets.isEmpty) return resolved.exitCode;

  final List<LiveOutcome> outcomes = await Future.wait(
    resolved.targets.map((app) async {
      final LiveBlock block = LiveBlock();
      try {
        final Object? result = await action(app, block);
        return LiveOutcome(app, block, result: result);
      } on LiveReportedFailure catch (e) {
        // The app already explained the failure in the output lines.
        return LiveOutcome(app, block, result: e.result, error: e.message);
      } catch (e) {
        final String message = describeLiveError(e);
        block.failure(message);
        return LiveOutcome(app, block, error: message);
      }
    }),
  );

  if (options.json) {
    output.json([
      for (final LiveOutcome outcome in outcomes) outcome.toJson(),
    ]);
  } else {
    output.blocks([
      for (final LiveOutcome outcome in outcomes)
        (label: outcome.app.device, block: outcome.block),
    ]);
  }

  await _release(resolved.targets);
  return outcomes.every((outcome) => outcome.isSuccess) ? 0 : 1;
}