Skip to content

Commit

Permalink
remove alt_getopt from splat
Browse files Browse the repository at this point in the history
  • Loading branch information
leafo committed Nov 23, 2024
1 parent d5341c9 commit 60094ca
Showing 1 changed file with 13 additions and 23 deletions.
36 changes: 13 additions & 23 deletions bin/splat.moon
Original file line number Diff line number Diff line change
@@ -1,26 +1,18 @@
#!/usr/bin/env moon
argparse = require "argparse"

-- concatenate a collection of lua modules into one
parser = argparse "splat.moon", "Concatenate a collection of Lua modules into a single file"
parser\option("--load -l", "Module names that will be load on require")\count "*"

lfs = require "lfs"
alt_getopt = require "alt_getopt"

import insert, concat from table
import dump, split from require "moonscript.util"

opts, ind = alt_getopt.get_opts arg, "l:", {
load: "l"
}
parser\argument("directories", "Directories to scan for Lua modules")\args "+"

if not arg[ind]
print "usage: splat [-l module_names] directory [directories...]"
os.exit!

dirs = [a for a in *arg[ind,]]
args = parser\parse [v for _, v in ipairs _G.arg]
dirs = args.directories

normalize = (path) ->
path\match("(.-)/*$").."/"

lfs = require "lfs"
scan_directory = (root, patt, collected={}) ->
root = normalize root
for fname in lfs.dir root
Expand All @@ -31,23 +23,22 @@ scan_directory = (root, patt, collected={}) ->
scan_directory full_path, patt, collected
else
if full_path\match patt
insert collected, full_path
table.insert collected, full_path

collected

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
coroutine.yield text\sub start, pos - 1
start = after + 1
yield text\sub start, #text
coroutine.yield text\sub start, #text
nil

write_module = (name, text) ->
Expand All @@ -73,8 +64,7 @@ for dir in *dirs
name = base
write_module name, content

if opts.l
for module_name in *split opts.l, ","
if modules[module_name]
print ([[package.preload["%s"]()]])\format module_name
for module_name in *args.load
if modules[module_name]
print ([[package.preload["%s"]()]])\format module_name

0 comments on commit 60094ca

Please sign in to comment.