From 4c4f6de43eded174129251255a932fd13e854c8c Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Sun, 11 Dec 2011 16:57:07 -0800 Subject: [PATCH] added splat.moon --- bin/splat.moon | 80 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 80 insertions(+) create mode 100755 bin/splat.moon diff --git a/bin/splat.moon b/bin/splat.moon new file mode 100755 index 0000000..2a230d6 --- /dev/null +++ b/bin/splat.moon @@ -0,0 +1,80 @@ +#!/usr/bin/env moon + +-- concatenate a collection of lua modules into one + +require "lfs" + +import insert, concat from table +import dump from require "moonscript.util" + +if not arg[1] + print "usage: splat directory" + os.exit! + +dir = arg[1] + +normalize = (path) -> + path\match("(.-)/*$").."/" + +scan_directory = (root, patt, collected={}) -> + root = normalize root + for fname in lfs.dir root + if not fname\match "^%." + full_path = root..fname + + if lfs.attributes(full_path, "mode") == "directory" + scan_directory full_path, patt, collected + else + if full_path\match patt + insert collected, full_path + + collected + +files = scan_directory dir, "%.lua$" + +modules = {} + +path_to_module_name = (path) -> + (path\match("(.-)%.lua")\gsub("/", ".")) + +each_line = (text) -> + import yield from coroutine + coroutine.wrap -> + start = 1 + while true + pos, after = text\find "\n", start, true + break if not pos + yield text\sub start, pos - 1 + start = after + 1 + yield text\sub start, #text + nil + +write_module = (name, text) -> + print "package.preload['"..name.."'] = function()" + for line in each_line text + print " "..line + print "end" + +modules = {} +chunks = for path in *files + module_name = path_to_module_name path + content = io.open(path)\read"*a" + modules[module_name] = true + {module_name, content} + +for chunk in *chunks + name, content = unpack chunk + base, init = name\match"(.-)%.init" + if base and not modules[base] then + modules[base] = true + name = base + write_module name, content + +default_name = if arg[2] + arg[2] +else + dir\gsub("/", ".")\gsub("%.$", "") + +if modules[default_name] + print ([[return package.preload["%s"]()]])\format default_name +