1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
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}
|