-
Notifications
You must be signed in to change notification settings - Fork 0
/
amalg_util.lua
80 lines (68 loc) · 2.09 KB
/
amalg_util.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
local constants = require("luasql.exasol.constants")
local log = require("remotelog")
local M = {}
local THIRD_PARTY_MODULE_NAMES<const> = {"remotelog", "ExaError", "MessageExpander"}
local function get_rockspec_filename() --
return string.format("luasql-exasol-%s.rockspec", constants.VERSION)
end
local function load_rockspec(path)
local env = {}
local rockspec_function = assert(loadfile(path, "t", env))
rockspec_function()
return env
end
local function get_driver_module_names()
local rockspec = load_rockspec(get_rockspec_filename())
local modules = {}
for module_name, _ in pairs(rockspec.build.modules) do
table.insert(modules, module_name)
end
return modules
end
local function amalgamate(lua_path, modules, script_path)
local command = "LUA_PATH=" .. lua_path .. " amalg.lua --fallback"
if script_path then
command = command .. " --script=" .. script_path
end
command = command .. " " .. table.concat(modules, " ")
log.debug("Running amalg command: %s", command)
local file = io.popen(command, "r")
local output = file:read("*all")
file:close()
return output
end
local function write_temp_file(content)
if not content then
return nil
end
local path = os.tmpname()
local temp_file = assert(io.open(path, "w"))
temp_file:write(content)
temp_file:close()
return path
end
local function get_lua_path()
return "src/?.lua"
end
local function insert_all(target, list)
for _, value in ipairs(list) do
table.insert(target, value)
end
end
local function concat_tables(list1, list2)
local result = {}
insert_all(result, list1)
insert_all(result, list2)
return result
end
function M.amalgamate_with_script(script_content)
local modules = concat_tables(get_driver_module_names(), THIRD_PARTY_MODULE_NAMES)
local script_path = write_temp_file(script_content)
local lua_path = get_lua_path()
local content = amalgamate(lua_path, modules, script_path)
if script_path then
assert(os.remove(script_path))
end
return content
end
return M