summaryrefslogtreecommitdiff
path: root/xmake/core/project/task.lua
diff options
context:
space:
mode:
authorruki <[email protected]>2016-03-28 13:04:24 +0800
committerruki <[email protected]>2016-03-28 13:04:24 +0800
commit7edb31caaa3d1a0d58737324b6af85cb3b2d2c75 (patch)
treee041e67878bdaef387742986debb08b639274995 /xmake/core/project/task.lua
parentf86b3d2ae37f1bb0633246f7d1cea81f996b7d48 (diff)
optimizate option menu
Diffstat (limited to 'xmake/core/project/task.lua')
-rw-r--r--xmake/core/project/task.lua232
1 files changed, 127 insertions, 105 deletions
diff --git a/xmake/core/project/task.lua b/xmake/core/project/task.lua
index eb93012b2..b4690c027 100644
--- a/xmake/core/project/task.lua
+++ b/xmake/core/project/task.lua
@@ -43,6 +43,109 @@ function task._directories()
}
end
+-- translate menu
+function task._translate_menu(menu)
+
+ -- check
+ assert(menu)
+
+ -- the interpreter
+ local interp = task._interpreter()
+ assert(interp)
+
+ -- translate options
+ local options = menu.options
+ if options then
+
+ -- make full options
+ local options_full = {}
+ for _, opt in ipairs(options) do
+
+ -- this option is function? translate it
+ if type(opt) == "function" then
+
+ -- call menu script in the sandbox
+ local ok, results = sandbox.load(opt)
+ if ok then
+ if results then
+ for _, opt in ipairs(results) do
+ table.insert(options_full, opt)
+ end
+ end
+ else
+ -- errors
+ os.raise("taskmenu: %s", results)
+ end
+ else
+ table.insert(options_full, opt)
+ end
+ end
+
+ -- update the options
+ options = options_full
+ menu.options = options_full
+
+ -- filter options
+ if interp:filter() then
+
+ -- filter option
+ for _, opt in ipairs(options) do
+
+ -- filter default
+ local default = opt[4]
+ if type(default) == "string" then
+ opt[4] = interp:filter():handle(default)
+ end
+
+ -- filter description
+ for i = 5, 64 do
+
+ -- the description, @note some option may be nil
+ local description = opt[i]
+ if not description then break end
+
+ -- the description is string?
+ if type(description) == "string" then
+ opt[i] = interp:filter():handle(description)
+
+ -- the description is function? wrap it for calling it in the sandbox
+ elseif type(description) == "function" then
+ opt[i] = function ()
+
+ -- call it in the sandbox
+ local ok, results = sandbox.load(description)
+ if not ok then
+ -- errors
+ os.raise("taskmenu: %s", results)
+ end
+
+ -- ok
+ return results
+ end
+ end
+ end
+ end
+ end
+
+ -- add common options
+ table.insert(options, 1, {'v', "verbose", "k", nil, "Print lots of verbose information." })
+ table.insert(options, 2, {nil, "version", "k", nil, "Print the version number and exit." })
+ table.insert(options, 3, {'h', "help", "k", nil, "Print this help message and exit." })
+ table.insert(options, 4, {})
+ table.insert(options, 5, {'f', "file", "kv", nil, "Read a given xmake.lua file." })
+ table.insert(options, 6, {'P', "project", "kv", nil, "Change to the given project directory."
+ , "Search priority:"
+ , " 1. The Given Command Argument"
+ , " 2. The Envirnoment Variable: XMAKE_PROJECT_DIR"
+ , " 3. The Current Directory" })
+ table.insert(options, 7, {})
+
+ end
+
+ -- ok
+ return menu
+end
+
-- the interpreter
function task._interpreter()
@@ -323,10 +426,6 @@ function task.menu()
local tasks = task.tasks()
assert(tasks)
- -- the interpreter
- local interp = task._interpreter()
- assert(interp)
-
-- make menu
local menu = {}
for taskname, taskinfo in pairs(tasks) do
@@ -334,117 +433,40 @@ function task.menu()
-- has menu?
if taskinfo.menu then
- -- translate options
- local options = taskinfo.menu.options
- if options then
-
- -- make full options
- local options_full = {}
- for _, opt in ipairs(options) do
-
- -- this option is function? translate it
- if type(opt) == "function" then
-
- -- call menu script in the sandbox
- local ok, results = sandbox.load(opt)
- if ok then
- if results then
- for _, opt in ipairs(results) do
- table.insert(options_full, opt)
- end
- end
- else
- -- errors
- os.raise("taskmenu: %s", results)
- end
- else
- table.insert(options_full, opt)
- end
- end
-
- -- update the options
- options = options_full
- taskinfo.menu.options = options_full
-
- -- filter options
- if interp:filter() then
-
- -- filter option
- for _, opt in ipairs(options) do
-
- -- filter default
- local default = opt[4]
- if type(default) == "string" then
- opt[4] = interp:filter():handle(default)
- end
+ -- main?
+ if taskinfo.category == "main" then
- -- filter description
- for i = 5, 64 do
+ -- delay to load main menu
+ menu.main = function ()
- -- the description, @note some option may be nil
- local description = opt[i]
- if not description then break end
+ -- translate main menu
+ local mainmenu = task._translate_menu(taskinfo.menu)
- -- the description is string?
- if type(description) == "string" then
- opt[i] = interp:filter():handle(description)
+ -- make tasks for the main menu
+ mainmenu.tasks = {}
+ for name, info in pairs(tasks) do
- -- the description is function? wrap it for calling it in the sandbox
- elseif type(description) == "function" then
- opt[i] = function ()
-
- -- call it in the sandbox
- local ok, results = sandbox.load(description)
- if not ok then
- -- errors
- os.raise("taskmenu: %s", results)
- end
+ -- has menu?
+ if info.menu then
- -- ok
- return results
- end
- end
+ -- add task
+ mainmenu.tasks[name] =
+ {
+ category = info.category
+ , shortname = info.menu.shortname
+ , description = info.menu.description
+ }
end
end
- end
-
- -- add common options
- table.insert(options, 1, {'v', "verbose", "k", nil, "Print lots of verbose information." })
- table.insert(options, 2, {nil, "version", "k", nil, "Print the version number and exit." })
- table.insert(options, 3, {'h', "help", "k", nil, "Print this help message and exit." })
- table.insert(options, 4, {})
- table.insert(options, 5, {'f', "file", "kv", nil, "Read a given xmake.lua file." })
- table.insert(options, 6, {'P', "project", "kv", nil, "Change to the given project directory."
- , "Search priority:"
- , " 1. The Given Command Argument"
- , " 2. The Envirnoment Variable: XMAKE_PROJECT_DIR"
- , " 3. The Current Directory" })
- table.insert(options, 7, {})
+ -- ok
+ return mainmenu
+ end
end
- -- main?
- if taskinfo.category == "main" then
- menu.main = taskinfo.menu
- end
-
- -- add menu
- menu[taskname] = taskinfo.menu
- end
- end
-
- -- add tasks to the main menu
- if menu.main then
-
- -- make tasks for the main menu
- menu.main.tasks = menu.main.tasks or {}
- for taskname, taskinfo in pairs(tasks) do
-
- -- has menu?
- if taskinfo.menu then
-
- -- add task
- menu.main.tasks[taskname] = taskinfo
+ -- delay to load task menu
+ menu[taskname] = function ()
+ return task._translate_menu(taskinfo.menu)
end
end
end