2021-04-29 20:19:56 -04:00
|
|
|
local utime = utime
|
|
|
|
|
2021-04-29 07:22:08 -04:00
|
|
|
local base = string.gsub(@@LUA_SCRIPT_FILENAME@@, "(.*[/\\])(.*)", "%1")
|
|
|
|
|
2021-03-03 15:40:19 -05:00
|
|
|
local _M = {}
|
|
|
|
|
2021-04-28 04:33:52 -04:00
|
|
|
_M.isWin = package.config:sub(1, 1) == '\\'
|
|
|
|
|
2021-04-28 18:54:39 -04:00
|
|
|
--- Echo a command, run it, and return the file handle
|
|
|
|
--- @param cmd string The command to execute
|
|
|
|
--- @param workdir string The working directory
|
|
|
|
--- @param env table The environment variables
|
|
|
|
function _M.popenCmd(cmd, workdir, env)
|
|
|
|
local cmdParts = {}
|
|
|
|
if workdir ~= nil then
|
|
|
|
if _M.isWin then
|
|
|
|
table.insert(cmdParts, 'cd /d "'..workdir..'" &&')
|
|
|
|
else
|
|
|
|
table.insert(cmdParts, 'cd "'..workdir..'" &&')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
if env ~= nil then
|
|
|
|
for k,v in pairs(env) do
|
|
|
|
if _M.isWin then
|
|
|
|
table.insert(cmdParts, string.format("set %s=%s&&", k, v))
|
|
|
|
else
|
|
|
|
table.insert(cmdParts, string.format("%s='%s'", k, v))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
table.insert(cmdParts, cmd)
|
|
|
|
local fullCmd = table.concat(cmdParts, " ")
|
|
|
|
print(fullCmd)
|
|
|
|
|
|
|
|
--[[ local dummy = "/dev/null"
|
|
|
|
if isWin then
|
|
|
|
dummy = "NUL"
|
|
|
|
end
|
|
|
|
return io.open(dummy, 'r') ]]
|
|
|
|
return io.popen(fullCmd, 'r')
|
|
|
|
end
|
|
|
|
|
|
|
|
--- Echo a command, run it, and handle any errors
|
|
|
|
--- @return string string The stdout
|
|
|
|
function _M.doCmd(...)
|
|
|
|
return _M.scrapeCmd('*a', ...)
|
|
|
|
end
|
|
|
|
|
2021-04-29 07:22:08 -04:00
|
|
|
--- Download a url
|
|
|
|
--- @param url string URI of resource to download
|
|
|
|
--- @param dest string File to save resource to
|
|
|
|
function _M.downloadFile (url, dest)
|
|
|
|
return _M.doCmd('curl -sL "'..url..'" > "'..dest..'" || wget -qO- "'..url..'" > "'..dest..'"')
|
|
|
|
end
|
|
|
|
|
|
|
|
--- Unzip a ZIP file with unzip or tar
|
|
|
|
--- @param zipfile string The ZIP file path
|
|
|
|
--- @param dest string Where to unzip the ZIP file. Beware ZIP bombs.
|
|
|
|
function _M.unzip (zipfile, dest)
|
|
|
|
return _M.doCmd('unzip "'..zipfile..'" -d "'..dest..
|
|
|
|
'" 2>&1 || tar -C "'..dest..'" -xvf "'..zipfile..
|
|
|
|
'" 2>&1', nil)
|
|
|
|
end
|
|
|
|
|
2021-04-29 04:56:55 -04:00
|
|
|
--- Create a directory
|
|
|
|
--- @return string dir The directory to create
|
|
|
|
function _M.mkdir(dir)
|
|
|
|
if _M.isWin then
|
|
|
|
return _M.doCmd('if not exist "'..dir..'" mkdir "'..dir..'"')
|
|
|
|
else
|
|
|
|
return _M.doCmd("mkdir '"..dir.."'")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-04-28 18:54:39 -04:00
|
|
|
--- Run a command and get the output
|
|
|
|
--- @param formats table|string|number List or single io.read() specifier
|
|
|
|
--- @return table table List of results based on read specifiers
|
|
|
|
function _M.scrapeCmd(formats, ...)
|
|
|
|
local poppet = _M.popenCmd(...)
|
|
|
|
local outputs = nil
|
|
|
|
if type(formats) ~= 'table' then
|
|
|
|
outputs = poppet:read(formats)
|
|
|
|
else
|
|
|
|
outputs = {}
|
|
|
|
for i=1,#formats,1 do
|
|
|
|
table.insert(outputs, poppet:read(formats[i]))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
_M.closeCmd(poppet)
|
|
|
|
return outputs
|
|
|
|
end
|
|
|
|
|
|
|
|
--- Check the command's exit code and throw a Lua error if it isn't right
|
|
|
|
--- @param handle file* The handle of the command
|
|
|
|
function _M.closeCmd(handle)
|
|
|
|
local ok, state, code = handle:close()
|
|
|
|
if state ~= "exit" then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
if code ~= 0 then
|
|
|
|
error("The last command failed")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-04-29 07:22:08 -04:00
|
|
|
function _M.finishWaiting(waiter, count)
|
|
|
|
if count == nil then
|
|
|
|
count = 1
|
|
|
|
end
|
|
|
|
|
|
|
|
if _M.isWin then
|
|
|
|
local i = 1
|
|
|
|
while i <= count do
|
|
|
|
local line = waiter:read("*l")
|
|
|
|
for chr in line:gmatch(";") do
|
|
|
|
i = i + 1
|
|
|
|
end
|
|
|
|
i = i + 1
|
|
|
|
end
|
|
|
|
else
|
|
|
|
waiter:read("*a")
|
|
|
|
_M.closeCmd(waiter)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function _M.startWaiting(filenames, count, wild)
|
2021-04-28 23:18:26 -04:00
|
|
|
if type(filenames) == 'string' then
|
|
|
|
if wild == nil then
|
|
|
|
wild = filenames
|
|
|
|
end
|
|
|
|
|
|
|
|
filenames = {filenames}
|
|
|
|
end
|
|
|
|
|
2021-04-28 18:54:39 -04:00
|
|
|
if count == nil then
|
2021-04-28 23:18:26 -04:00
|
|
|
count = #filenames
|
2021-04-28 18:54:39 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
if _M.isWin then
|
|
|
|
local sec, usec = utime()
|
|
|
|
print(string.format('Starting watching file at %d', sec * 1000000 + usec))
|
|
|
|
|
2021-04-29 07:22:08 -04:00
|
|
|
local cmd = '"'..base..'/watchexec/watchexec.exe" "-w" "'..table.concat(filenames, '" "-w" "')..'" "echo" "%WATCHEXEC_WRITTEN_PATH%"'
|
|
|
|
local poppet = _M.popenCmd(cmd, base)
|
2021-04-29 05:33:47 -04:00
|
|
|
|
2021-04-29 07:22:08 -04:00
|
|
|
poppet:read("*l")
|
2021-04-29 05:33:47 -04:00
|
|
|
|
|
|
|
return poppet
|
2021-04-28 18:54:39 -04:00
|
|
|
else
|
2021-04-28 23:18:26 -04:00
|
|
|
local watchCmd = ''
|
|
|
|
if count == 1 then
|
|
|
|
watchCmd = [[which inotifywait >/dev/null && { inotifywait -q -e close_write ']]..filenames[1]..[[' || exit 0 ; }]]
|
|
|
|
else
|
|
|
|
watchCmd = [[bash <<'EOF'
|
|
|
|
COUNT=]]..count..[[
|
|
|
|
FILENAMES=(']]..table.concat(filenames, "' '")..[[')
|
|
|
|
declare -A SEEN
|
|
|
|
((I = 0))
|
|
|
|
set -m
|
|
|
|
which inotifywait >/dev/null
|
|
|
|
( inotifywait -q -m -e close_write "${FILENAMES[@]}" | while read LINE ; do
|
|
|
|
SEEN["$LINE"]=1
|
|
|
|
TOTAL=${#SEEN[@]}
|
|
|
|
if ((TOTAL == COUNT)) ; then
|
|
|
|
kill -s TERM 0
|
|
|
|
fi
|
|
|
|
done ) &
|
|
|
|
wait
|
|
|
|
EOF]]
|
|
|
|
end
|
|
|
|
return _M.popenCmd(watchCmd)
|
2021-04-28 18:54:39 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-03-03 15:40:19 -05:00
|
|
|
function _M.table_to_string(tbl)
|
|
|
|
local result = "{"
|
|
|
|
local keys = {}
|
|
|
|
for k in pairs(tbl) do
|
|
|
|
table.insert(keys, k)
|
|
|
|
end
|
|
|
|
table.sort(keys)
|
|
|
|
for _, k in ipairs(keys) do
|
|
|
|
local v = tbl[k]
|
|
|
|
if type(v) == "number" and v == 0 then
|
2021-03-15 16:05:02 -05:00
|
|
|
--goto continue
|
2021-03-03 15:40:19 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
-- Check the key type (ignore any numerical keys - assume its an array)
|
|
|
|
if type(k) == "string" then
|
|
|
|
result = result.."[\""..k.."\"]".."="
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Check the value type
|
|
|
|
if type(v) == "table" then
|
2021-03-05 20:17:41 -05:00
|
|
|
result = result.._M.table_to_string(v)
|
2021-03-03 15:40:19 -05:00
|
|
|
elseif type(v) == "boolean" then
|
|
|
|
result = result..tostring(v)
|
2021-03-05 14:37:57 -05:00
|
|
|
elseif type(v) == "number" and v >= 0 then
|
|
|
|
result = result..string.format("%x", v)
|
2021-03-03 15:40:19 -05:00
|
|
|
else
|
|
|
|
result = result.."\""..v.."\""
|
|
|
|
end
|
|
|
|
result = result..",\n"
|
|
|
|
::continue::
|
|
|
|
end
|
|
|
|
-- Remove leading commas from the result
|
|
|
|
if result ~= "" then
|
|
|
|
result = result:sub(1, result:len()-1)
|
|
|
|
end
|
|
|
|
return result.."}"
|
|
|
|
end
|
|
|
|
|
|
|
|
function _M.file_exists(name)
|
|
|
|
local f=io.open(name,"r")
|
|
|
|
if f~=nil then io.close(f) return true else return false end
|
|
|
|
end
|
|
|
|
|
2021-03-05 20:17:41 -05:00
|
|
|
return _M
|