summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorruki <[email protected]>2018-09-12 22:38:13 +0800
committerruki <[email protected]>2018-09-12 09:49:20 +0800
commit6638fb5f62e95b465305914953e38233d49a8856 (patch)
treea0a0c7091ba66d65c9d2991825c0600e7c6ab050
parentaa39ccc9c25bd3ecd8ae6cf30e8b447da5ab20b4 (diff)
improve xxx_script to support multi-patterns
-rw-r--r--tests/apis/xxx_script/test.lua1
-rw-r--r--tests/apis/xxx_script/xmake.lua11
-rw-r--r--xmake/core/base/interpreter.lua70
3 files changed, 43 insertions, 39 deletions
diff --git a/tests/apis/xxx_script/test.lua b/tests/apis/xxx_script/test.lua
index bb2762d7d..4347ecd3c 100644
--- a/tests/apis/xxx_script/test.lua
+++ b/tests/apis/xxx_script/test.lua
@@ -1,4 +1,5 @@
function main()
+ os.exec("xmake f -c")
os.exec("xmake")
if os.host() == "macosx" then
os.exec("xmake f -p iphoneos")
diff --git a/tests/apis/xxx_script/xmake.lua b/tests/apis/xxx_script/xmake.lua
index 5b529622a..c3acce0ab 100644
--- a/tests/apis/xxx_script/xmake.lua
+++ b/tests/apis/xxx_script/xmake.lua
@@ -1,12 +1,7 @@
target("test")
- before_build("iphoneos|arm64", function (target)
- assert(val("plat") == "iphoneos")
- assert(val("arch") == "arm64")
- end)
-
- before_build("macosx", function (target)
- assert(vformat("$(plat)") == "macosx")
+ before_build("iphoneos|arm64", "macosx", function (target)
+ assert(is_plat("macosx") or (is_plat("iphoneos") and is_arch("arm64")))
end)
before_build(function (target)
@@ -22,5 +17,5 @@ target("test")
end)
after_build("linux|*", function (target)
- assert(val("plat") == "linux")
+ assert(is_plat("linux"))
end)
diff --git a/xmake/core/base/interpreter.lua b/xmake/core/base/interpreter.lua
index 79c782842..1d6a180b2 100644
--- a/xmake/core/base/interpreter.lua
+++ b/xmake/core/base/interpreter.lua
@@ -278,51 +278,59 @@ end
function interpreter:_api_register_xxx_script(scope_kind, action, ...)
-- define implementation
- local implementation = function (self, scope, name, arg1, arg2)
+ local implementation = function (self, scope, name, ...)
-- patch action to name
if action ~= "on" then
name = name .. "_" .. action
end
- -- on_xxx(pattern, script)?
- if arg1 and arg2 then
+ -- get arguments, pattern1, pattern2, ..., script function or name
+ local args = {...}
- -- get pattern
- local pattern = arg1
- assert(type(pattern) == "string")
+ -- get patterns
+ local patterns = {}
+ if #args > 1 then
+ patterns = table.slice(args, 1, #args - 1)
+ end
- -- get script
- local script, errors = self:_script(arg2)
- if not script then
- os.raise("%s_%s(%s, %s): %s", action, name, tostring(arg1), tostring(arg2), errors)
- end
+ -- get script function or name
+ local script_func_or_name = args[#args]
- -- convert pattern to a lua pattern ('*' => '.*')
- pattern = pattern:gsub("([%+%.%-%^%$%(%)%%])", "%%%1")
- pattern = pattern:gsub("%*", "\001")
- pattern = pattern:gsub("\001", ".*")
+ -- get script
+ local script, errors = self:_script(script_func_or_name)
+ if not script then
+ if #patterns > 0 then
+ os.raise("%s_%s(%s, %s): %s", action, name, table.concat(patterns, ', '), tostring(script_func_or_name), errors)
+ else
+ os.raise("%s_%s(%s): %s", action, name, tostring(script_func_or_name), errors)
+ end
+ end
- -- save script
+ -- save script for all patterns
+ if #patterns > 0 then
local scripts = scope[name] or {}
- if type(scripts) == "table" then
- scripts[pattern] = script
- elseif type(scripts) == "function" then
- scripts = {__generic__ = scripts}
- scripts[pattern] = script
- end
- scope[name] = scripts
+ for _, pattern in ipairs(patterns) do
- -- on_xxx(script)?
- elseif arg1 then
+ -- check
+ assert(type(pattern) == "string")
- -- get script
- local script, errors = self:_script(arg1)
- if not script then
- os.raise("%s_%s(%s): %s", action, name, tostring(arg1), errors)
- end
+ -- convert pattern to a lua pattern ('*' => '.*')
+ pattern = pattern:gsub("([%+%.%-%^%$%(%)%%])", "%%%1")
+ pattern = pattern:gsub("%*", "\001")
+ pattern = pattern:gsub("\001", ".*")
- -- save script
+ -- save script
+ if type(scripts) == "table" then
+ scripts[pattern] = script
+ elseif type(scripts) == "function" then
+ scripts = {__generic__ = scripts}
+ scripts[pattern] = script
+ end
+ end
+ scope[name] = scripts
+ else
+ -- save the generic script
local scripts = scope[name]
if type(scripts) == "table" then
scripts["__generic__"] = script