Skip to content

Commit 4c4f6de

Browse files
committed
added splat.moon
1 parent 07fb8dc commit 4c4f6de

File tree

1 file changed

+80
-0
lines changed

1 file changed

+80
-0
lines changed

bin/splat.moon

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
#!/usr/bin/env moon
2+
3+
-- concatenate a collection of lua modules into one
4+
5+
require "lfs"
6+
7+
import insert, concat from table
8+
import dump from require "moonscript.util"
9+
10+
if not arg[1]
11+
print "usage: splat directory"
12+
os.exit!
13+
14+
dir = arg[1]
15+
16+
normalize = (path) ->
17+
path\match("(.-)/*$").."/"
18+
19+
scan_directory = (root, patt, collected={}) ->
20+
root = normalize root
21+
for fname in lfs.dir root
22+
if not fname\match "^%."
23+
full_path = root..fname
24+
25+
if lfs.attributes(full_path, "mode") == "directory"
26+
scan_directory full_path, patt, collected
27+
else
28+
if full_path\match patt
29+
insert collected, full_path
30+
31+
collected
32+
33+
files = scan_directory dir, "%.lua$"
34+
35+
modules = {}
36+
37+
path_to_module_name = (path) ->
38+
(path\match("(.-)%.lua")\gsub("/", "."))
39+
40+
each_line = (text) ->
41+
import yield from coroutine
42+
coroutine.wrap ->
43+
start = 1
44+
while true
45+
pos, after = text\find "\n", start, true
46+
break if not pos
47+
yield text\sub start, pos - 1
48+
start = after + 1
49+
yield text\sub start, #text
50+
nil
51+
52+
write_module = (name, text) ->
53+
print "package.preload['"..name.."'] = function()"
54+
for line in each_line text
55+
print " "..line
56+
print "end"
57+
58+
modules = {}
59+
chunks = for path in *files
60+
module_name = path_to_module_name path
61+
content = io.open(path)\read"*a"
62+
modules[module_name] = true
63+
{module_name, content}
64+
65+
for chunk in *chunks
66+
name, content = unpack chunk
67+
base, init = name\match"(.-)%.init"
68+
if base and not modules[base] then
69+
modules[base] = true
70+
name = base
71+
write_module name, content
72+
73+
default_name = if arg[2]
74+
arg[2]
75+
else
76+
dir\gsub("/", ".")\gsub("%.$", "")
77+
78+
if modules[default_name]
79+
print ([[return package.preload["%s"]()]])\format default_name
80+

0 commit comments

Comments
 (0)