80 lines
1.6 KiB
Lua
80 lines
1.6 KiB
Lua
local core = require "core"
|
|
local process = require "process"
|
|
|
|
---@class shell: runner
|
|
local shell = {
|
|
name = "shell",
|
|
proc = nil ---@type process|nil
|
|
}
|
|
|
|
function shell:new(o)
|
|
o = o or {}
|
|
setmetatable(o, self)
|
|
self.__index = self
|
|
return o
|
|
end
|
|
|
|
---@meta
|
|
function shell.log(msg) end
|
|
---@meta
|
|
function shell.error(msg) end
|
|
---@meta
|
|
function shell.stdout(msg) end
|
|
---@meta
|
|
function shell.stderr(msg) end
|
|
---@meta
|
|
function shell.exited() end
|
|
|
|
|
|
function shell:run(target, name)
|
|
local opts = {
|
|
cwd = target.cwd or ".",
|
|
env = target.env or {},
|
|
stdout = process.REDIRECT_PIPE,
|
|
stderr = process.REDIRECT_PIPE
|
|
}
|
|
if target.cmd then
|
|
self.proc = process.start(target.cmd, opts)
|
|
if not self.proc then
|
|
self.error("Could not start process...")
|
|
return
|
|
end
|
|
else
|
|
self.error(string.format("command not specified for target %s", name))
|
|
end
|
|
|
|
-- output pump
|
|
core.add_thread(function()
|
|
while true do
|
|
core.redraw = true
|
|
coroutine.yield(0.016) -- 60FPS
|
|
local sout = self.proc:read_stdout()
|
|
local serr = self.proc:read_stderr()
|
|
if sout == nil and serr == nil then
|
|
self.exited()
|
|
break
|
|
end
|
|
if sout and sout~="" then self.stdout(sout) end
|
|
if serr and serr~="" then self.stderr(serr) end
|
|
end
|
|
end)
|
|
end
|
|
|
|
function shell:wait(time)
|
|
if not self.proc then return end
|
|
return self.proc:wait(time)
|
|
end
|
|
|
|
function shell:kill()
|
|
if not self.proc then return end
|
|
self.proc:kill()
|
|
end
|
|
|
|
function shell:terminate()
|
|
if not self.proc then return end
|
|
self.proc:terminate()
|
|
end
|
|
|
|
return shell
|
|
|