From 86425c8c93c8529cd2c88db1d0303fe980048896 Mon Sep 17 00:00:00 2001 From: citrons Date: Wed, 10 Apr 2024 18:16:41 -0500 Subject: initial commit --- bundler/init.lua | 81 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 81 insertions(+) create mode 100644 bundler/init.lua (limited to 'bundler/init.lua') 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} -- cgit v1.2.3