enhance config include

This commit is contained in:
Cloud Wu
2017-03-21 11:49:04 +08:00
parent 67b055009d
commit 3a4a673b43
3 changed files with 31 additions and 15 deletions

View File

@@ -83,18 +83,32 @@ int sigign() {
}
static const char * load_config = "\
local config_name = ...\n\
local f = assert(io.open(config_name))\n\
local code = assert(f:read \'*a\')\n\
local function getenv(name) return assert(os.getenv(name), \'os.getenv() failed: \' .. name) end\n\
code = string.gsub(code, \'%$([%w_%d]+)\', getenv)\n\
f:close()\n\
local result = {}\n\
local function getenv(name) return assert(os.getenv(name), [[os.getenv() failed: ]] .. name) end\n\
local sep = package.config:sub(1,1)\n\
local current_path = [[.]]..sep\n\
local function include(filename)\n\
assert(loadfile(filename, \'t\', result))()\n\
local last_path = current_path\n\
local path, name = filename:match([[(.*]]..sep..[[)(.*)$]])\n\
if path then\n\
if path:sub(1,1) == sep then -- root\n\
current_path = path\n\
else\n\
current_path = current_path .. path\n\
end\n\
else\n\
name = filename\n\
end\n\
local f = assert(io.open(current_path .. name))\n\
local code = assert(f:read [[*a]])\n\
code = string.gsub(code, [[%$([%w_%d]+)]], getenv)\n\
f:close()\n\
assert(load(code,[[@]]..filename,[[t]],result))()\n\
current_path = last_path\n\
end\n\
setmetatable(result, { __index = { include = include } })\n\
assert(load(code,\'=(load)\',\'t\',result))()\n\
local config_name = ...\n\
include(config_name)\n\
setmetatable(result, nil)\n\
return result\n\
";