Engine 0.0.6
Game engine in lua
Loading...
Searching...
No Matches
bootstrap.lua
Go to the documentation of this file.
1local function bootstrap()
2 local file = io.open('mock/bootstrap.lua', 'r')
3 local content = file:read('*a')
4 file:close()
5 return content
6end
8local function explode_string(input)
9 local result = {}
10
11 for line in string.gmatch(input, "[^\n]+") do
12 table.insert(result, line)
13 end
14
15 return result
16end
18local function string_to_hex(input)
19 local result = ""
20 for i = 1, #input do
21 local byte = input:byte(i)
22 result = result .. string.format("\\x%02x", byte)
23 end
24 return result
25end
26
27local function map_files(files, prefix_len)
28 local list_paths = {}
29 local list_files = {}
30 local dict_files = {}
31 local index = 1
32
33 while index <= #files do
34 local file_real_path = files[index]
35 local file_real = io.open(file_real_path, 'rb')
36 local file_real_content = file_real and file_real:read('*a')
37 local file_name = file_real_path:sub(prefix_len)
38
39 if file_real_content then
40 list_files[#list_files + 1] = file_name
41 dict_files[file_name] = string_to_hex(file_real_content)
42 else
43 list_paths[#list_paths + 1] = file_name
44 end
45
46 index = index + 1
47 end
48
49 return list_paths, list_files, dict_files
50end
51
52local function merge_dict_and_lists(list_out, dict_out, list_in, dict_in)
53 local index = 1
54 while index <= #list_in do
55 local key = list_in[index]
56 list_out[#list_out + 1] = key
57 if dict_out and dict_in then
58 dict_out[key] = dict_in[key]
59 end
60 index = index + 1
61 end
62end
63
64local function build(...)
65 local args = {...}
66
67 if BOOTSTRAP then
68 BOOTSTRAP_DISABLE = true
69 end
70
71 local input_file = io.open(args[1], 'r')
72 local output_file = io.open(args[2], 'w')
73
74 if #args <= 2 then
75 return false, 'missing src\'s to bundle!'
76 end
77
78 if not input_file or not output_file then
79 return false, 'usage: lua bootstrap.lua ./dist/main.lua ./dist/cli.lua ./src ./assets'
80 end
81
82 local index = 3
83 local all_list_paths = {}
84 local all_list_files = {}
85 local all_dict_files = {}
86 while index <= #args do
87 local path_src = args[index] or './src'
88 local prefix_path = path_src:match("^(.-)/[^/]+$")
89 local prefix_len = 0
90
91 if not prefix_path then
92 return false, 'src path must be a explicit relative path or absolute'
93 elseif path_src:sub(1, 2) == './' then
94 prefix_len = 3
95 else
96 prefix_len = #prefix_path + 2
97 end
98
99 do
100 local f = io.open(path_src, 'rb')
101 if not f then
102 return false, 'directory not found'
103 end
104 local can_read = f:read(1)
105 if can_read then
106 return false, 'path src must be a directory'
107 end
108 f:close()
109 end
110
111 local cmd_pid = io.popen('find '..path_src)
112 local cmd_raw, cmd_pid = cmd_pid:read('*a'), cmd_pid:close()
113 local list_raw = explode_string(cmd_raw)
114 local list_paths, list_files, dict_files = map_files(list_raw, prefix_len)
115 merge_dict_and_lists(all_list_files, all_dict_files, list_files, dict_files)
116 merge_dict_and_lists(all_list_paths, nil, list_paths, nil)
117 index = index + 1
118 end
119
120 output_file:write('local BOOTSTRAP = {}\nlocal BOOTSTRAP_DISABLE = false\n')
121
122 do
123 local index = 1
124 local content = 'local BOOTSTRAP_DIRS = {'
125 while index <= #all_list_paths do
126 local file_name = all_list_paths[index]
127 local file_content = all_dict_files[file_name]
128 content = content..'\''..file_name..'\''
129 index = index + 1
130 if index <= #all_list_paths then
131 content = content..', '
132 else
133 content = content..'}\n'
134 end
135 end
136 output_file:write(content)
137 end
138
139 do
140 local index = 1
141 local content = 'local BOOTSTRAP_LIST = {'
142 while index <= #all_list_files do
143 local file_name = all_list_files[index]
144 local file_content = all_dict_files[file_name]
145 content = content..'\''..file_name..'\''
146 index = index + 1
147 if index <= #all_list_files then
148 content = content..', '
149 else
150 content = content..'}\n'
151 end
152 end
153 output_file:write(content)
154 end
155
156 index = 1
157 while index <= #all_list_files do
158 local file_name = all_list_files[index]
159 local file_content = all_dict_files[file_name]
160 output_file:write('BOOTSTRAP[\''..file_name..'\'] = \''..file_content..'\'\n')
161 index = index + 1
162 end
163
164 output_file:write(bootstrap())
165 output_file:write(input_file:read('*a'))
166 output_file:close()
167 input_file:close()
168
169 return true
170end
171
172local function dump()
173 if not BOOTSTRAP then
174 return false, 'cli is not bootstraped'
175 end
176
177 do
178 local index = 1
179 while index <= #BOOTSTRAP_DIRS do
180 os.execute('mkdir -p '..BOOTSTRAP_DIRS[index])
181 index = index + 1
182 end
183 end
184
185 do
186 local index = 1
187 while index <= #BOOTSTRAP_LIST do
188 local file = BOOTSTRAP_LIST[index]
189 local output_file = io.open(file, 'wb')
190 output_file:write(BOOTSTRAP[file])
191 output_file:close()
192 index = index + 1
193 end
194 end
195
196 return true
197end
198
199local P = {
200 build = build,
201 dump = dump
202}
203
204return P
build
Definition build.lua:20
local function string_to_hex(input)
local function merge_dict_and_lists(list_out, dict_out, list_in, dict_in)
local function map_files(files, prefix_len)
local function dump()
local function explode_string(input)
local function bootstrap()
local function file(self, file)
local function line(x1, y1, x2, y2)