Add option to get the exit-code and stderr from deferred process

This commit is contained in:
Wojciech Kozlowski 2017-09-09 17:53:17 +01:00
parent e908c27827
commit be266b48aa

View File

@ -756,6 +756,15 @@ The next deferred object receives stdout and stderr string from
the command process." the command process."
(deferred:process-gen 'start-process-shell-command command args)) (deferred:process-gen 'start-process-shell-command command args))
(defun deferred:process-w-stderr (command &rest args)
"A deferred wrapper of `make-process'. Return a deferred
object. The process name and buffer name of the argument of the
`make-process' are generated by this function automatically. The
next deferred object receives a list containing (exit-code stdout
stderr), where stdout and stderr are strings, from the command
process."
(deferred:process-gen 'make-process command args))
(defun deferred:process-buffer (command &rest args) (defun deferred:process-buffer (command &rest args)
"A deferred wrapper of `start-process'. Return a deferred "A deferred wrapper of `start-process'. Return a deferred
object. The process name and buffer name of the argument of the object. The process name and buffer name of the argument of the
@ -772,36 +781,43 @@ The next deferred object receives stdout and stderr buffer from
the command process." the command process."
(deferred:process-buffer-gen 'start-process-shell-command command args)) (deferred:process-buffer-gen 'start-process-shell-command command args))
(defun deferred:process-w-stderr-buffer (command &rest args)
"A deferred wrapper of `make-process'. Return a deferred
object. The process name and buffer name of the argument of the
`make-process' are generated by this function automatically. The
next deferred object receives a list containing (exit-code stdout
stderr), where stdout and stderr are buffers, from the command
process."
(deferred:process-buffer-gen 'make-process command args))
(defun deferred:process-gen (f command args) (defun deferred:process-gen (f command args)
"[internal]" "[internal]"
(let ((pd (deferred:process-buffer-gen f command args)) d) (let ((pd (deferred:process-buffer-gen f command args)) d)
(setq d (deferred:nextc pd (setq d (deferred:nextc pd
(lambda (buf) (lambda (output)
(if (eq f 'make-process)
(let ((exit-code (nth 0 output))
(stdout (nth 1 output))
(stderr (nth 2 output)))
(prog1 (prog1
(with-current-buffer buf (buffer-string)) (list exit-code
(kill-buffer buf))))) (with-current-buffer stdout (buffer-string))
(with-current-buffer stderr (buffer-string)))
(kill-buffer stdout)
(kill-buffer stderr)))
(prog1
(with-current-buffer output (buffer-string))
(kill-buffer output))))))
(setf (deferred-cancel d) (setf (deferred-cancel d)
(lambda (_x) (lambda (_x)
(deferred:default-cancel d) (deferred:default-cancel d)
(deferred:default-cancel pd))) (deferred:default-cancel pd)))
d)) d))
(defun deferred:process-buffer-gen (f command args) (defun deferred:run-proc (nd f proc-name buf-name command args)
"[internal]" "[internal]"
(let ((d (deferred:next)) (uid (deferred:uid))) (let (proc-buf proc)
(let ((proc-name (format "*deferred:*%s*:%s" command uid))
(buf-name (format " *deferred:*%s*:%s" command uid))
(pwd default-directory)
(env process-environment)
(con-type process-connection-type)
(nd (deferred:new)) proc-buf proc)
(deferred:nextc d
(lambda (_x)
(setq proc-buf (get-buffer-create buf-name)) (setq proc-buf (get-buffer-create buf-name))
(condition-case err
(let ((default-directory pwd)
(process-environment env)
(process-connection-type con-type))
(setq proc (setq proc
(if (null (car args)) (if (null (car args))
(apply f proc-name buf-name command nil) (apply f proc-name buf-name command nil)
@ -827,7 +843,53 @@ the command process."
(lambda (x) (deferred:default-cancel x) (lambda (x) (deferred:default-cancel x)
(when proc (when proc
(kill-process proc) (kill-process proc)
(kill-buffer proc-buf))))) (kill-buffer proc-buf))))))
(defun deferred:run-proc-w-stderr (nd proc-name buf-name err-buf-name command args)
"[internal]"
(let (proc-buf proc-err-buf proc)
(setq proc-buf (get-buffer-create buf-name)
proc-err-buf (get-buffer-create err-buf-name))
(setq proc
(make-process
:name proc-name
:buffer buf-name
:command (cons command args)
:sentinel (lambda (proc _event)
(unless (process-live-p proc)
(deferred:post-task nd 'ok
(list (process-exit-status proc)
proc-buf
proc-err-buf))))
:stderr err-buf-name))
(setf (deferred-cancel nd)
(lambda (x) (deferred:default-cancel x)
(when proc
(kill-process proc)
(kill-buffer proc-buf)
(kill-buffer proc-err-buf))))))
(defun deferred:process-buffer-gen (f command args)
"[internal]"
(let ((d (deferred:next)) (uid (deferred:uid)))
(let ((proc-name (format "*deferred:*%s*:%s" command uid))
(buf-name (format " *deferred:*%s*:%s" command uid))
(err-buf-name (format " *deferred:err:*%s*:%s" command uid))
(pwd default-directory)
(env process-environment)
(con-type process-connection-type)
(nd (deferred:new)))
(deferred:nextc d
(lambda (_x)
(condition-case err
(let ((default-directory pwd)
(process-environment env)
(process-connection-type con-type))
(if (eq f 'make-process)
(deferred:run-proc-w-stderr nd
proc-name buf-name err-buf-name command args)
(deferred:run-proc nd
f proc-name buf-name command args)))
(error (deferred:post-task nd 'ng err))) (error (deferred:post-task nd 'ng err)))
nil)) nil))
nd))) nd)))