Files
imgui-luau/tools/bundle.py
T

52 lines
2.2 KiB
Python

#!/usr/bin/env python3
# Bundles imgui/*.lua + backend into ONE ModuleScript: build/ImGui.luau
# Why: ModuleScripts don't share globals, and the setfenv() alternative turns off Luau's fast paths / native codegen.
# Each source file becomes a function in __FILES; IM_INCLUDE(name) runs it once and caches the result.
# The module returns a table with every global the library defines (ImGui, ImVec2, ImGuiConfigFlags, ImGui_ImplRoblox, ...).
import os, re
ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
SRC = os.path.join(ROOT, "imgui")
FILES = {}
for dp, _, fn in os.walk(SRC):
for f in sorted(fn):
if f.endswith(".lua"):
FILES[f] = open(os.path.join(dp, f)).read()
ENTRY = ["imgui.lua", "imgui_impl_roblox.lua"] + sorted(f for f in FILES if f.startswith("imgui_demo")) # run order
names = set()
for src in FILES.values():
names.update(re.findall(r"^function ([A-Za-z_]\w*)\s*\(", src, re.M))
names.update(re.findall(r"^([A-Za-z_]\w*)\s*=[^=]", src, re.M))
out = ["--!optimize 2", "-- GENERATED by tools/bundle.py from imgui/*.lua - do not edit", "",
"local __FILES, __LOADED = {}, {}",
"function IM_INCLUDE(name)",
" local r = __LOADED[name]",
" if r == nil then r = __FILES[name](); if r == nil then r = true end; __LOADED[name] = r end",
" return r",
"end", ""]
linemap = [] # (first bundle line, file) for error -> source mapping (tools/test/run.py)
for name, src in sorted(FILES.items()):
out.append("__FILES[%r] = function()" % name)
linemap.append((sum(x.count("\n") + 1 for x in out) + 1, name))
out.append(src.rstrip("\n"))
out.append("end")
out.append("")
for name in ENTRY:
out.append("IM_INCLUDE(%r)" % name)
out.append("")
out.append("return {")
for n in sorted(names):
out.append(" %s = %s," % (n, n))
out.append("}")
import sys
OUT = sys.argv[1] if len(sys.argv) > 1 else os.path.join(ROOT, "build")
os.makedirs(OUT, exist_ok=True)
open(os.path.join(OUT, "ImGui.luau"), "w").write("\n".join(out) + "\n")
import json
json.dump(linemap, open(os.path.join(OUT, "ImGui.linemap.json"), "w"))
print("wrote build/ImGui.luau (%d files, %d exports)" % (len(FILES), len(names)))