aboutsummaryrefslogtreecommitdiff
path: root/bundler/init.lua
diff options
context:
space:
mode:
authorcitrons <citrons@mondecitronne.com>2024-04-10 18:16:41 -0500
committercitrons <citrons@mondecitronne.com>2024-04-10 18:24:14 -0500
commit86425c8c93c8529cd2c88db1d0303fe980048896 (patch)
tree87d160b058ba0990d8c66631f3b776a6d2deb82f /bundler/init.lua
initial commit
Diffstat (limited to 'bundler/init.lua')
-rw-r--r--bundler/init.lua81
1 files changed, 81 insertions, 0 deletions
diff --git a/bundler/init.lua b/bundler/init.lua
new file mode 100644
index 0000000..799a398
--- /dev/null
+++ b/bundler/init.lua
@@ -0,0 +1,81 @@
+-- bundler. public domain.
+-- https://citrons.xyz/git/cc-bundler.git
+
+local platform = require "bundler.platform"
+
+local function stringize(str, multiline)
+ local equal_signs = "="
+ for e in string.gmatch(str, "](=*)]") do
+ if #e > #equal_signs then
+ equal_signs = e .. "="
+ end
+ end
+ if multiline then
+ str = "\n"..str.."\n"
+ end
+ return ("[%s[%s]%s]"):format(equal_signs, str, equal_signs)
+end
+
+local function module_name(path)
+ path = path:match "^(.*).lua$" or path
+ path = path:match "^%./(.*)$" or path
+ path = path:gsub("/+", ".")
+ path = path:gsub("^%.+", "")
+ path = path:gsub("%.+$", "")
+ path = path:match "^(.*)%.init$" or path
+ return path
+end
+
+local function bundle_module(path)
+ if platform.is_dir(path) then
+ local modules = {}
+ for f in platform.list_dir(path) do
+ if f ~= "." and f ~= ".." then
+ table.insert(
+ modules, bundle_module(path.."/"..f))
+ end
+ end
+ return table.concat(modules, "\n")
+ else
+ local name = module_name(path)
+ local content = platform.read(path)
+ return ("modules[ %s ] = %s"):format(
+ stringize(name), stringize(content, true))
+ end
+end
+
+local loader = [[
+
+-- https://citrons.xyz/git/cc-bundler.git
+
+local load = loadstring or load
+
+local function loader(name)
+ if modules[name] then
+ local f = load(modules[name], name)
+ if setfenv then
+ setfenv(f, getfenv())
+ end
+ return f
+ end
+end
+table.insert(package.loaders or package.searchers, 1, loader)
+
+return loader( %s )(...)
+
+]]
+
+local function bundle(main_module, other_modules)
+ local output = {"local modules = {}"}
+
+ table.insert(output, bundle_module(main_module))
+ for _, m in ipairs(other_modules) do
+ table.insert(output, bundle_module(m))
+ end
+
+ local loader = loader:format(stringize(module_name(main_module)))
+
+ return table.concat(output, "\n")..loader
+end
+
+return {bundle = bundle}