run method

Future<CiStepResult> run(
  1. List<CiCommand> commands, {
  2. required String logPath,
  3. bool append = false,
})

Runs commands in order into the log at logPath (created, or appended to when append), stopping at the first that fails.

Implementation

Future<CiStepResult> run(
  List<CiCommand> commands, {
  required String logPath,
  bool append = false,
}) async {
  final watch = Stopwatch()..start();
  final logFile = File(logPath)..parent.createSync(recursive: true);
  final log = logFile.openSync(
    mode: append ? FileMode.append : FileMode.write,
  );
  final tail = <String>[];
  void write(String line) {
    log.writeStringSync('$line\n');
    log.flushSync();
    tail.add(line);
    if (tail.length > 40) tail.removeAt(0);
  }

  var exit = 0;
  try {
    for (final command in commands) {
      write('\$ $command');
      write(
        '  (in ${command.workingDirectory}, ${DateTime.now().toIso8601String()})',
      );
      await lock.acquire(onWait: onLockWait);
      try {
        exit = await _runOne(command, write);
      } finally {
        lock.release();
      }
      write('[exit $exit]');
      if (exit != 0) break;
    }
  } on ProcessException catch (e) {
    write('could not start: ${e.message} (${e.executable})');
    exit = 127;
  } finally {
    log.closeSync();
  }
  return CiStepResult(
    exitCode: exit,
    logPath: logPath,
    elapsed: watch.elapsed,
    tail: tail,
  );
}