Merge pull request #200 from andyholmes/use-gsubprocess

commandLineUtil: refactor to use GSubprocess
This commit is contained in:
Vasilii
2021-04-27 20:13:42 +03:00
committed by GitHub

View File

@@ -12,42 +12,23 @@ var CommandLineUtil = class {
execute(callback) { execute(callback) {
try{ try{
this._callback = callback; this._callback = callback;
let [exit, pid, stdinFd, stdoutFd, stderrFd] =
GLib.spawn_async_with_pipes(null, /* cwd */
this._argv, /* args */
null, /* env */
GLib.SpawnFlags.DO_NOT_REAP_CHILD,
null /* child_setup */);
let stdout = new Gio.UnixInputStream({fd: stdoutFd, close_fd: true});
let outReader = new Gio.DataInputStream({base_stream: stdout});
let stderr = new Gio.UnixInputStream({fd: stderrFd, close_fd: true}); let proc = Gio.Subprocess.new(this._argv,
let errReader = new Gio.DataInputStream({base_stream: stderr}); Gio.SubprocessFlags.STDOUT_PIPE |
Gio.SubprocessFlags.STDERR_PIPE);
GLib.close(stdinFd); proc.communicate_utf8_async(null, null, (proc, result) => {
try {
let [, stdout, stderr] = proc.communicate_utf8_finish(result);
let childWatch = GLib.child_watch_add(GLib.PRIORITY_DEFAULT, pid, (pid, status, requestObj) => { this._output = stdout ? stdout.split('\n') : [];
let output = []; this._error_output = stderr ? stderr.split('\n') : [];
let error_output = []; } catch (e) {
let [line, size] = [null, 0]; logError(e);
} finally {
while (([line, size] = outReader.read_line(null)) != null && line != null) { this._updated = true;
if(line) callback();
output.push(ByteArray.toString(line));
} }
stdout.close(null);
while (([line, size] = errReader.read_line(null)) != null && line != null) {
if(line)
error_output.push(ByteArray.toString(line));
}
stderr.close(null);
GLib.source_remove(childWatch);
this._output = output;
this._error_output = error_output;
this._updated = true;
callback();
}); });
} catch(e){ } catch(e){
global.log(e.toString()); global.log(e.toString());