2--! @brief groups code into a single source
3--! @
param[in] src_path folder with lua includes
5--! @
param[in] dest_file packaged
file output
7--! @li @c lib_common_math.lua
9--! local function sum(a, b)
23--! local zeebo_math =
require(
'lib_common_math')
25--! print(zeebo_math.sum(1, 2))
33--! local lib_common_math = nil
35--! local function
main()
36--! local zeebo_math = lib_common_math()
37--! print(zeebo_math.sum(1, 2))
41--! lib_common_math = function()
42--! local function sum(a, b)
55local function
build(src_path, src_filename, dest)
56 local pattern =
"local ([%w_%-]+) = require%('(.-)'%)"
58 local src_in = src_path..src_filename
59 local src_file = io.open(src_in,
'r')
60 local dest_file = io.open(dest,
'w')
61 local relative_path = src_path:gsub(
'[%w_-]+',
'..')
62 local deps_imported = {}
63 local deps_var_name = {}
64 local deps_module_path = {}
65 local main_content =
''
66 local main_before =
''
68 local lib_module = nil
74 if from ==
'system' then
75 main_before =
'local '..lib_var..
' = select(2, pcall(require, \''..lib_module..
'\')) or
'..lib_var..'\n
'..main_before
79 main_before = main_before..'local
'..lib_name..' = nil\n
'
80 main_after = main_after..lib_name..' = function()\n
'
83 local line = src_file:read()
85 line = line:gsub('\n
', '')
86 line = line:gsub('^%s*
', '')
87 line = line:gsub('*%s$
', '')
88 line = line:gsub('^%-%-$
', '')
89 line = line:gsub('^
_ENV = nil$
', '')
90 line = line:gsub('%s*%-%-([^\
'\"%[%]].*)$',
'')
93 local line_require =
line and {
line:match(pattern) }
95 if line_require and #line_require > 0 then
96 local index = #deps_var_name + 1
97 deps_var_name[index] = line_require[1]
98 deps_module_path[index] = line_require[2]
99 if from ==
'main' then
100 main_content = main_content..
'-'..
'-'..line_require[2]..line_require[1]..
'-'..
'-\n'
102 main_after = main_after..
'-'..
'-'..line_require[2]..line_require[1]..
'-'..
'-\n'
105 main_content = main_content..
line..
'\n'
107 main_after = main_after..
line..
'\n'
110 if from ==
'lib' then
111 main_after = main_after..
'end\n'
122 while not src_in and index <=
#deps_var_name do
123 lib_module = deps_module_path[index]
124 lib_var = deps_var_name[index]
125 local lib = lib_module and lib_var and lib_module..lib_var
126 if lib and not deps_imported[lib] then
127 lib_name = lib_module:gsub(
'/',
'_')
128 src_in = src_path..lib_module..'.lua'
129 src_file = io.open(src_in, 'r') or io.open(lib_module..'.lua', 'r')
130 src_file = src_file or io.open(src_path..relative_path..lib_module..'.lua')
131 from = src_file and 'lib' or 'system'
132 deps_imported[lib] =
from
139 while index <=
#deps_var_name do
140 lib_module = deps_module_path[index]
141 lib_name = lib_module:gsub(
'/',
'_')
142 lib_var = deps_var_name[index]
143 local lib = lib_module and lib_var and lib_module..lib_var
144 if lib and deps_imported[lib] then
145 local search = '%-%-'..lib_module..lib_var..'%-%-\n'
146 local
replace = 'local '..lib_var..' = '..lib_name..'()\n'
147 local replacer = deps_imported[lib] == 'system' and '' or
replace
148 main_after = main_after:gsub(search, replacer)
149 main_content = main_content:gsub(search, replacer)
155 main_content = 'local function
main()\n'..main_content..'end\n'
156 main_content = main_before..main_content..main_after
157 main_content = main_content..'return
main()\n'
160 dest_file:write(main_content)
local function from(host_args)
local function file(self, file)
local function require(std, game, application)
local function param(self, name, value)
local function replace(args)
local function line(x1, y1, x2, y2)