runAsyncProcess function

Future<ProcessResult> runAsyncProcess(
  1. String executable,
  2. List<String> arguments, {
  3. String? workingDirectory,
  4. Map<String, String>? environment,
  5. bool includeParentEnvironment = true,
  6. bool runInShell = false,
  7. bool printCall = false,
})

Implementation

Future<ProcessResult> runAsyncProcess(
  String executable,
  List<String> arguments, {
  String? workingDirectory,
  Map<String, String>? environment,
  bool includeParentEnvironment = true,
  bool runInShell = false,
  bool printCall = false,
}) async {
  _runAsyncProcessLogger.log(printCall ? Level.INFO : Level.FINE,
      '$executable ${arguments.join(' ')}');
  final result = await Process.start(
    executable,
    arguments,
    workingDirectory: workingDirectory,
    environment: environment,
    includeParentEnvironment: includeParentEnvironment,
    runInShell: runInShell,
  );

  final List<String> stdOutLines = [];
  result.stdout.forEach((line) {
    final stdOutLine = utf8.decode(line);
    if (stdOutLine.isEmpty) return;
    stdOutLines.add(stdOutLine);
    _runAsyncProcessLogger.fine(stdOutLine);
  });

  final List<String> stdErrLines = [];
  result.stderr.forEach((line) {
    final stdErrLine = utf8.decode(line);
    if (stdErrLine.isEmpty) return;
    stdErrLines.add(stdErrLine);
    _runAsyncProcessLogger.severe(stdErrLine);
  });

  final resultCode = await result.exitCode;
  if (resultCode != 0) {
    throw Exception('Process "$executable" failed. See log above.');
  }
  return ProcessResult(
    result.pid,
    resultCode,
    stdOutLines.join('\n'),
    stdErrLines.isEmpty ? Uint8List(0) : stdErrLines.join('\n'),
  );
}