-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbundle.lua
167 lines (131 loc) · 4.97 KB
/
bundle.lua
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
--[[
Scissors XML Viewer License (MIT License)
-----------------------------------------
Copyright (c) 2009-2011 Mateusz Czapliński
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
]]--
--[[[
Script: bundle.lua
A utility script, which compiles all Lua modules required by Scissors
into bytecode and glues them together to one file runnable by Lua
interpreter.
Usage:
> lua bundle.lua [--stdin] MAIN_SCRIPT [< OPTIONS_FILE]
Output:
Writes two files: 'bundle.out.lua' and 'bundle.out'.
Options file:
The OPTIONS_FILE is required and loaded only if the '--stdin' commandline
option is provided. The OPTIONS_FILE should be a Lua script in which
the following settings are recognized:
modules - a table of strings containing names of the modules,
which are to be included in the bundle. The modules should
be ordered properly to fulfill their dependencies (independent
modules first, more dependent ones later).
Example options file:
(code)
modules = { 'module1', 'module2' }
(end code)
]]--
package.path = package.path .. ';./?'
require 'lib/luaEscape'
require 'bundle.findPackage'
require 'bundle.loadOptions'
--[[[
Function: multiReplace(s, replacements)
Treats every sequence of 2 or more ALLCAPS letters in 's' as a
key in the 'replacements' table and replaces it's occurence
with the associated value from the table.
]]--
local function multiReplace(s, replacements)
return s:gsub('%u%u+', replacements)
end
local function applyLoader(files, loader)
local code = {}
local separator = '\n'
for _, moduleName in ipairs(files) do
local filename = assert(findPackage(moduleName))
-- Load a file and get it's compiled bytecode.
-- local moduleBytecode = string.dump(assert(loadfile(filename)))
local f = io.open(filename)
local moduleCode = f:read('*a')
f:close()
local stuffedLoader = multiReplace(loader, {
CODE = moduleCode,
NAME = moduleName:luaEscape()
})
table.insert(code, stuffedLoader .. separator)
-- print(loaderCode)
-- print(code)
-- print(moduleName)
end
return table.concat(code, '')
end
local moduleLoader = [[
package.loaded[NAME] = package.loaded[NAME] or (function(...) CODE end)(NAME) or package.loaded[NAME] or true]]
local chunkLoader = [[(function(...) CODE end)()]]
local function writefile(opt)
local f = assert(io.open(opt.fname, opt.mode))
assert(f:write(opt.data))
assert(f:close())
end
local function bundle(options)
local code =
'package.path=""\n' .. -- to make it easier to check if all requirements got bundled
applyLoader(options.modules, moduleLoader) ..
applyLoader({options.mainScript}, chunkLoader)
-- Write the result in Lua form
writefile {
fname = ('%s.out.lua'):format(options.outputName),
mode = "w",
data = code
}
-- Compile the result to bytecode
local bytecode = string.dump(assert(loadstring(code, '<bundle.out.lua>')))
-- Write the result in binary form
writefile {
fname = ('%s.out'):format(options.outputName),
mode = "wb",
data = bytecode
}
end
local function main(arg)
local options = {
modules = {},
outputName = 'bundle',
}
if #arg < 1 or arg[1] == '--help' or (#arg >= 2 and arg[1] ~= '--stdin') then
print[[
Usage: lua bundle.lua [--stdin] MAIN_SCRIPT [< OPTIONS_FILE]
Writes two files: 'bundle.out.lua' and 'bundle.out'.
Available options:
modules = { 'module1', 'module2' },
outputName = 'bundle' -- changes the name of the created files
]]
os.exit(1)
end
if arg[1] == '--stdin' then
options.mainScript = arg[2]
loadOptions(io.stdin, options)
else
options.mainScript = arg[1]
end
bundle(options)
end
if arg == nil then
return { run = bundle }
end
main(arg)