Files
zeekay 274cacae31 ci: real LuaJIT build/test workflow; generate test fixture via capnp
- .github/workflows/ci.yml: push-to-main + PR + workflow_dispatch. Installs
  luajit + luarocks + lunitx + capnproto on ubuntu-latest, loads the module,
  runs the full suite. Validated end-to-end in ubuntu:22.04 (capnp 0.8.0):
  59 tests / 504 assertions, 0 failures, 0 skips.
- tools/gen-example.sh: single canonical path to generate proto/example_zap.lua
  from proto/*.zap using the capnp front-end + this repo's pure-Lua compiler.
  Makes tests 01 and 10 run for real instead of skipping when no 'zap' tool.
- tests/run_tests.sh: generate the fixture via tools/gen-example.sh; honest
  skip only when capnp is genuinely absent.
- bin/zapc-lua: make cjson optional (pcall) to match bin/zapc-llua; it is only
  used for the --verbose debug dump and is not required to run.
2026-06-20 21:35:13 -07:00

146 lines
3.1 KiB
Bash
Executable File

#!/bin/bash
VERSION=0.1.3-1
usage() {
cat <<EOF
Usage: zapc-lua [<option>...]
This is a ZAP compiler plugin which compiles the schema into lua
files. This is meant to be run using the ZAP compiler, e.g.:
zap compile -olua foo.zap
Options:
--verbose
Write schema file: test.schema.lua and lua.schema.json; useful for debugging.
--version
Print version information and exit.
--help
Display this help text and exit.
EOF
exit
}
version() {
echo "ZAP Lua plug-in version $VERSION"
exit
}
realpath() {
perl -MCwd -e 'print Cwd::abs_path shift' $1;
}
VERBOSE=${VERBOSE:="false"}
FILE=$(realpath $0)
DIR=$(dirname $FILE)
export LUA_PATH="$DIR/../?.lua;$LUA_PATH;;"
while : ; do
case $1 in
--version)
version ;;
--help)
usage ;;
--verbose)
#[ -n "${VERBOSE}" ] && usage
VERBOSE="true"
shift ;;
*)
break ;;
esac
done
BASE=$(dirname $(realpath $0))
#export LUA_PATH="$BASE/lua/?.lua;$BASE/proto/?.lua;$LUA_PATH;;"
luajit - <(cat - | zap decode $BASE/schema.zap CodeGeneratorRequest) $@ <<EOT
local compile = require ("zap.compile")
local util = require ("zap.util")
-- cjson is only used for an optional --verbose schema dump; not required to run.
local has_cjson, cjson = pcall(require, "cjson")
local format = string.format
local insert = table.insert
local concat = table.concat
local debug = ${VERBOSE}
local schema_lua_file = "test.schema.lua"
local schema_json_file = "lua.schema.json"
function usage()
print("lua compile.lua [schema.txt]")
end
local f = arg[1]
if not f then
usage()
return
end
local naming
local config = {
version = "${VERSION}",
}
local namings = {}
for k, v in pairs(compile.naming_funcs) do
insert(namings, k)
end
-- TODO fix this
--arg[2] = "--naming=camel"
for i=2, #arg do
if string.sub(arg[i], 1, 9) == "--naming=" then
naming = string.sub(arg[i], 10)
local naming_func = compile.naming_funcs[naming]
if not naming_func then
error(format("unknown naming: %s. Available values are %s", naming,
concat(namings, " ")))
end
config.default_naming_func = naming_func
--config.default_enum_naming_func = naming_func
end
end
local lua_schema = util.parse_zap_decode_txt(f)
if debug then
print("write schema_lua_file")
util.write_file(schema_lua_file, lua_schema)
end
local schema = assert(assert(loadstring(lua_schema))())
schema.__compiler = "lua-zap(decoded by zap tool)"
if debug and has_cjson then
print("write schema_json_file")
util.write_file(schema_json_file, cjson.encode(schema))
end
local outfile = util.get_output_name(schema) .. ".lua"
--print(outfile)
--print("set config:")
compile.init(config)
compile.set_debug(debug)
local res = compile.compile(schema)
util.write_file(outfile, res)
--local file = assert(io.open(outfile, "w"))
--file:write(res)
--file:close()
--[[
local outfile = "data_generator.lua"
local res = compile.compile_data_generator(schema)
local file = assert(io.open(outfile, "w"))
file:write(res)
file:close()
]]
EOT