okay, we good now. i think.

This commit is contained in:
sam 2019-12-14 17:16:32 -05:00
parent 05cdf4fc1c
commit 82d10aa87a
8 changed files with 188 additions and 16 deletions

View File

@ -1,20 +1,12 @@
# LuaComp
A general purpose Lua preprocessor and minifier.
A general purpose preprocessor and postprocessor written in Lua.
## Building
To build, either execute `luapreproc` or `luacomp` on src/init.lua
### luapreproc
Execute `luapreproc init.lua ../luacomp.lua`
### luacomp
Execute `luacomp init.lua -xO ../luacomp.lua`
***NOTE***: Do not use a minifier, it breaks argparse!
See manual/README.md
## How-To
### Merging Lua source files
### Merging source files
```lua
-- myfile.lua
local my_lib = {}

2
examples/error.lua Normal file
View File

@ -0,0 +1,2 @@
--#error "What a scam"
print("I never get printed.")

120
manual/README.md Normal file
View File

@ -0,0 +1,120 @@
# Building
## Luapreproc
```sh
git clone https://github.com/Adorable-Catgirl/LuaComp.git
cd LuaComp
luapreproc src/init.lua /tmp/luacomp.part
echo "#!/usr/bin/env lua5.3" | cat - /tmp/luacomp.part > ~/bin/luacomp
chmod +x ~/bin/luacomp
```
## LuaComp
```sh
git clone https://github.com/Adorable-Catgirl/LuaComp.git
cd LuaComp
luacomp -xlua5.3 -mluamin -O ~/bin/luacomp src/init.lua
chmod +x ~/bin/luacomp
```
## By hand
Ha, no.
# Usage
`luacomp -h` outputs:
```
Usage: /tmp/luacomp [-h] [-O <output>] [-m <minifier>]
[--generator-code] [--verbose] [--post-processors]
[--directives] [-v] <input> [-x [<executable>]]
LuaComp v1.2.0
A preprocessor+postprocessor written in Lua.
Arguments:
input Input file (- for STDIN)
Options:
-h, --help Show this help message and exit.
-O <output>, Output file. (- for STDOUT) (default: -)
--output <output>
-m <minifier>,
--minifier <minifier>
Sets the minifier (default: none)
-x [<executable>],
--executable [<executable>]
Makes the script an executable (default: current lua version)
--generator-code Outputs only the code from the generator.
--verbose Verbose output. (Debugging)
--post-processors Lists postprocessors
--directives Lists directives
-v, --version Prints the version and exits
```
# Directives
LuaComp currently ships with four built-in directives. More directives can be installed at `/usr/share/luacomp/directives` and `~/.local/share/luacomp/directives`.
## include
Usage: `--#include "path"`
Include tells the preprocessor to include the file specified. The file included will also be preprocessed.
## define
Usage `--#define "var" "value"`
Defines the env var for this session.
## error
Usage `--#error "text"`
Throws an error and stops the preprocessor.
## loadmod
**Note**: Depreciated, use the directive dirs.
Usage `--#loadmod "lua_file.lua"`
Loads a directive module. Don't use this. I just don't want to have to bump the major version number.
# Postprocessors
LuaComp currently ships with built-in support for three postprocessors. More can be installed at `/usr/share/luacomp/postproc` and `~/.local/share/luacomp/postproc`
## luamin
Language: Lua 5.1 to 5.3
Minifies Lua.
## uglify
Language: JavaScript
Minifies JS. Options are `--compress --mangle`.
## bython(2)
Language: Bython (Python 3.x and 2.x)
Turns Bython into Python.
# Syntax
The syntax was made for Lua, but it works well enough for some other languages.
## Directives
Syntax: `--#directive "arguments"`
## Lua code
Syntax: `@[[ code ]]`
Note that this can be used for macros.
See examples/macro.lua.
## Lua variable
Syntax: `@[{ variable }]`
Puts the value of the variable in the code
## Quoted shell variables
Syntax: `$(var)`
Puts the value of the variable in the code, quoted.
## Unquoted shell variables
Syntax: `$[{var}]`
Puts the value of the variable in the code, not quoted.
## Shell output
Syntax: `$[[code]]`
Puts the stdout of the script in the code. Note that you can't set variables from the shell script.
# Debugging
## Generator code
To output the code the generator executes, add the `--generator-code` flag.
## Debug messages
To output debug messages, add the `--verbose` flag.

View File

@ -15,13 +15,15 @@ end
--#include "src/directive_provider.lua"
--#include "src/cfg/minifier_providers.lua"
local parser = argparse(arg[0], "LuaComp v"..LUACOMP_VERSION.."\nA Lua preprocessor+postprocessor.")
local parser = argparse(arg[0], "LuaComp v"..LUACOMP_VERSION.."\nA preprocessor+postprocessor written in Lua.")
parser:argument("input", "Input file (- for STDIN)")
parser:option("-O --output", "Output file. (- for STDOUT)", "-")
parser:option("-m --minifier", "Sets the minifier", "none")
parser:option("-m --minifier", "Sets the postprocessor", "none")
parser:option("-x --executable", "Makes the script an executable (default: current lua version)"):args "?"
parser:flag("--generator-code", "Outputs only the code from the generator.")
parser:flag("--verbose", "Verbose output. (Debugging)"):action(function() VERBOSE=true end)
parser:flag("--post-processors", "Lists postprocessors"):action(function() preload_providers() for k, v in pairs(providers) do print(k) end os.exit(0) end)
parser:flag("--directives", "Lists directives"):action(function() preload_directives() for k, v in pairs(directives) do print(k) end os.exit(0) end)
parser:flag("-v --version", "Prints the version and exits"):action(function()
print(LUACOMP_VERSION)
os.exit(0)
@ -47,14 +49,14 @@ local ocode = generate(ast, args.generator_code)
local minifier = providers[args.minifier]
dprint("Minifier: "..args.minifier, minifier)
if not minifier then
io.stderr:write("ERROR: Minifier `"..args.minifier.."' not found!\n")
io.stderr:write("ERROR: Postprocessor `"..args.minifier.."' not found!\n")
os.exit(1)
end
dprint("Running...")
local rcode, err = minifier(ocode)
if (not rcode) then
io.stderr:write("ERROR: Error for minifier `"..args.minifier.."': \n")
io.stderr:write("ERROR: Error for postprocessor `"..args.minifier.."': \n")
io.stderr:write(err)
os.exit(1)
end

View File

@ -0,0 +1,4 @@
local directive_paths = {
"/usr/share/luacomp/directives",
os.getenv("HOME").."/.local/share/luacomp/directives"
}

View File

@ -16,6 +16,11 @@
limitations under the License.
]]
local postproc_paths = {
"/usr/share/luacomp/postproc",
os.getenv("HOME").."/.local/share/luacomp/postproc"
}
local providers = {}
function providers.luamin(cin)
@ -92,4 +97,27 @@ end
function providers.none(cin)
return cin
end
setmetatable(providers, {__index=function(t, i)
for i=1, #postproc_paths do
if (os.execute("stat "..postproc_paths[i].."/"..i..".lua 1>/dev/null 2>&1")) then
providers[i] = loadfile(postproc_paths[i].."/"..i..".lua")()
return providers[i]
end
end
end})
local function preload_providers()
--Do this in the best way possible
for i=1, #postproc_paths do
if (os.execute("stat "..postproc_paths[i].."1>/dev/null 2>&1")) then
local fh = io.popen("ls "..postproc_paths[i], "r")
for line in fh:lines() do
if (line:match("%.lua$")) then
providers[line:sub(1, #line-4)] = loadfile(postproc_paths[i].."/"..line)()
end
end
end
end
end

View File

@ -16,7 +16,31 @@
limitations under the License.
]]
--#include "src/cfg/directive_providers.lua"
--#include "src/directives/define.lua"
--#include "src/directives/include.lua"
--#include "src/directives/loadmod.lua"
--#include "src/directives/error.lua"
setmetatable(directives, {__index=function(t, i)
for i=1, #directive_paths do
if (os.execute("stat "..directive_paths[i].."/"..i..".lua 1>/dev/null 2>&1")) then
directives[i] = loadfile(directive_paths[i].."/"..i..".lua")()
return directives[i]
end
end
end})
local function preload_directives()
--Do this in the best way possible
for i=1, #directive_paths do
if (os.execute("stat "..directive_paths[i].." 1>/dev/null 2>&1")) then
local fh = io.popen("ls "..directive_paths[i], "r")
for line in fh:lines() do
if (line:match("%.lua$")) then
directives[line:sub(1, #line-4)] = loadfile(directive_paths[i].."/"..line)()
end
end
end
end
end

View File

@ -1,7 +1,7 @@
local warned = false
function directives.loadmod(env, mod)
if not warned then
io.stderr:write("Warning: loadmod is depreciated and unsafe. The API differs from luapreproc.\n")
io.stderr:write("Warning: loadmod is depreciated and unsafe. The API differs from luapreproc. Use the include paths!\n")
warned = true
end
if (not os.execute("stat "..file..">/dev/null")) then