diff options
| author | ruki <[email protected]> | 2016-04-05 20:14:08 +0800 |
|---|---|---|
| committer | ruki <[email protected]> | 2016-04-05 20:14:08 +0800 |
| commit | 444f3f3f9e9dabf46da999d6d8d6f200c672a866 (patch) | |
| tree | c291d75f7a036ad10122de3bcc7dfd34b511e7ce | |
| parent | ee1293db712b47706496da86e0564010bafdaaa9 (diff) | |
save option and run new task
| -rw-r--r-- | xmake/core/base/option.lua | 123 | ||||
| -rw-r--r-- | xmake/core/main.lua | 5 | ||||
| -rw-r--r-- | xmake/core/project/task.lua | 16 | ||||
| -rw-r--r-- | xmake/core/sandbox/modules/import/core/project/task.lua | 61 | ||||
| -rw-r--r-- | xmake/core/sandbox/modules/os.lua | 12 |
5 files changed, 108 insertions, 109 deletions
diff --git a/xmake/core/base/option.lua b/xmake/core/base/option.lua index 053bb85dd..144b41edc 100644 --- a/xmake/core/base/option.lua +++ b/xmake/core/base/option.lua @@ -62,7 +62,7 @@ function option._taskmenu(task) assert(option._MENU) -- the current task - task = task or option._TASK or "main" + task = task or option.taskname() or "main" -- get the task menu local taskmenu = option._MENU[task] @@ -79,6 +79,42 @@ function option._taskmenu(task) return taskmenu end +-- get the top context +function option._context() + + -- the contexts + local contexts = option._CONTEXTS + assert(contexts) + + -- get it + return contexts[#contexts] +end + +-- save context +function option.save(taskname) + + -- init contexts + option._CONTEXTS = option._CONTEXTS or {} + + -- new a context + local context = {options = {}, defaults = {}, taskname = taskname} + + -- push this new context to the top stack + table.insert(option._CONTEXTS, context) + + -- ok + return context +end + +-- restore context +function option.restore() + + -- pop it + if option._CONTEXTS then + table.remove(option._CONTEXTS) + end +end + -- init the option function option.init(argv, menu) @@ -92,11 +128,11 @@ function option.init(argv, menu) local main = option._taskmenu("main") assert(main) - -- init _OPTIONS - option._OPTIONS = {} - option._DEFAULTS = {} + -- new top context + local context = option.save() + assert(context) - -- parse _ARGV to _OPTIONS + -- parse _ARGV local _iter, _s, _k = ipairs(argv) while true do @@ -139,7 +175,7 @@ function option.init(argv, menu) print("invalid option: " .. arg) -- print menu - option.print_menu(option._TASK) + option.show_menu(context.taskname) -- failed return false @@ -173,7 +209,7 @@ function option.init(argv, menu) print("invalid option: " .. arg) -- print menu - option.print_menu(option._TASK) + option.show_menu(context.taskname) -- failed return false @@ -193,7 +229,7 @@ function option.init(argv, menu) print("invalid option: " .. option._ifelse(idx, arg, key)) -- print menu - option.print_menu(option._TASK) + option.show_menu(context.taskname) -- failed return false @@ -210,7 +246,7 @@ function option.init(argv, menu) print("invalid option: " .. arg) -- print menu - option.print_menu(option._TASK) + option.show_menu(context.taskname) -- failed return false @@ -224,7 +260,7 @@ function option.init(argv, menu) end -- save option - option._OPTIONS[option._ifelse(prefix == 1 and opt[2], opt[2], key)] = value + context.options[option._ifelse(prefix == 1 and opt[2], opt[2], key)] = value -- task? elseif idx == 1 then @@ -235,19 +271,19 @@ function option.init(argv, menu) -- ok? if taskname == key or taskinfo.shortname == key then -- save this task - option._TASK = taskname + context.taskname = taskname break end end -- not found? - if not option._TASK or not menu[option._TASK] then + if not context.taskname or not menu[context.taskname] then -- invalid task print("invalid task: " .. key) -- print the main menu - option.print_main() + option.show_main() -- failed return false @@ -271,7 +307,7 @@ function option.init(argv, menu) assert(o and ((mode ~= "v" and mode ~= "vs") or name)) -- is value and with name? - if mode == "v" and name and not option._OPTIONS[name] then + if mode == "v" and name and not context.options[name] then opt = o break -- is values and with name? @@ -292,13 +328,13 @@ function option.init(argv, menu) -- save value if mode == "v" then - option._OPTIONS[name] = key + context.options[name] = key elseif mode == "vs" then -- the option - local o = option._OPTIONS[name] + local o = context.options[name] if not o then - option._OPTIONS[name] = {} - o = option._OPTIONS[name] + context.options[name] = {} + o = context.options[name] end -- append value @@ -309,7 +345,7 @@ function option.init(argv, menu) print("invalid option: " .. arg) -- print menu - option.print_menu(option._TASK) + option.show_menu(context.taskname) -- failed return false @@ -329,11 +365,11 @@ function option.init(argv, menu) assert(key) -- save the default value - option._DEFAULTS[key] = o[4] + context.defaults[key] = o[4] -- value with name? elseif o[3] == "v" and o[2] then -- save the default value - option._DEFAULTS[o[2]] = o[4] + context.defaults[o[2]] = o[4] end end @@ -373,11 +409,11 @@ function option.find(argv, name, shortname) end end --- get the current task -function option.task() +-- get the current task name +function option.taskname() -- get it - return option._TASK + return option._context().taskname end -- get the given option value for the current task @@ -394,6 +430,23 @@ function option.get(name) return options[name] or option.default(name) end +-- set the given option for the current task +function option.set(name, value) + + -- check + assert(name) + + -- cannot be the first context for menu + assert(#option._CONTEXTS > 1) + + -- the options + local options = option.options() + assert(options) + + -- set it + options[name] = value +end + -- get the given default option value for the current task function option.default(name) @@ -412,7 +465,7 @@ end function option.options() -- get it - return option._OPTIONS + return option._context().options end -- get all default options for the current or given task @@ -420,7 +473,7 @@ function option.defaults(task) -- get the default options for the current task if task == nil then - return option._DEFAULTS + return option._context().defaults end -- get the default options for the given task @@ -447,12 +500,12 @@ function option.defaults(task) return defaults end --- print the menu -function option.print_menu(task) +-- show the menu +function option.show_menu(task) -- no task? print main menu if not task then - option.print_main() + option.show_main() return end @@ -488,12 +541,12 @@ function option.print_menu(task) -- print options if taskmenu.options then - option.print_options(taskmenu.options) + option.show_options(taskmenu.options) end end --- print the main menu -function option.print_main() +-- show the main menu +function option.show_main() -- the menu local menu = option._MENU @@ -603,12 +656,12 @@ function option.print_main() -- print options if main.options then - option.print_options(main.options) + option.show_options(main.options) end end --- print the options menu -function option.print_options(options) +-- show the options menu +function option.show_options(options) -- check assert(options) diff --git a/xmake/core/main.lua b/xmake/core/main.lua index dab1627db..589f1ae1c 100644 --- a/xmake/core/main.lua +++ b/xmake/core/main.lua @@ -52,7 +52,7 @@ function main._help() if option.get("help") then -- print menu - option.print_menu(option.task()) + option.show_menu(option.taskname()) -- ok return true @@ -117,8 +117,9 @@ function main.done() end -- run task - local ok = task.run(option.task() or "build") + local ok, errors = task.run(option.taskname() or "build") if not ok then + utils.error(errors) return -1 end diff --git a/xmake/core/project/task.lua b/xmake/core/project/task.lua index ee68c92c1..22cee9714 100644 --- a/xmake/core/project/task.lua +++ b/xmake/core/project/task.lua @@ -373,18 +373,15 @@ function task.run(name) -- get the task info local taskinfo = tasks[name] if not taskinfo then - utils.error("task(\"%s\"): unknown task", name) - return false + return false, string.format("task(\"%s\"): unknown task", name) end -- check if not taskinfo.run then - utils.error("task(\"%s\"): no run script, please call on_task_run() first!", name) - return false + return false, string.format("task(\"%s\"): no run script, please call on_task_run() first!", name) end -- run on_before, on_run, on_after - local ok = true local on_scripts = {taskinfo.before, taskinfo.run, taskinfo.after} for i = 1, 3 do @@ -393,16 +390,15 @@ function task.run(name) if on_script then -- call it in the sandbox - ok, errors = sandbox.load(on_script) + local ok, errors = sandbox.load(on_script) if not ok then - utils.error(errors) - break + return false, errors end end end - -- ok? - return ok + -- ok + return true end -- the menu diff --git a/xmake/core/sandbox/modules/import/core/project/task.lua b/xmake/core/sandbox/modules/import/core/project/task.lua index fac132ce9..99ef6ea24 100644 --- a/xmake/core/sandbox/modules/import/core/project/task.lua +++ b/xmake/core/sandbox/modules/import/core/project/task.lua @@ -33,10 +33,7 @@ local task = require("project/task") local raise = require("sandbox/modules/raise") -- run the given task -function sandbox_core_project_task.run(taskname, options, ...) - - -- init values - local values = table.wrap(...) +function sandbox_core_project_task.run(taskname, options) -- init options options = table.wrap(options) @@ -48,58 +45,22 @@ function sandbox_core_project_task.run(taskname, options, ...) end end - -- FIXME --verbose no value - -- make command - local cmd = "xmake " .. (taskname or "") - for name, value in pairs(options) do - cmd = string.format("%s --%s=%s", cmd, name, tostring(value)) - end - for _, value in pairs(values) do - cmd = string.format("%s %s", cmd, value) - end + -- save the current option and push a new option context + option.save(taskname) - -- run command - if 0 ~= os.execute(cmd) then - os.raise("run task: %s failed!", taskname or cmd) - end -end - --- quietly run the given task -function sandbox_core_project_task.qrun(taskname, options, ...) - - -- init values - local values = table.wrap(...) - - -- init options - options = table.wrap(options) - - -- inherit some parent options - for _, name in ipairs({"file", "project", "verbose"}) do - if not options[name] and option.get(name) then - options[name] = option.get(name) - end - end - - -- make command - local cmd = "xmake " .. (taskname or "") + -- init the new options for name, value in pairs(options) do - cmd = string.format("%s --%s=%s", cmd, name, tostring(value)) + option.set(name, value) end - for _, value in pairs(values) do - cmd = string.format("%s %s", cmd, value) - end - - -- make temporary log file - local log = os.tmpname() - -- run command - if 0 ~= os.execute(cmd .. string.format(" > %s 2>&1", log)) then - io.cat(log) - os.raise("run task: %s failed!", taskname or cmd) + -- run the task + local ok, errors = task.run(taskname) + if not ok then + raise(errors) end - -- remove the temporary log file - os.rm(log) + -- restore the previous option context + option.restore() end -- return module diff --git a/xmake/core/sandbox/modules/os.lua b/xmake/core/sandbox/modules/os.lua index 8b2ab34ff..a05e26939 100644 --- a/xmake/core/sandbox/modules/os.lua +++ b/xmake/core/sandbox/modules/os.lua @@ -165,18 +165,6 @@ function sandbox_os.run(cmd, ...) -- make command cmd = vformat(cmd, ...) - -- run command - if 0 ~= os.execute(cmd) then - os.raise("run: %s failed!", cmd) - end -end - --- quietly run shell -function sandbox_os.qrun(cmd, ...) - - -- make command - cmd = vformat(cmd, ...) - -- make temporary log file local log = os.tmpname() |
