mirror of
https://github.com/leafo/moonscript.git
synced 2024-12-08 01:54:24 +00:00
117 lines
2.8 KiB
Lua
Executable File
117 lines
2.8 KiB
Lua
Executable File
#!/usr/bin/env lua
|
|
local argparse = require("argparse")
|
|
local moonscript = require("moonscript.base")
|
|
local util = require("moonscript.util")
|
|
local errors = require("moonscript.errors")
|
|
local unpack = util.unpack
|
|
local argparser = argparse()({
|
|
name = "moon"
|
|
})
|
|
argparser:argument("script")
|
|
argparser:argument("args"):args("*")
|
|
argparser:option("-c --coverage", "Collect and print code coverage")
|
|
argparser:option("-d", "Disable stack trace rewriting")
|
|
argparser:option("-v --version", "Print version information")
|
|
local base = 0
|
|
local _list_0 = arg
|
|
for _index_0 = 1, #_list_0 do
|
|
local flag = _list_0[_index_0]
|
|
base = base + 1
|
|
if flag:sub(1, 1) ~= "-" then
|
|
break
|
|
end
|
|
end
|
|
local args = {
|
|
unpack(arg, 1, base)
|
|
}
|
|
local opts = argparser:parse(args)
|
|
local print_err
|
|
print_err = function(...)
|
|
local msg = table.concat((function(...)
|
|
local _accum_0 = { }
|
|
local _len_0 = 1
|
|
local _list_1 = {
|
|
...
|
|
}
|
|
for _index_0 = 1, #_list_1 do
|
|
local v = _list_1[_index_0]
|
|
_accum_0[_len_0] = tostring(v)
|
|
_len_0 = _len_0 + 1
|
|
end
|
|
return _accum_0
|
|
end)(...), "\t")
|
|
return io.stderr:write(msg .. "\n")
|
|
end
|
|
local run
|
|
run = function()
|
|
if opts.version then
|
|
require("moonscript.version").print_version()
|
|
os.exit()
|
|
end
|
|
local script_fname = opts.script
|
|
args = {
|
|
unpack(arg, base + 1)
|
|
}
|
|
args[-1] = arg[0]
|
|
args[0] = opts.script
|
|
local moonscript_chunk, lua_parse_error
|
|
local passed, err = pcall(function()
|
|
moonscript_chunk, lua_parse_error = moonscript.loadfile(script_fname, {
|
|
implicitly_return_root = false
|
|
})
|
|
end)
|
|
if not (passed) then
|
|
print_err(err)
|
|
os.exit(1)
|
|
end
|
|
if not (moonscript_chunk) then
|
|
if lua_parse_error then
|
|
print_err(lua_parse_error)
|
|
else
|
|
print_err("Can't file file: " .. tostring(script_fname))
|
|
end
|
|
os.exit(1)
|
|
end
|
|
util.getfenv(moonscript_chunk).arg = args
|
|
local run_chunk
|
|
run_chunk = function()
|
|
moonscript.insert_loader()
|
|
moonscript_chunk(unpack(args))
|
|
return moonscript.remove_loader()
|
|
end
|
|
if opts.d then
|
|
return run_chunk()
|
|
end
|
|
local err, trace, cov
|
|
if opts.coverage then
|
|
print("starting coverage")
|
|
local coverage = require("moonscript.cmd.coverage")
|
|
cov = coverage.CodeCoverage()
|
|
cov:start()
|
|
end
|
|
xpcall(run_chunk, function(_err)
|
|
err = _err
|
|
trace = debug.traceback("", 2)
|
|
end)
|
|
if err then
|
|
local truncated = errors.truncate_traceback(util.trim(trace))
|
|
local rewritten = errors.rewrite_traceback(truncated, err)
|
|
if rewritten then
|
|
print_err(rewritten)
|
|
else
|
|
print_err(table.concat({
|
|
err,
|
|
util.trim(trace)
|
|
}, "\n"))
|
|
end
|
|
return os.exit(1)
|
|
else
|
|
if cov then
|
|
cov:stop()
|
|
return cov:print_results()
|
|
end
|
|
end
|
|
end
|
|
return run()
|
|
-- vim: set filetype=lua:
|