summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorruki <[email protected]>2016-02-26 22:31:19 +0800
committerruki <[email protected]>2016-02-26 22:31:19 +0800
commit5520585557e76e13368325c50ffb0ca344f2c714 (patch)
tree83b8859bdab03b31cfed75fed461b4e4fbdc72c3
parent939effc6597b544d498d0644f7b48cb121f54aae (diff)
load script in task menu
-rw-r--r--xmake/core/base/option.lua28
-rw-r--r--xmake/core/base/task.lua120
-rw-r--r--xmake/core/base/utils.lua5
-rw-r--r--xmake/core/sandbox/import.lua4
-rw-r--r--xmake/core/sandbox/import/platform.lua59
-rw-r--r--xmake/core/sandbox/import/project.lua26
-rw-r--r--xmake/core/sandbox/import/template.lua52
-rw-r--r--xmake/core/sandbox/os.lua26
-rw-r--r--xmake/core/sandbox/string.lua8
-rw-r--r--xmake/core/sandbox/utils.lua8
-rwxr-xr-xxmake/core/tasks/config.lua73
-rwxr-xr-xxmake/core/tasks/create.lua47
-rwxr-xr-xxmake/core/tasks/global.lua12
-rwxr-xr-xxmake/core/tasks/package.lua34
14 files changed, 354 insertions, 148 deletions
diff --git a/xmake/core/base/option.lua b/xmake/core/base/option.lua
index f0e9a262b..473830518 100644
--- a/xmake/core/base/option.lua
+++ b/xmake/core/base/option.lua
@@ -46,34 +46,6 @@ function option._translate(menu)
end
table.copy2(menu, submenus_all)
- -- translate it if exists function in the option menu
- for _, submenu in pairs(menu) do
-
- -- exits options?
- if submenu.options then
-
- -- translate options
- local options_all = {}
- for _, option in ipairs(submenu.options) do
-
- -- this option is function? translate it
- if type(option) == "function" then
- local _options = option()
- if _options then
- for _, o in ipairs(_options) do
- table.insert(options_all, o)
- end
- end
- else
- table.insert(options_all, option)
- end
- end
-
- -- update the options
- submenu.options = options_all
- end
- end
-
-- save menu
option._MENU = menu
diff --git a/xmake/core/base/task.lua b/xmake/core/base/task.lua
index 6eba98114..11643ee7e 100644
--- a/xmake/core/base/task.lua
+++ b/xmake/core/base/task.lua
@@ -30,6 +30,7 @@ local utils = require("base/utils")
local filter = require("base/filter")
local string = require("base/string")
local global = require("base/global")
+local sandbox = require("base/sandbox")
local interpreter = require("base/interpreter")
-- the directories of tasks
@@ -108,6 +109,23 @@ function task._load(filepath)
return results
end
+-- call the sandbox script
+function task._call(script, ...)
+
+ -- get interpreter
+ local interp = task._interpreter()
+ assert(interp)
+
+ -- bind script and get new script with sandbox
+ local newscript, errors = sandbox.bind(script, interp)
+ if not newscript then
+ return false, errors
+ end
+
+ -- load it
+ return sandbox.load(script, ...)
+end
+
-- get all tasks
function task.tasks()
@@ -161,9 +179,83 @@ function task.menu()
-- has menu?
if taskinfo.menu then
- -- add common options
+ -- translate options
local options = taskinfo.menu.options
if options then
+
+ -- make full options
+ local options_full = {}
+ for _, option in ipairs(options) do
+
+ -- this option is function? translate it
+ if type(option) == "function" then
+
+ -- call menu script in the sandbox
+ local ok, results = task._call(option)
+ if ok then
+ if results then
+ for _, opt in ipairs(results) do
+ table.insert(options_full, opt)
+ end
+ end
+ else
+ -- errors
+ utils.error("taskmenu: %s", results)
+ utils.abort()
+ end
+ else
+ table.insert(options_full, option)
+ end
+ end
+
+ -- update the options
+ options = options_full
+ taskinfo.menu.options = options_full
+
+ -- filter options
+ if interp:filter() then
+
+ -- filter option
+ for _, option in ipairs(options) do
+
+ -- filter default
+ local default = option[4]
+ if type(default) == "string" then
+ option[4] = interp:filter():handle(default)
+ end
+
+ -- filter description
+ for i = 5, 64 do
+
+ -- the description, @note some option may be nil
+ local description = option[i]
+ if not description then break end
+
+ -- the description is string?
+ if type(description) == "string" then
+ option[i] = interp:filter():handle(description)
+
+ -- the description is function? wrap it for calling it in the sandbox
+ elseif type(description) == "function" then
+ option[i] = function ()
+
+ -- call it in the sandbox
+ local ok, results = task._call(description)
+ if not ok then
+ -- errors
+ utils.error("taskmenu: %s", results)
+ utils.abort()
+ 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." })
@@ -175,33 +267,7 @@ function task.menu()
, " 2. The Envirnoment Variable: XMAKE_PROJECT_DIR"
, " 3. The Current Directory" })
table.insert(options, 7, {})
- end
- -- filter options
- if options and interp:filter() then
-
- -- filter option
- for _, option in ipairs(options) do
-
- -- filter default
- local default = option[4]
- if type(default) == "string" then
- option[4] = interp:filter():handle(default)
- end
-
- -- filter description
- for i = 5, 64 do
-
- -- the description, @note some option may be nil
- local description = option[i]
- if not description then break end
-
- -- the description is string?
- if type(description) == "string" then
- option[i] = interp:filter():handle(description)
- end
- end
- end
end
-- main?
diff --git a/xmake/core/base/utils.lua b/xmake/core/base/utils.lua
index acaf7f57d..f0664f294 100644
--- a/xmake/core/base/utils.lua
+++ b/xmake/core/base/utils.lua
@@ -56,7 +56,10 @@ function utils.error(msg, ...)
print("error: " .. string.format(msg, ...))
end
--- the abort function
+-- the abort function, will raise an exception
+-- and the parent function can capture it
+--
+-- so we do not use os.exit()
function utils.abort()
-- trace
diff --git a/xmake/core/sandbox/import.lua b/xmake/core/sandbox/import.lua
index d216c5c1d..94b062894 100644
--- a/xmake/core/sandbox/import.lua
+++ b/xmake/core/sandbox/import.lua
@@ -24,7 +24,7 @@
local utils = require("base/utils")
-- import module
-function sandbox_builtin_import(name)
+function sandbox_import(name)
-- load module
local module = require("sandbox/import/" .. name)
@@ -48,5 +48,5 @@ function sandbox_builtin_import(name)
end
-- load module
-return sandbox_builtin_import
+return sandbox_import
diff --git a/xmake/core/sandbox/import/platform.lua b/xmake/core/sandbox/import/platform.lua
new file mode 100644
index 000000000..e6de93681
--- /dev/null
+++ b/xmake/core/sandbox/import/platform.lua
@@ -0,0 +1,59 @@
+--!The Automatic Cross-platform Build Tool
+--
+-- XMake is free software; you can redistribute it and/or modify
+-- it under the terms of the GNU Lesser General Public License as published by
+-- the Free Software Foundation; either version 2.1 of the License, or
+-- (at your option) any later version.
+--
+-- XMake is distributed in the hope that it will be useful,
+-- but WITHOUT ANY WARRANTY; without even the implied warranty of
+-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+-- GNU Lesser General Public License for more details.
+--
+-- You should have received a copy of the GNU Lesser General Public License
+-- along with XMake;
+-- If not, see <a href="http://www.gnu.org/licenses/"> http://www.gnu.org/licenses/</a>
+--
+-- Copyright (C) 2015 - 2016, ruki All rights reserved.
+--
+-- @author ruki
+-- @file platform.lua
+--
+
+-- define module
+local sandbox_platform = sandbox_platform or {}
+
+-- load modules
+local platform = require("base/platform")
+
+-- get the platform menu
+function sandbox_platform.menu(action)
+
+ -- get it
+ return platform.menu(action)
+end
+
+-- get the all platforms
+function sandbox_platform.plats()
+
+ -- get it
+ local plats = platform.plats()
+ assert(plats)
+
+ -- ok
+ return plats
+end
+
+-- get the all architectures for the given platform
+function sandbox_platform.archs(plat)
+
+ -- get it
+ local archs = platform.archs(plat)
+ assert(archs)
+
+ -- ok
+ return archs
+end
+
+-- return module
+return sandbox_platform
diff --git a/xmake/core/sandbox/import/project.lua b/xmake/core/sandbox/import/project.lua
index 735a0253d..3d65b8768 100644
--- a/xmake/core/sandbox/import/project.lua
+++ b/xmake/core/sandbox/import/project.lua
@@ -20,47 +20,55 @@
-- @file project.lua
--
--- define module: project
-local project = project or {}
+-- define module
+local sandbox_project = sandbox_project or {}
-- load modules
local rule = require("base/rule")
local config = require("base/config")
+local project = require("base/project")
-- get the build directory
-function project.buildir()
+function sandbox_project.buildir()
-- get it
return config.get("buildir")
end
-- get the project directory
-function project.projectdir()
+function sandbox_project.projectdir()
-- get it
return xmake._PROJECT_DIR
end
-- get the current platform
-function project.plat()
+function sandbox_project.plat()
-- get it
return config.get("plat")
end
-- get the current architecture
-function project.arch()
+function sandbox_project.arch()
-- get it
return config.get("arch")
end
-- get the current mode
-function project.mode()
+function sandbox_project.mode()
-- get it
return config.get("mode")
end
--- return module: project
-return project
+-- get the menu
+function sandbox_project.menu()
+
+ -- get it
+ return project.menu()
+end
+
+-- return module
+return sandbox_project
diff --git a/xmake/core/sandbox/import/template.lua b/xmake/core/sandbox/import/template.lua
new file mode 100644
index 000000000..712d01b25
--- /dev/null
+++ b/xmake/core/sandbox/import/template.lua
@@ -0,0 +1,52 @@
+--!The Automatic Cross-template Build Tool
+--
+-- XMake is free software; you can redistribute it and/or modify
+-- it under the terms of the GNU Lesser General Public License as published by
+-- the Free Software Foundation; either version 2.1 of the License, or
+-- (at your option) any later version.
+--
+-- XMake is distributed in the hope that it will be useful,
+-- but WITHOUT ANY WARRANTY; without even the implied warranty of
+-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+-- GNU Lesser General Public License for more details.
+--
+-- You should have received a copy of the GNU Lesser General Public License
+-- along with XMake;
+-- If not, see <a href="http://www.gnu.org/licenses/"> http://www.gnu.org/licenses/</a>
+--
+-- Copyright (C) 2015 - 2016, ruki All rights reserved.
+--
+-- @author ruki
+-- @file template.lua
+--
+
+-- define module
+local sandbox_template = sandbox_template or {}
+
+-- load modules
+local template = require("base/template")
+
+-- get all languages
+function sandbox_template.languages()
+
+ -- get it
+ local languages = template.languages()
+ assert(languages)
+
+ -- ok
+ return languages
+end
+
+-- load all templates from the given language
+function sandbox_template.loadall(language)
+
+ -- get it
+ local templates = template.loadall(language)
+ assert(templates)
+
+ -- ok
+ return templates
+end
+
+-- return module
+return sandbox_template
diff --git a/xmake/core/sandbox/os.lua b/xmake/core/sandbox/os.lua
index ffa9173a0..354e21b01 100644
--- a/xmake/core/sandbox/os.lua
+++ b/xmake/core/sandbox/os.lua
@@ -27,13 +27,13 @@ local utils = require("base/utils")
local vformat = require("sandbox/vformat")
-- define module
-local sandbox_builtin_os = sandbox_builtin_os or {}
+local sandbox_os = sandbox_os or {}
-- inherit the public interfaces of os
-sandbox_builtin_os.curdir = os.curdir
+sandbox_os.curdir = os.curdir
-- copy file or directory
-function sandbox_builtin_os.cp(src, dst)
+function sandbox_os.cp(src, dst)
-- check
assert(src and dst)
@@ -52,7 +52,7 @@ function sandbox_builtin_os.cp(src, dst)
end
-- move file or directory
-function sandbox_builtin_os.mv(src, dst)
+function sandbox_os.mv(src, dst)
-- check
assert(src and dst)
@@ -71,7 +71,7 @@ function sandbox_builtin_os.mv(src, dst)
end
-- remove file or directory
-function sandbox_builtin_os.rm(file_or_dir, emptydir)
+function sandbox_os.rm(file_or_dir, emptydir)
-- check
assert(file_or_dir)
@@ -89,7 +89,7 @@ function sandbox_builtin_os.rm(file_or_dir, emptydir)
end
-- change to directory
-function sandbox_builtin_os.cd(dir)
+function sandbox_os.cd(dir)
-- check
assert(dir)
@@ -107,7 +107,7 @@ function sandbox_builtin_os.cd(dir)
end
-- create directory
-function sandbox_builtin_os.mkdir(dir)
+function sandbox_os.mkdir(dir)
-- check
assert(dir)
@@ -124,7 +124,7 @@ function sandbox_builtin_os.mkdir(dir)
end
-- remove directory
-function sandbox_builtin_os.rmdir(dir)
+function sandbox_os.rmdir(dir)
-- check
assert(dir)
@@ -143,7 +143,7 @@ function sandbox_builtin_os.rmdir(dir)
end
-- run shell
-function sandbox_builtin_os.run(cmd, ...)
+function sandbox_os.run(cmd, ...)
-- make command
cmd = vformat(cmd, ...)
@@ -163,7 +163,7 @@ function sandbox_builtin_os.run(cmd, ...)
end
-- match files or directories
-function sandbox_builtin_os.match(pattern, findir)
+function sandbox_os.match(pattern, findir)
-- check
assert(pattern)
@@ -176,7 +176,7 @@ function sandbox_builtin_os.match(pattern, findir)
end
-- is directory?
-function sandbox_builtin_os.isdir(dirpath)
+function sandbox_os.isdir(dirpath)
-- check
assert(dirpath)
@@ -189,7 +189,7 @@ function sandbox_builtin_os.isdir(dirpath)
end
-- is directory?
-function sandbox_builtin_os.isfile(filepath)
+function sandbox_os.isfile(filepath)
-- check
assert(filepath)
@@ -202,5 +202,5 @@ function sandbox_builtin_os.isfile(filepath)
end
-- return module
-return sandbox_builtin_os
+return sandbox_os
diff --git a/xmake/core/sandbox/string.lua b/xmake/core/sandbox/string.lua
index e0f547008..5812c40dd 100644
--- a/xmake/core/sandbox/string.lua
+++ b/xmake/core/sandbox/string.lua
@@ -25,17 +25,17 @@ local string = require("base/string")
local sandbox = require("base/sandbox")
-- define module
-local sandbox_builtin_string = sandbox_builtin_string or {}
+local sandbox_string = sandbox_string or {}
-- inherit the public interfaces of string
for k, v in pairs(string) do
if not k:startswith("_") and type(v) == "function" then
- sandbox_builtin_string[k] = v
+ sandbox_string[k] = v
end
end
-- format string with the builtin variables
-function sandbox_builtin_string.vformat(format, ...)
+function sandbox_string.vformat(format, ...)
-- check
assert(format)
@@ -59,5 +59,5 @@ function sandbox_builtin_string.vformat(format, ...)
end
-- return module
-return sandbox_builtin_string
+return sandbox_string
diff --git a/xmake/core/sandbox/utils.lua b/xmake/core/sandbox/utils.lua
index 88bcb2226..edc23e032 100644
--- a/xmake/core/sandbox/utils.lua
+++ b/xmake/core/sandbox/utils.lua
@@ -25,22 +25,22 @@ local utils = require("base/utils")
local vformat = require("sandbox/vformat")
-- define module
-local sandbox_builtin_utils = sandbox_builtin_utils or {}
+local sandbox_utils = sandbox_utils or {}
-- inherit the public interfaces of utils
for k, v in pairs(utils) do
if not k:startswith("_") and type(v) == "function" then
- sandbox_builtin_utils[k] = v
+ sandbox_utils[k] = v
end
end
-- printf with the builtin variables
-function sandbox_builtin_utils.printf(format, ...)
+function sandbox_utils.printf(format, ...)
-- done
return print(vformat(format, ...))
end
-- return module
-return sandbox_builtin_utils
+return sandbox_utils
diff --git a/xmake/core/tasks/config.lua b/xmake/core/tasks/config.lua
index 6abd41a01..6cfc587f3 100755
--- a/xmake/core/tasks/config.lua
+++ b/xmake/core/tasks/config.lua
@@ -43,34 +43,43 @@ task("config")
{'c', "clean", "k", nil, "Clean the cached configure and configure all again." }
, {}
---[[ , {'p', "plat", "kv", xmake._HOST, "Compile for the given platform."
+ , {'p', "plat", "kv", "$(host)", "Compile for the given platform."
+
+ -- show the description of all platforms
, function ()
- local descriptions = {}
- local plats = platform.plats()
- if plats then
- for i, plat in ipairs(plats) do
- descriptions[i] = " - " .. plat
- end
+
+ -- import platform
+ import("platform")
+
+ -- make description
+ local description = {}
+ for i, plat in ipairs(platform.plats()) do
+ description[i] = " - " .. plat
end
- return descriptions
+
+ -- get it
+ return description
end }
, {'a', "arch", "kv", "auto", "Compile for the given architecture."
+
+ -- show the description of all architectures
, function ()
- local descriptions = {}
- local plats = platform.plats()
- if plats then
- for i, plat in ipairs(plats) do
- descriptions[i] = " - " .. plat .. ":"
- local archs = platform.archs(plat)
- if archs then
- for _, arch in ipairs(archs) do
- descriptions[i] = descriptions[i] .. " " .. arch
- end
- end
+
+ -- import platform
+ import("platform")
+
+ -- make description
+ local description = {}
+ for i, plat in ipairs(platform.plats()) do
+ description[i] = " - " .. plat .. ":"
+ for _, arch in ipairs(platform.archs(plat)) do
+ description[i] = description[i] .. " " .. arch
end
end
- return descriptions
- end }]]
+
+ -- get it
+ return description
+ end }
, {'m', "mode", "kv", "release", "Compile for the given mode."
, " - debug"
, " - release"
@@ -81,8 +90,15 @@ task("config")
, " - binary" }
, {nil, "host", "kv", "$(host)", "The current host environment." }
- -- the options for project
--- , function () return project.menu() end
+ -- show menu for project
+ , function ()
+
+ -- import platform
+ import("project")
+
+ -- get menu for project
+ return project.menu()
+ end
, {}
, {nil, "make", "kv", "auto", "Set the make path." }
@@ -122,8 +138,15 @@ task("config")
, {nil, "sh", "kv", nil, "The Shared Library Linker" }
, {nil, "shflags", "kv", nil, "The Shared Library Linker Flags" }
- -- the options for all platforms
--- , function () return platform.menu("config") end
+ -- show menu for platform
+ , function ()
+
+ -- import platform
+ import("platform")
+
+ -- get menu for platform
+ return platform.menu("config")
+ end
, {'o', "buildir", "kv", "build", "Set the build directory." }
diff --git a/xmake/core/tasks/create.lua b/xmake/core/tasks/create.lua
index a38989de0..d8fc13a2c 100755
--- a/xmake/core/tasks/create.lua
+++ b/xmake/core/tasks/create.lua
@@ -38,32 +38,45 @@ task("create")
, options =
{
{'n', "name", "kv", nil, "The project name." }
---[[ , {'l', "language", "kv", "c", "The project language"
+ , {'l', "language", "kv", "c", "The project language"
+
+ -- show the description of all languages
, function ()
- local descriptions = {}
- local languages = template.languages()
- for _, language in ipairs(languages) do
- table.insert(descriptions, " - " .. language)
+
+ -- import template
+ import("template")
+
+ -- make description
+ local description = {}
+ for _, language in ipairs(template.languages()) do
+ table.insert(description, " - " .. language)
end
- return descriptions
+
+ -- get it
+ return description
end }
, {'t', "template", "kv", "1", "Select the project template id of the given language."
+
+ -- show the description of all templates
, function ()
- local descriptions = {}
- local languages = template.languages()
- for _, language in ipairs(languages) do
- table.insert(descriptions, string.format(" - language: %s", language))
- local templates = template.loadall(language)
- if templates then
- for i, template in ipairs(templates) do
- table.insert(descriptions, string.format(" %d. %s", i, utils.ifelse(template.description, template.description, "The Unknown Project")))
- end
+
+ -- import template
+ import("template")
+
+ -- make description
+ local description = {}
+ for _, language in ipairs(template.languages()) do
+ table.insert(description, format(" - language: %s", language))
+ for i, t in ipairs(template.loadall(language)) do
+ table.insert(description, format(" %d. %s", i, utils.ifelse(t.description, t.description, "The Unknown Project")))
end
end
- return descriptions
+
+ -- get it
+ return description
end }
- , {}]]
+ , {}
, {}
, {nil, "target", "v", nil, "Create the given target."
, "Uses the project name as target if not exists." }
diff --git a/xmake/core/tasks/global.lua b/xmake/core/tasks/global.lua
index 187770b1c..5d1abced7 100755
--- a/xmake/core/tasks/global.lua
+++ b/xmake/core/tasks/global.lua
@@ -46,8 +46,16 @@ task("global")
, " --ccache=[y|n]" }
, {}
- -- the options for all platforms
--- , function () return platform.menu("global") end
+
+ -- show menu for platform
+ , function ()
+
+ -- import platform
+ import("platform")
+
+ -- get menu for platform
+ return platform.menu("global")
+ end
}
})
diff --git a/xmake/core/tasks/package.lua b/xmake/core/tasks/package.lua
index ef2db6cd1..bc63a338b 100755
--- a/xmake/core/tasks/package.lua
+++ b/xmake/core/tasks/package.lua
@@ -40,25 +40,27 @@ task("package")
-- options
, options =
{
---[[ {'a', "archs", "kv", nil, "Package multiple given architectures."
+ {'a', "archs", "kv", nil, "Package multiple given architectures."
, " .e.g --archs=\"armv7, arm64\" or -a i386"
, ""
+ -- show the description of all architectures
, function ()
- local descriptions = {}
- local plats = platform.plats()
- if plats then
- for i, plat in ipairs(plats) do
- descriptions[i] = " - " .. plat .. ":"
- local archs = platform.archs(plat)
- if archs then
- for _, arch in ipairs(archs) do
- descriptions[i] = descriptions[i] .. " " .. arch
- end
- end
- end
- end
- return descriptions
- end }]]
+
+ -- import platform
+ import("platform")
+
+ -- make description
+ local description = {}
+ for i, plat in ipairs(platform.plats()) do
+ description[i] = " - " .. plat .. ":"
+ for _, arch in ipairs(platform.archs(plat)) do
+ description[i] = description[i] .. " " .. arch
+ end
+ end
+
+ -- get it
+ return description
+ end }
{'o', "outputdir", "kv", nil, "Set the output directory." }