diff options
| author | ruki <[email protected]> | 2015-05-11 22:43:21 +0800 |
|---|---|---|
| committer | ruki <[email protected]> | 2015-05-11 22:43:21 +0800 |
| commit | 3b93e699c16862a1c29c2047425ce3d8a4827ea7 (patch) | |
| tree | b83a3422f28efc68b2c66821681ddf117522986d /xmake/scripts | |
| parent | f2313cc0c48b37b7aa6222457325a23ad62e1fd8 (diff) | |
save and load .config.lua
Diffstat (limited to 'xmake/scripts')
| -rw-r--r-- | xmake/scripts/action/config.lua | 145 | ||||
| -rw-r--r-- | xmake/scripts/base/main.lua | 20 |
2 files changed, 161 insertions, 4 deletions
diff --git a/xmake/scripts/action/config.lua b/xmake/scripts/action/config.lua index 51c6f8d66..d8767171b 100644 --- a/xmake/scripts/action/config.lua +++ b/xmake/scripts/action/config.lua @@ -23,9 +23,152 @@ -- define module: config local config = config or {} +-- load modules +local utils = require("base/utils") + +-- save option to the file +function config._save_option(file, option) + + -- check + assert(file and option) + + -- save number + if type(option) == "number" then + file:write(option) + -- save string + elseif type(option) == "string" then + file:write(string.format("%q", option)) + -- save table + elseif type(option) == "table" then + + -- save head + file:write("{\n") + + -- save body + local i = 0 + for k, v in pairs(option) do + + -- skip --project and --file + if k ~= "project" and k ~= "file" and k ~= "verbose" and k ~= "_ACTION" then + + -- save separator + file:write(utils.ifelse(i == 0, " ", ", "), k, " = ") + + -- save this key: value + if not config._save_option(file, v) then + return false + end + + -- save newline + file:write("\n") + i = i + 1 + end + end + + -- save tail + file:write("}\n") + else + -- error + utils.error("invalid option type: %s", type(option)) + return false + end + + -- ok + return true +end + +-- save xmake._OPTIONS to the configure file +function config._save() + + -- the options + local options = xmake._OPTIONS + assert(options) + + -- open the configure file + local path = options.project .. "/.config.lua" + local file = io.open(path, "w") + if not file then + -- error + utils.error("open %s failed!", path) + return false + end + + -- save return to file + file:write("return \n") + + -- save options to file + if not config._save_option(file, options) then + -- error + utils.error("save %s failed!", path) + return false + end + + -- close file + file:close() + + -- ok + return true +end + +-- load configs to xmake._OPTIONS from the configure file +function config._load() + + -- the options + local options = xmake._OPTIONS + assert(options) + + -- open the configure file + local path = options.project .. "/.config.lua" + local file = loadfile(path) + if file then + -- execute it + local ok, configs = pcall(file) + if not ok then + -- error + utils.error("load %s failed!", path) + utils.error(configs) + return + end + + -- check + assert(configs and type(configs) == "table") + + -- ok? merge xmake._OPTIONS to configs + for k, v in pairs(xmake._OPTIONS) do + if type(k) == "string" then + configs[k] = v + else + -- error + utils.error("invalid option type: %s", type(k)) + return + end + end + + -- update configs to xmake._OPTIONS + xmake._OPTIONS = configs + else + -- error + utils.error("%s not found!", path) + end +end + -- done the given config function config.done() - print("config") + + -- attempt to load configs to xmake._OPTIONS from the configure file + config._load() + + -- save options to the configure file + if not config._save() then + return false + end + + -- dump + for k, v in pairs(xmake._OPTIONS) do + utils.printf("%s = %s", k, v) + end + + -- ok return true end diff --git a/xmake/scripts/base/main.lua b/xmake/scripts/base/main.lua index e9d3d2af9..eb1a57c3d 100644 --- a/xmake/scripts/base/main.lua +++ b/xmake/scripts/base/main.lua @@ -258,7 +258,7 @@ local menu = } -- done option -function main.done_option() +function main._done_option() -- the options local options = xmake._OPTIONS @@ -275,7 +275,21 @@ function main.done_option() assert(options.file) -- load and execute the xmake.lua script of the given project - dofile(options.file) + local script = loadfile(options.file) + if script then + -- execute it + local ok, err = pcall(script) + if not ok then + -- error + utils.error("load %s failed!", options.file) + utils.error(err) + return false + end + else + -- error + utils.error("%s not found!", options.file) + return false + end -- done help if options.help then @@ -316,7 +330,7 @@ function main.done() end -- done option - if not main.done_option() then + if not main._done_option() then return -1 end |
