| local uv = vim.uv |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| |
| |
| |
| |
| |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| |
| local SIG = { |
| HUP = 1, |
| INT = 2, |
| KILL = 9, |
| TERM = 15, |
| |
| } |
|
|
| |
| local function close_handle(handle) |
| if handle and not handle:is_closing() then |
| handle:close() |
| end |
| end |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| local SystemObj = {} |
|
|
| |
| |
| local function new_systemobj(state) |
| return setmetatable({ |
| cmd = state.cmd, |
| pid = state.pid, |
| _state = state, |
| }, { __index = SystemObj }) |
| end |
|
|
| |
| function SystemObj:kill(signal) |
| self._state.handle:kill(signal) |
| end |
|
|
| |
| |
| function SystemObj:_timeout(signal) |
| self._state.done = 'timeout' |
| self:kill(signal or SIG.TERM) |
| end |
|
|
| |
| local MAX_TIMEOUT = 2 ^ 31 - 1 |
|
|
| |
| |
| function SystemObj:wait(timeout) |
| local state = self._state |
|
|
| local done = vim.wait(timeout or state.timeout or MAX_TIMEOUT, function() |
| return state.result ~= nil |
| end, nil, true) |
|
|
| if not done then |
| |
| self:_timeout(SIG.KILL) |
| vim.wait(timeout or state.timeout or MAX_TIMEOUT, function() |
| return state.result ~= nil |
| end, nil, true) |
| end |
|
|
| return state.result |
| end |
|
|
| |
| function SystemObj:write(data) |
| local stdin = self._state.stdin |
|
|
| if not stdin then |
| error('stdin has not been opened on this object') |
| end |
|
|
| if type(data) == 'table' then |
| for _, v in ipairs(data) do |
| stdin:write(v) |
| stdin:write('\n') |
| end |
| elseif type(data) == 'string' then |
| stdin:write(data) |
| elseif data == nil then |
| |
| |
| |
| |
| stdin:write('', function() |
| stdin:shutdown(function() |
| close_handle(stdin) |
| end) |
| end) |
| end |
| end |
|
|
| |
| function SystemObj:is_closing() |
| local handle = self._state.handle |
| return handle == nil or handle:is_closing() or false |
| end |
|
|
| |
| |
| |
| |
| |
| local function setup_output(output, text) |
| if output == false then |
| return |
| end |
|
|
| local bucket |
| local handler |
|
|
| if type(output) == 'function' then |
| handler = output |
| else |
| bucket = {} |
| handler = function(err, data) |
| if err then |
| error(err) |
| end |
| if text and data then |
| bucket[#bucket + 1] = data:gsub('\r\n', '\n') |
| else |
| bucket[#bucket + 1] = data |
| end |
| end |
| end |
|
|
| local pipe = assert(uv.new_pipe(false)) |
|
|
| |
| local function handler_with_close(err, data) |
| handler(err, data) |
| if data == nil then |
| pipe:read_stop() |
| pipe:close() |
| end |
| end |
|
|
| return pipe, handler_with_close, bucket |
| end |
|
|
| |
| |
| |
| local function setup_input(input) |
| if not input then |
| return |
| end |
|
|
| local towrite |
| if type(input) == 'string' or type(input) == 'table' then |
| towrite = input |
| end |
|
|
| return assert(uv.new_pipe(false)), towrite |
| end |
|
|
| |
| local function base_env() |
| local env = vim.fn.environ() |
| env['NVIM'] = vim.v.servername |
| env['NVIM_LISTEN_ADDRESS'] = nil |
| return env |
| end |
|
|
| |
| |
| |
| |
| |
| |
| local function setup_env(env, clear_env) |
| if not env and clear_env then |
| return |
| end |
|
|
| env = env or {} |
| if not clear_env then |
| |
| env = vim.tbl_extend('force', base_env(), env) |
| end |
|
|
| local renv = {} |
| for k, v in pairs(env) do |
| renv[#renv + 1] = string.format('%s=%s', k, tostring(v)) |
| end |
|
|
| return renv |
| end |
|
|
| local is_win = vim.fn.has('win32') == 1 |
|
|
| local M = {} |
|
|
| |
| |
| |
| |
| |
| local function spawn(cmd, opts, on_exit, on_error) |
| if is_win then |
| local cmd1 = vim.fn.exepath(cmd) |
| if cmd1 ~= '' then |
| cmd = cmd1 |
| end |
| end |
|
|
| local handle, pid_or_err = uv.spawn(cmd, opts, on_exit) |
| if not handle then |
| on_error() |
| if opts.cwd and not uv.fs_stat(opts.cwd) then |
| error(("%s (cwd): '%s'"):format(pid_or_err, opts.cwd)) |
| elseif vim.fn.executable(cmd) == 0 then |
| error(("%s (cmd): '%s'"):format(pid_or_err, cmd)) |
| else |
| error(pid_or_err) |
| end |
| end |
| return handle, pid_or_err |
| end |
|
|
| |
| |
| |
| local function timer_oneshot(timeout, cb) |
| local timer = assert(uv.new_timer()) |
| timer:start(timeout, 0, function() |
| timer:stop() |
| timer:close() |
| cb() |
| end) |
| return timer |
| end |
|
|
| |
| |
| |
| |
| local function _on_exit(state, code, signal, on_exit) |
| close_handle(state.handle) |
| close_handle(state.stdin) |
| close_handle(state.timer) |
|
|
| |
| |
|
|
| local check = assert(uv.new_check()) |
| check:start(function() |
| for _, pipe in pairs({ state.stdin, state.stdout, state.stderr }) do |
| if not pipe:is_closing() then |
| return |
| end |
| end |
| check:stop() |
| check:close() |
|
|
| if state.done == nil then |
| state.done = true |
| end |
|
|
| if (code == 0 or code == 1) and state.done == 'timeout' then |
| |
| |
| code = 124 |
| end |
|
|
| local stdout_data = state.stdout_data |
| local stderr_data = state.stderr_data |
|
|
| state.result = { |
| code = code, |
| signal = signal, |
| stdout = stdout_data and table.concat(stdout_data) or nil, |
| stderr = stderr_data and table.concat(stderr_data) or nil, |
| } |
|
|
| if on_exit then |
| on_exit(state.result) |
| end |
| end) |
| end |
|
|
| |
| local function _on_error(state) |
| close_handle(state.handle) |
| close_handle(state.stdin) |
| close_handle(state.stdout) |
| close_handle(state.stderr) |
| close_handle(state.timer) |
| end |
|
|
| |
| |
| |
| |
| |
| |
| function M.run(cmd, opts, on_exit) |
| vim.validate('cmd', cmd, 'table') |
| vim.validate('opts', opts, 'table', true) |
| vim.validate('on_exit', on_exit, 'function', true) |
|
|
| opts = opts or {} |
|
|
| local stdout, stdout_handler, stdout_data = setup_output(opts.stdout, opts.text) |
| local stderr, stderr_handler, stderr_data = setup_output(opts.stderr, opts.text) |
| local stdin, towrite = setup_input(opts.stdin) |
|
|
| |
| local state = { |
| done = false, |
| cmd = cmd, |
| timeout = opts.timeout, |
| stdin = stdin, |
| stdout = stdout, |
| stdout_data = stdout_data, |
| stderr = stderr, |
| stderr_data = stderr_data, |
| } |
|
|
| |
| state.handle, state.pid = spawn(cmd[1], { |
| args = vim.list_slice(cmd, 2), |
| stdio = { stdin, stdout, stderr }, |
| cwd = opts.cwd, |
| |
| env = setup_env(opts.env, opts.clear_env), |
| detached = opts.detach, |
| hide = true, |
| }, function(code, signal) |
| _on_exit(state, code, signal, on_exit) |
| end, function() |
| _on_error(state) |
| end) |
|
|
| if stdout and stdout_handler then |
| stdout:read_start(stdout_handler) |
| end |
|
|
| if stderr and stderr_handler then |
| stderr:read_start(stderr_handler) |
| end |
|
|
| local obj = new_systemobj(state) |
|
|
| if towrite then |
| obj:write(towrite) |
| obj:write(nil) |
| end |
|
|
| if opts.timeout then |
| state.timer = timer_oneshot(opts.timeout, function() |
| if state.handle and state.handle:is_active() then |
| obj:_timeout() |
| end |
| end) |
| end |
|
|
| return obj |
| end |
|
|
| return M |
|
|