refactor: working on invoked process 13 pass 1 fail
This commit is contained in:
parent
4b4a321d99
commit
e2be3ebd54
1 changed files with 48 additions and 22 deletions
|
@ -44,6 +44,7 @@ class InvokedProcess {
|
|||
_handleOutput(data, _outputBuffer);
|
||||
},
|
||||
onDone: _stdoutController.close,
|
||||
cancelOnError: false,
|
||||
);
|
||||
|
||||
_stderrSubscription = _process.stderr.listen(
|
||||
|
@ -52,6 +53,7 @@ class InvokedProcess {
|
|||
_handleOutput(data, _errorBuffer);
|
||||
},
|
||||
onDone: _stderrController.close,
|
||||
cancelOnError: false,
|
||||
);
|
||||
}
|
||||
|
||||
|
@ -76,7 +78,11 @@ class InvokedProcess {
|
|||
|
||||
/// Kill the process.
|
||||
bool kill([ProcessSignal signal = ProcessSignal.sigterm]) {
|
||||
try {
|
||||
return _process.kill(signal);
|
||||
} catch (e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/// Get the process exit code.
|
||||
|
@ -84,8 +90,13 @@ class InvokedProcess {
|
|||
|
||||
/// Wait for the process to complete.
|
||||
Future<ProcessResult> wait() async {
|
||||
try {
|
||||
// Wait for process to complete first
|
||||
final exitCode = await _process.exitCode;
|
||||
|
||||
// Give streams a chance to complete
|
||||
await Future.delayed(Duration(milliseconds: 10));
|
||||
|
||||
// Cancel stream subscriptions
|
||||
await _stdoutSubscription.cancel();
|
||||
await _stderrSubscription.cancel();
|
||||
|
@ -96,6 +107,12 @@ class InvokedProcess {
|
|||
output: _outputBuffer.toString(),
|
||||
errorOutput: _errorBuffer.toString(),
|
||||
);
|
||||
} finally {
|
||||
// Ensure stdin is closed
|
||||
try {
|
||||
_process.stdin.close();
|
||||
} catch (_) {}
|
||||
}
|
||||
}
|
||||
|
||||
/// Get the process stdout stream.
|
||||
|
@ -109,24 +126,33 @@ class InvokedProcess {
|
|||
|
||||
/// Write data to the process stdin.
|
||||
Future<void> write(String input) async {
|
||||
try {
|
||||
_process.stdin.write(input);
|
||||
_process.stdin.flush();
|
||||
await _process.stdin.flush();
|
||||
if (input.endsWith('\n')) {
|
||||
_process.stdin.close();
|
||||
await _process.stdin.close();
|
||||
await Future.delayed(Duration(milliseconds: 10));
|
||||
}
|
||||
} catch (_) {}
|
||||
}
|
||||
|
||||
/// Write lines to the process stdin.
|
||||
Future<void> writeLines(List<String> lines) async {
|
||||
try {
|
||||
for (final line in lines) {
|
||||
_process.stdin.write('$line\n');
|
||||
_process.stdin.flush();
|
||||
await _process.stdin.flush();
|
||||
}
|
||||
_process.stdin.close();
|
||||
await _process.stdin.close();
|
||||
await Future.delayed(Duration(milliseconds: 10));
|
||||
} catch (_) {}
|
||||
}
|
||||
|
||||
/// Close stdin.
|
||||
Future<void> closeStdin() async {
|
||||
_process.stdin.close();
|
||||
try {
|
||||
await _process.stdin.close();
|
||||
await Future.delayed(Duration(milliseconds: 10));
|
||||
} catch (_) {}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue