refactor: working on invoked process 13 pass 1 fail

This commit is contained in:
Patrick Stewart 2024-12-30 07:16:05 -07:00
parent 4b4a321d99
commit e2be3ebd54

View file

@ -44,6 +44,7 @@ class InvokedProcess {
_handleOutput(data, _outputBuffer); _handleOutput(data, _outputBuffer);
}, },
onDone: _stdoutController.close, onDone: _stdoutController.close,
cancelOnError: false,
); );
_stderrSubscription = _process.stderr.listen( _stderrSubscription = _process.stderr.listen(
@ -52,6 +53,7 @@ class InvokedProcess {
_handleOutput(data, _errorBuffer); _handleOutput(data, _errorBuffer);
}, },
onDone: _stderrController.close, onDone: _stderrController.close,
cancelOnError: false,
); );
} }
@ -76,7 +78,11 @@ class InvokedProcess {
/// Kill the process. /// Kill the process.
bool kill([ProcessSignal signal = ProcessSignal.sigterm]) { bool kill([ProcessSignal signal = ProcessSignal.sigterm]) {
return _process.kill(signal); try {
return _process.kill(signal);
} catch (e) {
return false;
}
} }
/// Get the process exit code. /// Get the process exit code.
@ -84,18 +90,29 @@ class InvokedProcess {
/// Wait for the process to complete. /// Wait for the process to complete.
Future<ProcessResult> wait() async { Future<ProcessResult> wait() async {
final exitCode = await _process.exitCode; try {
// Wait for process to complete first
final exitCode = await _process.exitCode;
// Cancel stream subscriptions // Give streams a chance to complete
await _stdoutSubscription.cancel(); await Future.delayed(Duration(milliseconds: 10));
await _stderrSubscription.cancel();
return ProcessResultImpl( // Cancel stream subscriptions
command: _command, await _stdoutSubscription.cancel();
exitCode: exitCode, await _stderrSubscription.cancel();
output: _outputBuffer.toString(),
errorOutput: _errorBuffer.toString(), return ProcessResultImpl(
); command: _command,
exitCode: exitCode,
output: _outputBuffer.toString(),
errorOutput: _errorBuffer.toString(),
);
} finally {
// Ensure stdin is closed
try {
_process.stdin.close();
} catch (_) {}
}
} }
/// Get the process stdout stream. /// Get the process stdout stream.
@ -109,24 +126,33 @@ class InvokedProcess {
/// Write data to the process stdin. /// Write data to the process stdin.
Future<void> write(String input) async { Future<void> write(String input) async {
_process.stdin.write(input); try {
_process.stdin.flush(); _process.stdin.write(input);
if (input.endsWith('\n')) { await _process.stdin.flush();
_process.stdin.close(); if (input.endsWith('\n')) {
} await _process.stdin.close();
await Future.delayed(Duration(milliseconds: 10));
}
} catch (_) {}
} }
/// Write lines to the process stdin. /// Write lines to the process stdin.
Future<void> writeLines(List<String> lines) async { Future<void> writeLines(List<String> lines) async {
for (final line in lines) { try {
_process.stdin.write('$line\n'); for (final line in lines) {
_process.stdin.flush(); _process.stdin.write('$line\n');
} await _process.stdin.flush();
_process.stdin.close(); }
await _process.stdin.close();
await Future.delayed(Duration(milliseconds: 10));
} catch (_) {}
} }
/// Close stdin. /// Close stdin.
Future<void> closeStdin() async { Future<void> closeStdin() async {
_process.stdin.close(); try {
await _process.stdin.close();
await Future.delayed(Duration(milliseconds: 10));
} catch (_) {}
} }
} }