11# !/ usr/ bin/ env moon
2+ argparse = require " argparse"
23
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 " *"
46
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 " +"
148
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
2011
2112normalize = ( path) ->
2213 path\ match( " (.-)/*$" ) .. " /"
2314
15+ lfs = require " lfs"
2416scan_directory = ( root, patt, collected= {} ) ->
2517 root = normalize root
2618 for fname in lfs. dir root
@@ -31,23 +23,22 @@ scan_directory = (root, patt, collected={}) ->
3123 scan_directory full_path, patt, collected
3224 else
3325 if full_path\ match patt
34- insert collected, full_path
26+ table. insert collected, full_path
3527
3628 collected
3729
3830path_to_module_name = ( path) ->
3931 ( path\ match( " (.-)%.lua" ) \ gsub( " /" , " ." ))
4032
4133each_line = ( text) ->
42- import yield from coroutine
4334 coroutine.wrap ->
4435 start = 1
4536 while true
4637 pos, after = text\ find " \n " , start, true
4738 break if not pos
48- yield text\ sub start, pos - 1
39+ coroutine. yield text\ sub start, pos - 1
4940 start = after + 1
50- yield text\ sub start, # text
41+ coroutine. yield text\ sub start, # text
5142 nil
5243
5344write_module = ( name, text) ->
@@ -73,8 +64,7 @@ for dir in *dirs
7364 name = base
7465 write_module name, content
7566
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
8070
0 commit comments