Skip to content

Commit

Permalink
bin/: Move to mpeterv/argparse
Browse files Browse the repository at this point in the history
  • Loading branch information
RyanSquared committed May 18, 2017
1 parent 1294632 commit 34917d9
Show file tree
Hide file tree
Showing 4 changed files with 135 additions and 102 deletions.
50 changes: 25 additions & 25 deletions bin/compiler/source.lua
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,30 @@
-- -p | Write output to stdout
-- -h | Print help information

local argparse = require("argparse")
local compiler = require("fusion.core.compilers.source")
local lfs = require("lfs")

local argparser = argparse() {
name = "fusionc-source";
description = "Generate a Lua file from FusionScript code";
epilog = "Fur more info, see https://fusionscript.info";
}

argparser:argument("file", "Files or directories to compile"):args("+")
argparser:mutex(
argparser:flag("-p --print", "Print compiled output"),
argparser:flag("-q --quiet", "Don't print status messages")
)

local function walk(file_func, dir)
for item in lfs.dir(dir) do
if item:sub(1, 1) == "." then -- luacheck: ignore
-- pass
elseif lfs.attributes(dir .. "/" .. item, "mode") == "directory" then
walk(file_func, dir .. "/" .. item)
else
print(pcall(file_func, dir .. "/" .. item))
pcall(file_func, dir .. "/" .. item)
end
end
end
Expand All @@ -28,6 +41,9 @@ local function process(file, does_output)
return walk(process, file)
end
local base = file:match("^(.+)%.fuse$")
if not base then
return
end
local output = compiler.read_file(file)
local output_file = io.open(base .. ".lua", "w")
output_file:write(output .. "\n")
Expand All @@ -37,29 +53,13 @@ local function process(file, does_output)
end
end

local args = {...}
local arg_index = 1
while arg_index <= #args do
local _arg = args[arg_index]
if _arg == "-p" then
arg_index = arg_index + 1
for i=arg_index, #args do
io.write(compiler.read_file(args[i]))
end
break
elseif _arg == "--help" or _arg == "-h" then
local program = arg[0]:match(".+/(.-)$") or arg[0]
local usage = {
("Usage: %s [OPTIONS] [FILE]"):format(program);
(" or: %s [FILE/DIRECTORY]"):format(program);
("");
("\t-p | Write output to stdout");
("\t-h | Print help information")
}
print(table.concat(usage, "\n"))
break
else
process(_arg)
arg_index = arg_index + 1
local args = argparser:parse()
if args.print then -- two loops are written to run the check only once
for i, file in ipairs(args.file) do -- luacheck: ignore 213
io.write(compiler.read_file(file))
end
else
for i, file in ipairs(args.file) do -- luacheck: ignore 213
process(file, not args.quiet)
end
end
95 changes: 45 additions & 50 deletions bin/interpreter/source.lua
Original file line number Diff line number Diff line change
Expand Up @@ -8,63 +8,58 @@
-- --metadata <package> | Print metadata for package
-- -m <package> | Run <package>.main module
-- -h | Print help information

local argparse = require("argparse")
local compiler = require("fusion.core.compilers.source")
compiler.inject_loader()
compiler.inject_extensions()

local argparser = argparse() {
name = "fusion-source";
description = "Run FusionScript code with the Lua VM";
epilog = "For more info, see https://fusionscript.info";
}

argparser:mutex(
argparser:flag("--metadata", "Print metadata information for a package"),
argparser:flag("--package", "Run <package>.main module")
)

argparser:argument("file", "File to run")

local args = argparser:parse()

_G.compiler = compiler

local arg_index = 1
while arg_index <= #arg do
if arg[arg_index] == "--metadata" then -- return metadata from module
assert(arg[arg_index + 1], "missing argument to --metadata: module")
local ok, module = pcall(require, arg[arg_index + 1] ..
".metadata")
if not ok then
error("Could not find module metadata for package: " ..
arg[arg_index + 1] .. "\n" .. module)
else
local function check(name)
assert(module[name], "Missing field: " .. name)
end
local opts = {"version", "description", "author", "copyright",
"license"}
for _, name in ipairs(opts) do
check(name)
end
for _, name in ipairs(opts) do
local value = module[name]
local _type = type(value)
if _type == "string" then
print(("['%s'] = %q"):format(name, value))
else
print(("['%s'] = %s"):format(name, tostring(value)))
end
if args.metadata then
local ok, module = pcall(require, args.file .. ".metadata")
if not ok then
error("Could not find module metadata for package: " .. args.file ..
"\n" .. module)
else
local function check(name)
assert(module[name], "Missing field: " .. name)
end
local opts = {"version", "description", "author", "copyright",
"license"}
for _, name in ipairs(opts) do
check(name)
end
for _, name in ipairs(opts) do
local value = module[name]
local _type = type(value)
if _type == "string" then
print(("['%s'] = %q"):format(name, value))
else
print(("['%s'] = %s"):format(name, tostring(value)))
end
break
end
elseif arg[arg_index] == "-m" then -- load <module>.main and exit
local module = arg[arg_index + 1]
assert(module, "missing argument to -m: module")
require(module .. ".main")
break
elseif arg[arg_index] == "-h" or arg[arg_index] == "--help" then -- print help
local program = arg[0]:match(".+/(.-)$") or arg[0]
local usage = {
("Usage: %s [OPTIONS] [PACKAGE]"):format(program);
(" or: %s [FILE]"):format(program);
("");
("\t--metadata <package> | Print metadata for package");
("\t-m <package> | Run <package>.main module");
("\t-h | Print help information")
}
print(table.concat(usage, "\n"))
break
else -- run a file
local file = assert(arg[arg_index]:match("^.+%.fuse"),
("Incorrect filetype: %s"):format(arg[arg_index]))
compiler.do_file(file)
break
end
arg_index = arg_index + 1
elseif args.package then
local module = args.file
require(module .. ".main")
else
local file = assert(args.file:match("^.+%.fuse"),
("Incorrect filetype: %s"):format(args.file))
compiler.do_file(file)
end
18 changes: 13 additions & 5 deletions bin/util/ast.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,17 @@
-- @script fusion-ast
-- @author ChickenNuggers
-- @usage fusion-ast [FILE]
local argparse = require("argparse")
local parser = require("fusion.core.parser")
local pretty = require("pl.pretty")
local pretty = require("pl.pretty") -- TODO: replace

local argparser = argparse() {
name = "fusion-ast";
description = "Print a Lua table containing FusionScript AST";
epilog = "For more info, see https://fusionscript.info";
}
argparser:argument("file", "File(s) to parse"):args("+")
local files = argparser:parse().file

local function read_file(file)
local file_handler = assert(io.open(file))
Expand All @@ -17,12 +26,11 @@ local function read_file(file)
file_handler:close()
end

local args = {...}
if #args > 1 then
for file in ipairs(args) do
if #files > 1 then
for file in ipairs(files) do
print(("--- %s ---"):format(file))
read_file(file)
end
else
read_file(args[1])
read_file(files[1])
end
74 changes: 52 additions & 22 deletions bin/util/pkg.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,34 +6,64 @@
-- or: fusion-pkg upgrade
-- or: fusion-pkg remove [REPO]

local function clone(url)
os.execute((
"mkdir -p vendor; cd vendor; git clone %s --recursive; cd .."):format(
url))
local argparse = require("argparse")

local argparser = argparse() {
name = "fusion-pkg";
description = "Manage and install packages for FusionScript";
epilog = "For more info, see https://fusionscript.info";
}

local get = argparser:command "get"
get:argument("repository",
"GitHub repository or git+:// URL to clone package from")
get:option("-f --from",
"Website to clone repository (example: https://github.com)",
"https://github.com/")

local upgrade = argparser:command "upgrade"
upgrade:argument "repository"

local remove = argparser:command "remove"
remove:argument "repository"

local with_dir = "mkdir -p vendor; cd vendor; git clone %s %s --recursive; cd .."
local without_dir = "mkdir -p vendor; cd vendor; git clone %s --recursive; cd .."
local function clone(url, dir)
if dir then
os.execute(with_dir:format(url, dir))
else
os.execute(without_dir:format(url))
end
end

local args = {...}
if args[1] == "get" then
local url = args[2]
local args = argparser:parse()
if args.get then
local url = args.repository
if url:match("^git+") then
clone(url:sub(5))
elseif url:find("/") then
clone("https://github.com/" .. url)
if args.from:sub(-1) ~= "/" then
args.from = args.from .. "/"
end
clone(args.from .. url)
end
elseif args[1] == "upgrade" then
local lfs = require("lfs")
lfs.chdir("vendor")
for dir in lfs.dir(".") do
if dir:sub(1, 1) ~= "." then
print("Upgrading " .. dir)
os.execute(("cd %s; git pull --recurse-submodules; cd .."):format(dir))
elseif args.upgrade then
if args.repository then
os.execute(("cd vendor/%s; git pull --recurse-submodules; cd ../..")
:format(args.repository))
else
local lfs = require("lfs")
lfs.chdir("vendor")
for dir in lfs.dir(".") do
if dir:sub(1, 1) ~= "." then
print("Upgrading " .. dir)
os.execute(("cd %s; git pull --recurse-submodules; cd ..")
:format(dir))
end
lfs.chdir("..")
end
end
lfs.chdir("..")
elseif args[1] == "remove" then
os.execute("rm -rf vendor/" .. assert(args[2], "Missing repository name"))
elseif not args[1] then
error("No command given")
else
error("Unknown option: " .. args[1])
elseif args.remove then
os.execute("rm -rf vendor/" .. args.repository)
end

0 comments on commit 34917d9

Please sign in to comment.