runAsyncProcess function
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'),
);
}