moonscript/bin/moonc

241 lines
5.6 KiB
Plaintext
Raw Permalink Normal View History

2011-08-13 04:07:44 +00:00
#!/usr/bin/env lua
local argparse = require "argparse"
2013-02-28 03:17:16 +00:00
local lfs = require "lfs"
local parser = argparse()
parser:flag("-l --lint", "Perform a lint on the file instead of compiling")
2022-11-02 21:45:36 +00:00
parser:flag("-v --version", "Print version")
parser:flag("-w --watch", "Watch file/directory for updates")
2018-02-03 05:55:21 +00:00
parser:option("--transform", "Transform syntax tree with module")
2022-11-02 21:45:36 +00:00
parser:mutex(
2022-11-02 21:45:36 +00:00
parser:option("-t --output-to", "Specify where to place compiled files"),
parser:option("-o", "Write output to file"),
parser:flag("-p", "Write output to standard output"),
parser:flag("-T", "Write parse tree instead of code (to stdout)"),
parser:flag("-b", "Write parse and compile time instead of code(to stdout)"),
parser:flag("-X", "Write line rewrite map instead of code (to stdout)")
)
2022-11-02 21:45:36 +00:00
parser:flag("-",
2022-11-02 21:45:36 +00:00
"Read from standard in, print to standard out (Must be only argument)")
local read_stdin = arg[1] == "-" -- luacheck: ignore 113
if not read_stdin then
2022-11-02 21:45:36 +00:00
parser:argument("file/directory"):args("+")
2020-12-29 16:16:44 +00:00
else
2022-11-04 19:51:40 +00:00
if arg[2] ~= nil then
io.stderr:write("- must be the only argument\n")
os.exit(1)
end
end
2012-01-22 19:53:13 +00:00
2020-12-29 16:14:52 +00:00
local opts = read_stdin and {} or parser:parse()
if opts.version then
2022-11-02 21:45:36 +00:00
local v = require "moonscript.version"
v.print_version()
os.exit()
2011-07-19 06:53:43 +00:00
end
2013-12-27 06:44:54 +00:00
function log_msg(...)
2022-11-02 21:45:36 +00:00
if not opts.p then
io.stderr:write(table.concat({...}, " ") .. "\n")
end
2011-08-13 04:07:44 +00:00
end
2014-06-18 16:23:41 +00:00
local moonc = require("moonscript.cmd.moonc")
2015-03-12 08:09:17 +00:00
local util = require "moonscript.util"
2014-06-18 16:23:41 +00:00
local normalize_dir = moonc.normalize_dir
2014-06-18 16:48:25 +00:00
local compile_and_write = moonc.compile_and_write
2014-06-18 17:14:26 +00:00
local path_to_target = moonc.path_to_target
2011-08-13 04:07:44 +00:00
2014-06-18 16:48:25 +00:00
local function scan_directory(root, collected)
2022-11-02 21:45:36 +00:00
root = normalize_dir(root)
collected = collected or {}
for fname in lfs.dir(root) do
if not fname:match("^%.") then
local full_path = root..fname
if lfs.attributes(full_path, "mode") == "directory" then
scan_directory(full_path, collected)
elseif fname:match("%.moon$") then
table.insert(collected, full_path)
end
end
end
return collected
end
2014-06-18 16:48:25 +00:00
local function remove_dups(tbl, key_fn)
2022-11-02 21:45:36 +00:00
local hash = {}
local final = {}
for _, v in ipairs(tbl) do
local dup_key = key_fn and key_fn(v) or v
if not hash[dup_key] then
table.insert(final, v)
hash[dup_key] = true
end
end
return final
end
2014-06-17 17:45:42 +00:00
-- creates tuples of input and target
2014-06-18 16:48:25 +00:00
local function get_files(fname, files)
2022-11-02 21:45:36 +00:00
files = files or {}
if lfs.attributes(fname, "mode") == "directory" then
for _, sub_fname in ipairs(scan_directory(fname)) do
table.insert(files, {
sub_fname,
path_to_target(sub_fname, opts.output_to, fname)
})
end
else
table.insert(files, {
fname,
path_to_target(fname, opts.output_to)
})
end
return files
end
2012-01-22 19:53:13 +00:00
if read_stdin then
2022-11-02 21:45:36 +00:00
local parse = require "moonscript.parse"
local compile = require "moonscript.compile"
2014-06-18 17:26:20 +00:00
2022-11-02 21:45:36 +00:00
local text = io.stdin:read("*a")
local tree, err = parse.string(text)
2014-06-18 17:26:20 +00:00
2022-11-02 21:45:36 +00:00
if not tree then error(err) end
local code, err, pos = compile.tree(tree)
2012-01-22 19:53:13 +00:00
2022-11-02 21:45:36 +00:00
if not code then
error(compile.format_error(err, pos, text))
end
2012-01-22 19:53:13 +00:00
2022-11-02 21:45:36 +00:00
print(code)
os.exit()
2012-01-22 19:53:13 +00:00
end
local inputs = opts["file/directory"]
local files = {}
for _, input in ipairs(inputs) do
2022-11-02 21:45:36 +00:00
get_files(input, files)
end
files = remove_dups(files, function(f)
2022-11-02 21:45:36 +00:00
return f[2]
end)
2014-06-17 17:45:42 +00:00
-- returns an iterator that returns files that have been updated
2014-06-18 16:48:25 +00:00
local function create_watcher(files)
2022-11-02 21:45:36 +00:00
local watchers = require("moonscript.cmd.watchers")
2022-11-02 21:45:36 +00:00
if watchers.InotifyWacher:available() then
return watchers.InotifyWacher(files):each_update()
end
2022-11-02 21:45:36 +00:00
return watchers.SleepWatcher(files):each_update()
end
if opts.watch then
2022-11-02 21:45:36 +00:00
-- build function to check for lint or compile in watch
local handle_file
if opts.lint then
local lint = require "moonscript.cmd.lint"
handle_file = lint.lint_file
else
handle_file = compile_and_write
end
local watcher = create_watcher(files)
-- catches interrupt error for ctl-c
local protected = function()
local status, file = true, watcher()
if status then
return file
elseif file ~= "interrupted!" then
error(file)
end
end
for fname in protected do
local target = path_to_target(fname, opts.t)
if opts.o then
target = opts.o
end
local success, err = handle_file(fname, target)
if opts.lint then
if success then
io.stderr:write(success .. "\n\n")
elseif err then
io.stderr:write(fname .. "\n" .. err .. "\n\n")
end
elseif not success then
io.stderr:write(table.concat({
"",
"Error: " .. fname,
err,
"\n",
}, "\n"))
elseif success == "build" then
log_msg("Built", fname, "->", target)
end
end
io.stderr:write("\nQuitting...\n")
elseif opts.lint then
2022-11-02 21:45:36 +00:00
local has_linted_with_error;
local lint = require "moonscript.cmd.lint"
for _, tuple in pairs(files) do
local fname = tuple[1]
local res, err = lint.lint_file(fname)
if res then
has_linted_with_error = true
io.stderr:write(res .. "\n\n")
elseif err then
has_linted_with_error = true
io.stderr:write(fname .. "\n" .. err.. "\n\n")
end
end
if has_linted_with_error then
os.exit(1)
end
else
2022-11-02 21:45:36 +00:00
for _, tuple in ipairs(files) do
local fname, target = util.unpack(tuple)
if opts.o then
target = opts.o
end
local success, err = compile_and_write(fname, target, {
print = opts.p,
fname = fname,
benchmark = opts.b,
show_posmap = opts.X,
show_parse_tree = opts.T,
transform_module = opts.transform
})
if not success then
io.stderr:write(fname .. "\t" .. err .. "\n")
os.exit(1)
end
end
end