1
0
Fork 0
cc-stuff/runonchange.lua

44 lines
1.0 KiB
Lua
Raw Normal View History

2023-10-17 19:10:09 +03:00
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
2023-12-21 21:47:32 +03:00
local curdir = shell.dir()
2023-10-17 19:10:09 +03:00
while true do
local shall_run = false
for name, modtime in pairs(modtimes) do
2023-12-21 21:47:32 +03:00
local path = name:sub(1,1) == "/" and name or (curdir .. "/" .. name)
local modtime_new = fs.attributes(path).modified
2023-10-17 19:10:09 +03:00
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