Skip to content

Commit 34917d9

Browse files
committed
bin/: Move to mpeterv/argparse
1 parent 1294632 commit 34917d9

File tree

4 files changed

+135
-102
lines changed

4 files changed

+135
-102
lines changed

bin/compiler/source.lua

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,30 @@
88
-- -p | Write output to stdout
99
-- -h | Print help information
1010

11+
local argparse = require("argparse")
1112
local compiler = require("fusion.core.compilers.source")
1213
local lfs = require("lfs")
1314

15+
local argparser = argparse() {
16+
name = "fusionc-source";
17+
description = "Generate a Lua file from FusionScript code";
18+
epilog = "Fur more info, see https://fusionscript.info";
19+
}
20+
21+
argparser:argument("file", "Files or directories to compile"):args("+")
22+
argparser:mutex(
23+
argparser:flag("-p --print", "Print compiled output"),
24+
argparser:flag("-q --quiet", "Don't print status messages")
25+
)
26+
1427
local function walk(file_func, dir)
1528
for item in lfs.dir(dir) do
1629
if item:sub(1, 1) == "." then -- luacheck: ignore
1730
-- pass
1831
elseif lfs.attributes(dir .. "/" .. item, "mode") == "directory" then
1932
walk(file_func, dir .. "/" .. item)
2033
else
21-
print(pcall(file_func, dir .. "/" .. item))
34+
pcall(file_func, dir .. "/" .. item)
2235
end
2336
end
2437
end
@@ -28,6 +41,9 @@ local function process(file, does_output)
2841
return walk(process, file)
2942
end
3043
local base = file:match("^(.+)%.fuse$")
44+
if not base then
45+
return
46+
end
3147
local output = compiler.read_file(file)
3248
local output_file = io.open(base .. ".lua", "w")
3349
output_file:write(output .. "\n")
@@ -37,29 +53,13 @@ local function process(file, does_output)
3753
end
3854
end
3955

40-
local args = {...}
41-
local arg_index = 1
42-
while arg_index <= #args do
43-
local _arg = args[arg_index]
44-
if _arg == "-p" then
45-
arg_index = arg_index + 1
46-
for i=arg_index, #args do
47-
io.write(compiler.read_file(args[i]))
48-
end
49-
break
50-
elseif _arg == "--help" or _arg == "-h" then
51-
local program = arg[0]:match(".+/(.-)$") or arg[0]
52-
local usage = {
53-
("Usage: %s [OPTIONS] [FILE]"):format(program);
54-
(" or: %s [FILE/DIRECTORY]"):format(program);
55-
("");
56-
("\t-p | Write output to stdout");
57-
("\t-h | Print help information")
58-
}
59-
print(table.concat(usage, "\n"))
60-
break
61-
else
62-
process(_arg)
63-
arg_index = arg_index + 1
56+
local args = argparser:parse()
57+
if args.print then -- two loops are written to run the check only once
58+
for i, file in ipairs(args.file) do -- luacheck: ignore 213
59+
io.write(compiler.read_file(file))
60+
end
61+
else
62+
for i, file in ipairs(args.file) do -- luacheck: ignore 213
63+
process(file, not args.quiet)
6464
end
6565
end

bin/interpreter/source.lua

Lines changed: 45 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -8,63 +8,58 @@
88
-- --metadata <package> | Print metadata for package
99
-- -m <package> | Run <package>.main module
1010
-- -h | Print help information
11+
12+
local argparse = require("argparse")
1113
local compiler = require("fusion.core.compilers.source")
1214
compiler.inject_loader()
1315
compiler.inject_extensions()
1416

17+
local argparser = argparse() {
18+
name = "fusion-source";
19+
description = "Run FusionScript code with the Lua VM";
20+
epilog = "For more info, see https://fusionscript.info";
21+
}
22+
23+
argparser:mutex(
24+
argparser:flag("--metadata", "Print metadata information for a package"),
25+
argparser:flag("--package", "Run <package>.main module")
26+
)
27+
28+
argparser:argument("file", "File to run")
29+
30+
local args = argparser:parse()
31+
1532
_G.compiler = compiler
1633

17-
local arg_index = 1
18-
while arg_index <= #arg do
19-
if arg[arg_index] == "--metadata" then -- return metadata from module
20-
assert(arg[arg_index + 1], "missing argument to --metadata: module")
21-
local ok, module = pcall(require, arg[arg_index + 1] ..
22-
".metadata")
23-
if not ok then
24-
error("Could not find module metadata for package: " ..
25-
arg[arg_index + 1] .. "\n" .. module)
26-
else
27-
local function check(name)
28-
assert(module[name], "Missing field: " .. name)
29-
end
30-
local opts = {"version", "description", "author", "copyright",
31-
"license"}
32-
for _, name in ipairs(opts) do
33-
check(name)
34-
end
35-
for _, name in ipairs(opts) do
36-
local value = module[name]
37-
local _type = type(value)
38-
if _type == "string" then
39-
print(("['%s'] = %q"):format(name, value))
40-
else
41-
print(("['%s'] = %s"):format(name, tostring(value)))
42-
end
34+
if args.metadata then
35+
local ok, module = pcall(require, args.file .. ".metadata")
36+
if not ok then
37+
error("Could not find module metadata for package: " .. args.file ..
38+
"\n" .. module)
39+
else
40+
local function check(name)
41+
assert(module[name], "Missing field: " .. name)
42+
end
43+
local opts = {"version", "description", "author", "copyright",
44+
"license"}
45+
for _, name in ipairs(opts) do
46+
check(name)
47+
end
48+
for _, name in ipairs(opts) do
49+
local value = module[name]
50+
local _type = type(value)
51+
if _type == "string" then
52+
print(("['%s'] = %q"):format(name, value))
53+
else
54+
print(("['%s'] = %s"):format(name, tostring(value)))
4355
end
44-
break
4556
end
46-
elseif arg[arg_index] == "-m" then -- load <module>.main and exit
47-
local module = arg[arg_index + 1]
48-
assert(module, "missing argument to -m: module")
49-
require(module .. ".main")
50-
break
51-
elseif arg[arg_index] == "-h" or arg[arg_index] == "--help" then -- print help
52-
local program = arg[0]:match(".+/(.-)$") or arg[0]
53-
local usage = {
54-
("Usage: %s [OPTIONS] [PACKAGE]"):format(program);
55-
(" or: %s [FILE]"):format(program);
56-
("");
57-
("\t--metadata <package> | Print metadata for package");
58-
("\t-m <package> | Run <package>.main module");
59-
("\t-h | Print help information")
60-
}
61-
print(table.concat(usage, "\n"))
62-
break
63-
else -- run a file
64-
local file = assert(arg[arg_index]:match("^.+%.fuse"),
65-
("Incorrect filetype: %s"):format(arg[arg_index]))
66-
compiler.do_file(file)
67-
break
6857
end
69-
arg_index = arg_index + 1
58+
elseif args.package then
59+
local module = args.file
60+
require(module .. ".main")
61+
else
62+
local file = assert(args.file:match("^.+%.fuse"),
63+
("Incorrect filetype: %s"):format(args.file))
64+
compiler.do_file(file)
7065
end

bin/util/ast.lua

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,17 @@
33
-- @script fusion-ast
44
-- @author ChickenNuggers
55
-- @usage fusion-ast [FILE]
6+
local argparse = require("argparse")
67
local parser = require("fusion.core.parser")
7-
local pretty = require("pl.pretty")
8+
local pretty = require("pl.pretty") -- TODO: replace
9+
10+
local argparser = argparse() {
11+
name = "fusion-ast";
12+
description = "Print a Lua table containing FusionScript AST";
13+
epilog = "For more info, see https://fusionscript.info";
14+
}
15+
argparser:argument("file", "File(s) to parse"):args("+")
16+
local files = argparser:parse().file
817

918
local function read_file(file)
1019
local file_handler = assert(io.open(file))
@@ -17,12 +26,11 @@ local function read_file(file)
1726
file_handler:close()
1827
end
1928

20-
local args = {...}
21-
if #args > 1 then
22-
for file in ipairs(args) do
29+
if #files > 1 then
30+
for file in ipairs(files) do
2331
print(("--- %s ---"):format(file))
2432
read_file(file)
2533
end
2634
else
27-
read_file(args[1])
35+
read_file(files[1])
2836
end

bin/util/pkg.lua

Lines changed: 52 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -6,34 +6,64 @@
66
-- or: fusion-pkg upgrade
77
-- or: fusion-pkg remove [REPO]
88

9-
local function clone(url)
10-
os.execute((
11-
"mkdir -p vendor; cd vendor; git clone %s --recursive; cd .."):format(
12-
url))
9+
local argparse = require("argparse")
10+
11+
local argparser = argparse() {
12+
name = "fusion-pkg";
13+
description = "Manage and install packages for FusionScript";
14+
epilog = "For more info, see https://fusionscript.info";
15+
}
16+
17+
local get = argparser:command "get"
18+
get:argument("repository",
19+
"GitHub repository or git+:// URL to clone package from")
20+
get:option("-f --from",
21+
"Website to clone repository (example: https://github.com)",
22+
"https://github.com/")
23+
24+
local upgrade = argparser:command "upgrade"
25+
upgrade:argument "repository"
26+
27+
local remove = argparser:command "remove"
28+
remove:argument "repository"
29+
30+
local with_dir = "mkdir -p vendor; cd vendor; git clone %s %s --recursive; cd .."
31+
local without_dir = "mkdir -p vendor; cd vendor; git clone %s --recursive; cd .."
32+
local function clone(url, dir)
33+
if dir then
34+
os.execute(with_dir:format(url, dir))
35+
else
36+
os.execute(without_dir:format(url))
37+
end
1338
end
1439

15-
local args = {...}
16-
if args[1] == "get" then
17-
local url = args[2]
40+
local args = argparser:parse()
41+
if args.get then
42+
local url = args.repository
1843
if url:match("^git+") then
1944
clone(url:sub(5))
2045
elseif url:find("/") then
21-
clone("https://github.com/" .. url)
46+
if args.from:sub(-1) ~= "/" then
47+
args.from = args.from .. "/"
48+
end
49+
clone(args.from .. url)
2250
end
23-
elseif args[1] == "upgrade" then
24-
local lfs = require("lfs")
25-
lfs.chdir("vendor")
26-
for dir in lfs.dir(".") do
27-
if dir:sub(1, 1) ~= "." then
28-
print("Upgrading " .. dir)
29-
os.execute(("cd %s; git pull --recurse-submodules; cd .."):format(dir))
51+
elseif args.upgrade then
52+
if args.repository then
53+
os.execute(("cd vendor/%s; git pull --recurse-submodules; cd ../..")
54+
:format(args.repository))
55+
else
56+
local lfs = require("lfs")
57+
lfs.chdir("vendor")
58+
for dir in lfs.dir(".") do
59+
if dir:sub(1, 1) ~= "." then
60+
print("Upgrading " .. dir)
61+
os.execute(("cd %s; git pull --recurse-submodules; cd ..")
62+
:format(dir))
63+
end
64+
lfs.chdir("..")
3065
end
3166
end
32-
lfs.chdir("..")
33-
elseif args[1] == "remove" then
34-
os.execute("rm -rf vendor/" .. assert(args[2], "Missing repository name"))
35-
elseif not args[1] then
36-
error("No command given")
37-
else
38-
error("Unknown option: " .. args[1])
67+
elseif args.remove then
68+
os.execute("rm -rf vendor/" .. args.repository)
3969
end

0 commit comments

Comments
 (0)