run method

  1. @override
Future<ExitCode> run([
  1. List<String>? args
])
override

Runs this command.

The return value is wrapped in a Future if necessary and returned by CommandRunner.runCommand.

Implementation

@override
Future<ExitCode> run([List<String>? args]) async {
  final argResults = argParser.parse(args ?? this.argResults?.rest ?? []);
  final neverQuit = argResults['never-exit'] as bool? ?? false;
  final listOut = argResults['list'] as bool? ?? false;

  if (argResults['help'] as bool? ?? false) {
    printUsage();
    return ExitCode.success;
  }

  final keys = args ?? argResults.rest;

  final concurrent = argResults['concurrent'] == true;
  final disableConcurrency =
      argResults.wasParsed('concurrent') && !concurrent;

  if (disableConcurrency) {
    logger.warn('Disabling all concurrent runs');
  }

  final validateResult = await validate(keys);
  if (validateResult != null) {
    return validateResult;
  }

  final result = commandsToRun(keys, listOut: listOut).single;

  if (result.exitCode case final ExitCode exitCode) {
    return exitCode;
  }

  assert(result.commands != null, 'commands should not be null');

  final bail = result.bail ^ (argResults['bail'] as bool? ?? false);

  Future<ExitCode> runCommands() => _run(
        argResults: argResults,
        bail: bail,
        concurrent: concurrent,
        disableConcurrency: disableConcurrency,
        commands: result.commands ?? [],
        combinedEnvConfig: result.combinedEnvConfig,
      );

  if (neverQuit) {
    logger
      ..write('\n')
      ..warn('Never exit is set, restarting after each run.')
      ..warn('To exit, press Ctrl+C or close the terminal.')
      ..write('\n');

    await Future<void>.delayed(const Duration(seconds: 3));

    while (true) {
      await runCommands();

      logger
        ..warn('Restarting in 1 second')
        ..write('\n');

      await Future<void>.delayed(const Duration(seconds: 1));
    }
  }

  return runCommands();
}