cc-stuff/runonchange.lua

44 lines
1.0 KiB
Lua

local args = { ... }
local modtimes = {}
if #args == 0 then
print("usage: runonchange [files to watch ...] -- program [args...]")
return
end
while #args > 0 do
local name = table.remove(args, 1)
if name == "--" then break end
modtimes[name] = -1
end
if #args == 0 then
printError("No executable was given")
return
end
local curdir = shell.dir()
while true do
local shall_run = false
for name, modtime in pairs(modtimes) do
local path = name:sub(1,1) == "/" and name or (curdir .. "/" .. name)
local modtime_new = fs.attributes(path).modified
if modtime_new ~= modtime then
shall_run = true
modtimes[name] = modtime_new
print(name .. " was modified")
end
end
if shall_run then
local succ, err = pcall(shell.run, table.unpack(args))
if succ then
print("Process finished successfully")
else
printError("Process crashed: " .. err)
end
end
os.sleep(0.5)
end