From 52b4c9000f384e5ee13d06c126d0cd14cc966de5 Mon Sep 17 00:00:00 2001 From: Kai Vogelgesang Date: Mon, 26 Sep 2022 22:16:31 +0200 Subject: [PATCH] Improve lua build process + folder structure --- lua/justfile | 29 +++++++++++++++-------------- lua/minify.lua | 2 +- lua/{ => src}/auth.d.tl | 0 lua/{ => src/lib}/framebuffer.tl | 0 lua/{ => src/lib}/ringbuffer.tl | 0 lua/{ => src/lib}/socket.tl | 0 lua/{ => src}/main.tl | 8 ++++---- lua/{ => src/vendor}/json.d.tl | 0 lua/{ => src/vendor}/json.lua | 0 lua/tlconfig.lua | 3 +++ server/server/__init__.py | 13 ++++++++++++- server/templates/install.lua | 6 +++--- 12 files changed, 38 insertions(+), 23 deletions(-) rename lua/{ => src}/auth.d.tl (100%) rename lua/{ => src/lib}/framebuffer.tl (100%) rename lua/{ => src/lib}/ringbuffer.tl (100%) rename lua/{ => src/lib}/socket.tl (100%) rename lua/{ => src}/main.tl (96%) rename lua/{ => src/vendor}/json.d.tl (100%) rename lua/{ => src/vendor}/json.lua (100%) diff --git a/lua/justfile b/lua/justfile index 3489553..f3bdcec 100644 --- a/lua/justfile +++ b/lua/justfile @@ -1,30 +1,31 @@ default: @just --list -lua_files := `find . -type f -name "*.lua" ! -path './out/*' ! -name tlconfig.lua ! -name minify.lua -printf "%p "` -teal_files := `find . -type f -name "*.tl" ! -name '*.d.tl' -printf "%p "` - -build: - mkdir -p out - for file in {{lua_files}}; do \ - lua minify.lua $file; \ - done - for file in {{teal_files}}; do \ - tl gen $file; \ - lua minify.lua ${file%.tl}.lua; \ - rm ${file%.tl}.lua; \ - done +build: && manifest + tl build + @mkdir -p out/vendor + find src/vendor -name "*.lua" -exec cp -t out/vendor {} + alias b := build +manifest_file := "out/manifest.txt" +manifest: + rm -f {{ manifest_file }} + find out -name "*.lua" -exec realpath --relative-to=out {} >> {{ manifest_file }} \; + +min-build: build + find out -name "*.lua" -exec lua minify.lua {} \; + clean: rm -r out alias c := clean +watch_recipe := env_var_or_default("WATCH_RECIPE", "build") + watch: while sleep 0.1; do \ - find . -type f ! -path './out/*' | entr -d just build; \ + find . -type f ! -path './out/*' | entr -d just {{ watch_recipe }}; \ [ $? -eq 0 ] && exit 0; \ done diff --git a/lua/minify.lua b/lua/minify.lua index 95fc883..00fdc06 100644 --- a/lua/minify.lua +++ b/lua/minify.lua @@ -11,6 +11,6 @@ parser.minify(ast, true) local output = parser.toLua(ast) -local f = io.open("out/"..filename, "w") +local f = io.open(filename, "w") f:write(output) f:close() \ No newline at end of file diff --git a/lua/auth.d.tl b/lua/src/auth.d.tl similarity index 100% rename from lua/auth.d.tl rename to lua/src/auth.d.tl diff --git a/lua/framebuffer.tl b/lua/src/lib/framebuffer.tl similarity index 100% rename from lua/framebuffer.tl rename to lua/src/lib/framebuffer.tl diff --git a/lua/ringbuffer.tl b/lua/src/lib/ringbuffer.tl similarity index 100% rename from lua/ringbuffer.tl rename to lua/src/lib/ringbuffer.tl diff --git a/lua/socket.tl b/lua/src/lib/socket.tl similarity index 100% rename from lua/socket.tl rename to lua/src/lib/socket.tl diff --git a/lua/main.tl b/lua/src/main.tl similarity index 96% rename from lua/main.tl rename to lua/src/main.tl index 56dd0b4..524941e 100644 --- a/lua/main.tl +++ b/lua/src/main.tl @@ -1,7 +1,7 @@ -local json = require("json") -local Framebuffer = require("framebuffer") -local Ringbuffer = require("ringbuffer") -local Socket = require("socket") +local json = require("vendor/json") +local Framebuffer = require("lib/framebuffer") +local Ringbuffer = require("lib/ringbuffer") +local Socket = require("lib/socket") local auth = require("auth") local ENDPOINT = auth.server:gsub("http", "ws", 1) .. "/ipmi/computer/" .. auth.id .. "/ws" local HEADERS = { ["Authorization"] = "Bearer " .. auth.token } diff --git a/lua/json.d.tl b/lua/src/vendor/json.d.tl similarity index 100% rename from lua/json.d.tl rename to lua/src/vendor/json.d.tl diff --git a/lua/json.lua b/lua/src/vendor/json.lua similarity index 100% rename from lua/json.lua rename to lua/src/vendor/json.lua diff --git a/lua/tlconfig.lua b/lua/tlconfig.lua index 52911f6..f2b51e3 100644 --- a/lua/tlconfig.lua +++ b/lua/tlconfig.lua @@ -1,4 +1,7 @@ return { + source_dir = "src", + include_dir = {"src"}, + build_dir = "out", global_env_def = "cc", gen_target = "5.1", } \ No newline at end of file diff --git a/server/server/__init__.py b/server/server/__init__.py index d67caa8..e3fe911 100644 --- a/server/server/__init__.py +++ b/server/server/__init__.py @@ -23,11 +23,22 @@ app.mount("/tiles/", map_tiles) app.mount("/ipmi/", monitoring) app.mount("/api/", api) -installer = j2env.get_template("install.lua").render(deploy_path=settings.deploy_path) + +def render_lua_installer(): + with open(f"{settings.lua_out_path}/manifest.txt", "r") as f: + file_list = [name.strip() for name in f.readlines()] + return j2env.get_template("install.lua").render( + deploy_path=settings.deploy_path, file_list=file_list + ) + + +installer = render_lua_installer() @app.get("/install") async def get_installer(): + if settings.dev_mode: + installer = render_lua_installer() return PlainTextResponse(installer) diff --git a/server/templates/install.lua b/server/templates/install.lua index 00ee189..296f8a6 100644 --- a/server/templates/install.lua +++ b/server/templates/install.lua @@ -1,6 +1,6 @@ local args = { ... } - local token = args[1] + local existing = fs.exists("auth.lua") if not token and not existing then error("A token is required.") @@ -16,8 +16,8 @@ elseif token then error("Token failed!") end end -local files = { "main.lua", "json.lua", "framebuffer.lua", "ringbuffer.lua", "socket.lua" } +local files = { {% for filename in file_list %} "{{ filename }}", {% endfor %} } for _, file in ipairs(files) do fs.delete(file) - shell.run(("wget %s/lua/%s"):format(path, file)) + shell.run(("wget %s/lua/%s %s"):format(path, file, file)) end \ No newline at end of file