1
1
# !/ usr/ bin/ env moon
2
+ argparse = require " argparse"
2
3
3
- -- concatenate a collection of lua modules into one
4
+ parser = argparse " splat.moon" , " Concatenate a collection of Lua modules into a single file"
5
+ parser\ option( " --load -l" , " Module names that will be load on require" ) \ count " *"
4
6
5
- lfs = require " lfs"
6
- alt_getopt = require " alt_getopt"
7
-
8
- import insert, concat from table
9
- import dump, split from require " moonscript.util"
10
-
11
- opts, ind = alt_getopt. get_opts arg, " l:" , {
12
- load : " l"
13
- }
7
+ parser\ argument( " directories" , " Directories to scan for Lua modules" ) \ args " +"
14
8
15
- if not arg[ ind]
16
- print " usage: splat [-l module_names] directory [directories...]"
17
- os.exit !
18
-
19
- dirs = [ a for a in * arg[ ind, ]]
9
+ args = parser\ parse [ v for _, v in ipairs _G . arg]
10
+ dirs = args. directories
20
11
21
12
normalize = ( path) ->
22
13
path\ match( " (.-)/*$" ) .. " /"
23
14
15
+ lfs = require " lfs"
24
16
scan_directory = ( root, patt, collected= {} ) ->
25
17
root = normalize root
26
18
for fname in lfs. dir root
@@ -31,23 +23,22 @@ scan_directory = (root, patt, collected={}) ->
31
23
scan_directory full_path, patt, collected
32
24
else
33
25
if full_path\ match patt
34
- insert collected, full_path
26
+ table. insert collected, full_path
35
27
36
28
collected
37
29
38
30
path_to_module_name = ( path) ->
39
31
( path\ match( " (.-)%.lua" ) \ gsub( " /" , " ." ))
40
32
41
33
each_line = ( text) ->
42
- import yield from coroutine
43
34
coroutine.wrap ->
44
35
start = 1
45
36
while true
46
37
pos, after = text\ find " \n " , start, true
47
38
break if not pos
48
- yield text\ sub start, pos - 1
39
+ coroutine. yield text\ sub start, pos - 1
49
40
start = after + 1
50
- yield text\ sub start, # text
41
+ coroutine. yield text\ sub start, # text
51
42
nil
52
43
53
44
write_module = ( name, text) ->
@@ -73,8 +64,7 @@ for dir in *dirs
73
64
name = base
74
65
write_module name, content
75
66
76
- if opts. l
77
- for module_name in * split opts. l, " ,"
78
- if modules[ module_name]
79
- print ( [[ package.preload["%s"]()]] ) \ format module_name
67
+ for module_name in * args. load
68
+ if modules[ module_name]
69
+ print ( [[ package.preload["%s"]()]] ) \ format module_name
80
70
0 commit comments