2011-08-13 04:07:44 +00:00
|
|
|
#!/usr/bin/env lua
|
2011-05-29 16:33:25 +00:00
|
|
|
|
|
|
|
module("moonscript", package.seeall)
|
|
|
|
|
|
|
|
require "moonscript.parse"
|
2011-07-04 16:20:46 +00:00
|
|
|
require "moonscript.compile"
|
2012-11-28 08:24:58 +00:00
|
|
|
local util = require "moonscript.util"
|
2011-05-29 16:33:25 +00:00
|
|
|
|
|
|
|
require "alt_getopt"
|
|
|
|
require "lfs"
|
|
|
|
|
2012-10-30 07:31:56 +00:00
|
|
|
local opts, ind = alt_getopt.get_opts(arg, "vhwt:pTXb", {
|
2011-08-13 04:07:44 +00:00
|
|
|
print = "p", tree = "T", version = "v", help = "h"
|
|
|
|
})
|
2011-05-29 16:33:25 +00:00
|
|
|
|
2012-01-22 19:53:13 +00:00
|
|
|
local read_stdin = arg[1] == "--"
|
|
|
|
|
2011-10-09 08:59:50 +00:00
|
|
|
local polling_rate = 1.0
|
|
|
|
|
2011-08-13 04:07:44 +00:00
|
|
|
local help = [[Usage: %s [options] files...
|
2011-05-29 16:33:25 +00:00
|
|
|
|
|
|
|
-h Print this message
|
|
|
|
-w Watch file/directory
|
|
|
|
-t path Specify where to place compiled files
|
2011-08-13 04:07:44 +00:00
|
|
|
-p Write output to standard out
|
|
|
|
-T Write parse tree instead of code (to stdout)
|
2012-10-30 07:31:56 +00:00
|
|
|
-X Write line rewrite map instead of code (to stdout)
|
2011-08-13 04:07:44 +00:00
|
|
|
-b Dump parse and compile time (doesn't write output)
|
2011-07-19 06:53:43 +00:00
|
|
|
-v Print version
|
2012-01-22 19:53:13 +00:00
|
|
|
|
|
|
|
-- Read from standard in, print to standard out
|
|
|
|
(Must be first and only argument)
|
2011-05-29 16:33:25 +00:00
|
|
|
]]
|
|
|
|
|
2011-07-19 06:53:43 +00:00
|
|
|
if opts.v then
|
|
|
|
local v = require "moonscript.version"
|
|
|
|
v.print_version()
|
|
|
|
os.exit()
|
|
|
|
end
|
|
|
|
|
2011-05-29 16:33:25 +00:00
|
|
|
function print_help(err)
|
|
|
|
if err then print("Error: "..err) end
|
|
|
|
print(help:format(arg[0]))
|
|
|
|
os.exit()
|
|
|
|
end
|
|
|
|
|
|
|
|
function mkdir(path)
|
|
|
|
local chunks = util.split(path, "/")
|
|
|
|
local accum
|
|
|
|
|
|
|
|
for _, dir in ipairs(chunks) do
|
|
|
|
accum = accum and accum.."/"..dir or dir
|
|
|
|
lfs.mkdir(accum)
|
|
|
|
end
|
|
|
|
|
|
|
|
return lfs.attributes(path, "mode")
|
|
|
|
end
|
|
|
|
|
|
|
|
function normalize(path)
|
|
|
|
return path:match("(.-)/*$").."/"
|
|
|
|
end
|
|
|
|
|
|
|
|
function get_dir(fname)
|
|
|
|
return fname:match("^(.-)[^/]*$")
|
|
|
|
end
|
|
|
|
|
|
|
|
-- convert .moon to .lua
|
|
|
|
function convert_path(path)
|
|
|
|
return (path:gsub("%.moon$", ".lua"))
|
|
|
|
end
|
|
|
|
|
2011-08-13 04:07:44 +00:00
|
|
|
function msg(...)
|
|
|
|
if not opts.p then
|
|
|
|
print(...)
|
2011-05-29 16:33:25 +00:00
|
|
|
end
|
2011-08-13 04:07:44 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
local gettime = nil
|
|
|
|
if opts.b then
|
|
|
|
pcall(function()
|
|
|
|
require "socket"
|
|
|
|
gettime = socket.gettime
|
|
|
|
end)
|
|
|
|
|
|
|
|
function format_time(time)
|
|
|
|
return ("%.3fms"):format(time*1000)
|
|
|
|
end
|
|
|
|
if not gettime then
|
|
|
|
print_help"LuaSocket needed for benchmark"
|
|
|
|
end
|
|
|
|
else
|
|
|
|
gettime = function() return 0 end
|
|
|
|
end
|
|
|
|
|
|
|
|
function write_file(fname, code)
|
|
|
|
if opts.p then
|
|
|
|
if code ~= "" then print(code) end
|
|
|
|
else
|
|
|
|
mkdir(get_dir(fname))
|
|
|
|
local out_f = io.open(fname, "w")
|
|
|
|
if not out_f then
|
|
|
|
return nil, "Failed to write output: "..fname
|
|
|
|
end
|
|
|
|
|
|
|
|
out_f:write(code.."\n")
|
|
|
|
out_f:close()
|
|
|
|
end
|
|
|
|
return true
|
|
|
|
end
|
2011-05-29 16:33:25 +00:00
|
|
|
|
2011-08-13 04:07:44 +00:00
|
|
|
function compile_file(text, fname)
|
|
|
|
local parse_time = gettime()
|
2011-05-29 16:33:25 +00:00
|
|
|
local tree, err = parse.string(text)
|
2011-08-13 04:07:44 +00:00
|
|
|
parse_time = gettime() - parse_time
|
|
|
|
|
2011-05-29 16:33:25 +00:00
|
|
|
if not tree then
|
|
|
|
return nil, err
|
|
|
|
end
|
|
|
|
|
2011-08-13 04:07:44 +00:00
|
|
|
if opts.T then
|
|
|
|
opts.p = true
|
|
|
|
dump.tree(tree)
|
|
|
|
return ""
|
|
|
|
else
|
|
|
|
local compile_time = gettime()
|
2012-10-30 07:31:56 +00:00
|
|
|
local code, posmap_or_err, err_pos = compile.tree(tree)
|
2011-08-13 04:07:44 +00:00
|
|
|
compile_time = gettime() - compile_time
|
2012-10-30 07:31:56 +00:00
|
|
|
|
2011-08-13 04:07:44 +00:00
|
|
|
if not code then
|
2012-10-30 07:31:56 +00:00
|
|
|
return nil, compile.format_error(posmap_or_err, err_pos, text)
|
|
|
|
end
|
|
|
|
|
|
|
|
if opts.X then
|
|
|
|
opts.p = true
|
|
|
|
print("Pos", "Lua", ">>", "Moon")
|
|
|
|
print(util.debug_posmap(posmap_or_err, text, code))
|
|
|
|
return ""
|
2011-08-13 04:07:44 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
if opts.b then
|
|
|
|
opts.p = true
|
|
|
|
return table.concat({
|
|
|
|
fname,
|
|
|
|
"Parse time \t" .. format_time(parse_time),
|
|
|
|
"Compile time\t" .. format_time(compile_time),
|
|
|
|
""
|
|
|
|
}, "\n")
|
|
|
|
end
|
|
|
|
|
|
|
|
return code
|
2011-06-03 03:55:33 +00:00
|
|
|
end
|
2011-08-13 04:07:44 +00:00
|
|
|
end
|
2011-05-29 16:33:25 +00:00
|
|
|
|
2011-08-13 04:07:44 +00:00
|
|
|
function compile_and_write(from, to)
|
|
|
|
local f = io.open(from)
|
|
|
|
if not f then
|
|
|
|
return nil, "Can't find file"
|
2011-05-29 16:33:25 +00:00
|
|
|
end
|
2011-08-13 04:07:44 +00:00
|
|
|
local text = f:read("*a")
|
2011-05-29 16:33:25 +00:00
|
|
|
|
2011-08-13 04:07:44 +00:00
|
|
|
local code, err = compile_file(text, from)
|
|
|
|
if not code then
|
|
|
|
return nil, err
|
|
|
|
end
|
|
|
|
|
|
|
|
return write_file(to, code)
|
2011-05-29 16:33:25 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
function scan_directory(root, collected)
|
|
|
|
root = normalize(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)
|
|
|
|
end
|
|
|
|
|
|
|
|
if fname:match(".moon") then
|
|
|
|
table.insert(collected, full_path)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
return collected
|
|
|
|
end
|
|
|
|
|
|
|
|
function append(a, b)
|
|
|
|
for _, v in ipairs(b) do
|
|
|
|
table.insert(a, v)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function remove_dups(tbl)
|
|
|
|
local hash = {}
|
|
|
|
local final = {}
|
|
|
|
|
|
|
|
for _, v in ipairs(tbl) do
|
|
|
|
if not hash[v] then
|
|
|
|
table.insert(final, v)
|
|
|
|
hash[v] = true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
return final
|
|
|
|
end
|
|
|
|
|
|
|
|
function get_files(fname, files)
|
|
|
|
files = files or {}
|
|
|
|
|
|
|
|
if lfs.attributes(fname, "mode") == "directory" then
|
|
|
|
append(files, scan_directory(fname))
|
|
|
|
else
|
|
|
|
table.insert(files, "./"..fname)
|
|
|
|
end
|
|
|
|
|
|
|
|
return files
|
|
|
|
end
|
|
|
|
|
|
|
|
if opts.h then print_help() end
|
|
|
|
|
2012-01-22 19:53:13 +00:00
|
|
|
if read_stdin then
|
|
|
|
local text = io.stdin:read("*a")
|
|
|
|
local tree, err = parse.string(text)
|
|
|
|
if not tree then error(err) end
|
|
|
|
local code, err, pos = compile.tree(tree)
|
|
|
|
|
|
|
|
if not code then
|
|
|
|
error(compile.format_error(err, pos, text))
|
|
|
|
end
|
|
|
|
|
|
|
|
print(code)
|
|
|
|
os.exit()
|
|
|
|
end
|
|
|
|
|
2011-05-29 16:33:25 +00:00
|
|
|
local inputs = {}
|
|
|
|
for i = ind, #arg do
|
|
|
|
table.insert(inputs, arg[i])
|
|
|
|
end
|
|
|
|
|
|
|
|
if #inputs == 0 then
|
|
|
|
print_help("No files specified")
|
|
|
|
end
|
|
|
|
|
|
|
|
local target_dir = "."
|
|
|
|
if opts.t then
|
|
|
|
if mkdir(opts.t) ~= "directory" then
|
|
|
|
print_help("Invalid target dir")
|
|
|
|
end
|
|
|
|
target_dir = opts.t
|
|
|
|
end
|
|
|
|
|
|
|
|
target_dir = target_dir.."/"
|
|
|
|
|
|
|
|
local files = {}
|
|
|
|
for _, input in ipairs(inputs) do
|
|
|
|
get_files(input, files)
|
|
|
|
end
|
|
|
|
|
|
|
|
files = remove_dups(files)
|
|
|
|
|
2011-10-09 08:59:50 +00:00
|
|
|
function get_sleep_func()
|
|
|
|
local sleep
|
|
|
|
if not pcall(function()
|
|
|
|
require "socket"
|
|
|
|
sleep = socket.sleep
|
|
|
|
end) then
|
|
|
|
sleep = moonscript._sleep
|
|
|
|
end
|
2011-12-01 03:25:41 +00:00
|
|
|
if not sleep then
|
|
|
|
error("Missing sleep function; install LuaSocket")
|
|
|
|
end
|
2011-10-09 08:59:50 +00:00
|
|
|
return sleep
|
|
|
|
end
|
|
|
|
|
2011-10-09 21:52:50 +00:00
|
|
|
|
|
|
|
function plural(count, word)
|
|
|
|
if count ~= 1 then
|
|
|
|
word = word .. "s"
|
|
|
|
end
|
|
|
|
return table.concat({count, word}, " ")
|
|
|
|
end
|
|
|
|
|
2011-10-09 08:59:50 +00:00
|
|
|
-- returns an iterator that returns files that have been updated
|
|
|
|
function create_watcher(files)
|
2011-12-01 03:25:41 +00:00
|
|
|
local msg = "Starting watch loop (Ctrl-C to exit)"
|
2011-10-09 08:59:50 +00:00
|
|
|
|
2011-05-29 16:33:25 +00:00
|
|
|
local inotify
|
2011-10-09 09:03:09 +00:00
|
|
|
pcall(function()
|
2011-05-29 16:33:25 +00:00
|
|
|
inotify = require "inotify"
|
2011-10-09 09:03:09 +00:00
|
|
|
end)
|
2011-05-29 16:33:25 +00:00
|
|
|
|
2011-10-09 09:03:09 +00:00
|
|
|
if inotify then
|
2011-10-09 08:59:50 +00:00
|
|
|
local dirs = {}
|
|
|
|
for _, fname in ipairs(files) do
|
|
|
|
table.insert(dirs, get_dir(fname))
|
|
|
|
end
|
|
|
|
dirs = remove_dups(dirs)
|
2011-05-29 16:33:25 +00:00
|
|
|
|
2011-10-09 08:59:50 +00:00
|
|
|
return coroutine.wrap(function()
|
2011-10-09 21:52:50 +00:00
|
|
|
print(("%s with inotify [%s]"):format(msg, plural(#dirs, "dir")))
|
2011-05-29 16:33:25 +00:00
|
|
|
|
2011-10-09 08:59:50 +00:00
|
|
|
local wd_table = {}
|
2012-03-01 03:32:10 +00:00
|
|
|
local handle = inotify.init()
|
2011-10-09 08:59:50 +00:00
|
|
|
for _, dir in ipairs(dirs) do
|
2012-03-01 03:32:10 +00:00
|
|
|
local wd = handle:addwatch(dir, inotify.IN_CLOSE_WRITE)
|
2011-10-09 08:59:50 +00:00
|
|
|
wd_table[wd] = dir
|
|
|
|
end
|
2011-05-29 16:33:25 +00:00
|
|
|
|
2011-10-09 08:59:50 +00:00
|
|
|
while true do
|
2011-10-09 21:52:50 +00:00
|
|
|
local events = handle:read()
|
2011-10-09 08:59:50 +00:00
|
|
|
if events then
|
|
|
|
for _, ev in ipairs(events) do
|
|
|
|
local fname = wd_table[ev.wd]..ev.name
|
|
|
|
if fname:match("%.moon$") then
|
|
|
|
coroutine.yield(fname)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
else
|
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end)
|
|
|
|
else
|
|
|
|
-- poll the filesystem instead
|
|
|
|
local sleep = get_sleep_func()
|
|
|
|
return coroutine.wrap(function()
|
2011-10-09 21:52:50 +00:00
|
|
|
print(("%s with polling [%s]"):format(msg, plural(#files, "file")))
|
2011-10-09 08:59:50 +00:00
|
|
|
|
|
|
|
local mod_time = {}
|
|
|
|
while true do
|
|
|
|
for _, file in ipairs(files) do
|
|
|
|
local time = lfs.attributes(file, "modification")
|
|
|
|
if not mod_time[file] then
|
|
|
|
mod_time[file] = time
|
2011-05-30 08:23:35 +00:00
|
|
|
else
|
2011-10-09 08:59:50 +00:00
|
|
|
if time ~= mod_time[file] then
|
|
|
|
if time > mod_time[file] then
|
|
|
|
coroutine.yield(file)
|
|
|
|
mod_time[file] = time
|
|
|
|
end
|
|
|
|
end
|
2011-05-30 08:23:35 +00:00
|
|
|
end
|
2011-05-29 16:33:25 +00:00
|
|
|
end
|
2011-10-09 08:59:50 +00:00
|
|
|
sleep(polling_rate)
|
2011-05-29 16:33:25 +00:00
|
|
|
end
|
2011-10-09 08:59:50 +00:00
|
|
|
end)
|
2011-05-29 16:33:25 +00:00
|
|
|
end
|
2011-10-09 08:59:50 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
if opts.w then
|
|
|
|
local watcher = create_watcher(files)
|
|
|
|
-- catches interrupt error for ctl-c
|
|
|
|
local protected = function()
|
|
|
|
local status, file = pcall(watcher)
|
2011-12-01 03:25:41 +00:00
|
|
|
if status then
|
|
|
|
return file
|
|
|
|
elseif file ~= "interrupted!" then
|
|
|
|
error(file)
|
|
|
|
end
|
2011-10-09 08:59:50 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
for fname in protected do
|
|
|
|
local target = target_dir..convert_path(fname)
|
|
|
|
local success, err = compile_and_write(fname, target)
|
|
|
|
if not success then
|
|
|
|
print()
|
|
|
|
print("Error:", fname)
|
|
|
|
print(err)
|
|
|
|
print()
|
|
|
|
else
|
|
|
|
msg("Built:", fname, "->", target)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
print "\nQuitting..."
|
2011-05-29 16:33:25 +00:00
|
|
|
else
|
|
|
|
for _, fname in ipairs(files) do
|
2011-08-13 04:07:44 +00:00
|
|
|
local success, err = compile_and_write(fname, target_dir..convert_path(fname))
|
2011-05-29 16:33:25 +00:00
|
|
|
if not success then
|
|
|
|
print(fname, err)
|
2011-09-19 06:23:23 +00:00
|
|
|
os.exit(1)
|
2011-05-30 08:23:35 +00:00
|
|
|
else
|
2011-08-13 04:07:44 +00:00
|
|
|
msg("Built", fname)
|
2011-05-29 16:33:25 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|