Add infrastructure for multi-file programs

This commit is contained in:
Christopher Head 2021-03-23 21:57:57 -07:00
parent df74ae2a64
commit c25d166cba
No known key found for this signature in database
GPG Key ID: 211CEB0BE447577B
3 changed files with 76 additions and 1 deletions

27
infrastructure/Makefile Normal file
View File

@ -0,0 +1,27 @@
ALL_SOURCES := $(filter-out combined.lua,$(wildcard *.lua))
MAIN_SOURCE := main.lua
TEST_SOURCES := $(filter test-%.lua,$(ALL_SOURCES))
MODULE_SOURCES := $(filter-out $(MAIN_SOURCE) $(TEST_SOURCES),$(ALL_SOURCES))
.PHONY : world
world : combined.lua
combined.lua : $(MAIN_SOURCE) $(MODULE_SOURCES) ../infrastructure/combine
../infrastructure/combine $(MAIN_SOURCE) $(MODULE_SOURCES) > $@
.PHONY : check
check : luacheck $(TEST_SOURCES:test-%.lua=check-%)
.PHONY : luacheck
luacheck : $(ALL_SOURCES)
luacheck $+
.PHONY : $(TEST_SOURCES:test-%.lua=check-%)
$(TEST_SOURCES:test-%.lua=check-%) : check-% : $(ALL_SOURCES)
luajit $(@:check-%=test-%.lua)
.PHONY : clean
clean :
$(RM) combined.lua
.DELETE_ON_ERROR :

42
infrastructure/combine Executable file
View File

@ -0,0 +1,42 @@
#!/bin/bash
set -e
set -u
set -o pipefail
cat << 'EOF'
local require_cache = {}
local function require(module_name)
local entry = require_cache[module_name]
if type(entry) == "function" then
entry = entry(module_name)
require_cache[module_name] = entry
end
return entry
end
EOF
main_file="$1"
shift
for file in "$@"; do
echo 'require_cache["'"${file%.lua}"'"] = function(...)'
echo "--[["
echo "BEGIN CONTENTS OF ${file}"
echo "]]"
cat "${file}"
echo "--[["
echo "END CONTENTS OF ${file}"
echo "]]"
echo "end"
echo
done
echo "--[["
echo "BEGIN CONTENTS OF ${main_file}"
echo "]]"
cat "${main_file}"
echo "--[["
echo "END CONTENTS OF ${main_file}"
echo "]]"

View File

@ -121,5 +121,11 @@ stds.luacontrollerwriteable = {
},
}
files["*.lua"].std = "sandboxcommon+luacontroller"
stds.combinescript = {
read_globals = {
"require",
}
}
files["*.lua"].std = "sandboxcommon+luacontroller+combinescript"
files["test-*.lua"].std = "sandboxcommonwriteable+luacontrollerwriteable+luajit"