-- 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}