summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorruki <[email protected]>2025-01-07 00:59:53 +0800
committerruki <[email protected]>2025-01-07 00:59:53 +0800
commitbdc9a4cb46d5dd7d46dfa9a0ab2ec29aa9a2e6a8 (patch)
treec384c9db27b4b77a9e9fe5f6fd772da60ca90adf
parente66776b2980aec6bf3379814aae68882dcfb516e (diff)
add namespace for task
-rw-r--r--tests/apis/namespace/task/.gitignore8
-rw-r--r--tests/apis/namespace/task/test.lua5
-rw-r--r--tests/apis/namespace/task/xmake.lua22
-rw-r--r--xmake/core/base/task.lua20
4 files changed, 53 insertions, 2 deletions
diff --git a/tests/apis/namespace/task/.gitignore b/tests/apis/namespace/task/.gitignore
new file mode 100644
index 000000000..152105761
--- /dev/null
+++ b/tests/apis/namespace/task/.gitignore
@@ -0,0 +1,8 @@
+# Xmake cache
+.xmake/
+build/
+
+# MacOS Cache
+.DS_Store
+
+
diff --git a/tests/apis/namespace/task/test.lua b/tests/apis/namespace/task/test.lua
new file mode 100644
index 000000000..46fa44af3
--- /dev/null
+++ b/tests/apis/namespace/task/test.lua
@@ -0,0 +1,5 @@
+function main()
+ os.exec("xmake task0")
+ os.exec("xmake ns1::task1")
+ os.exec("xmake ns1::ns2::task2")
+end
diff --git a/tests/apis/namespace/task/xmake.lua b/tests/apis/namespace/task/xmake.lua
new file mode 100644
index 000000000..1f9d13e4a
--- /dev/null
+++ b/tests/apis/namespace/task/xmake.lua
@@ -0,0 +1,22 @@
+task("task0")
+ set_menu {options = {}}
+ on_run(function ()
+ print("task0")
+ end)
+
+namespace("ns1", function ()
+ task("task1")
+ set_menu {options = {}}
+ on_run(function ()
+ print("NS1_TASK1")
+ end)
+
+ namespace("ns2", function()
+ task("task2")
+ set_menu {options = {}}
+ on_run(function ()
+ print("NS2_TASK2")
+ end)
+ end)
+end)
+
diff --git a/xmake/core/base/task.lua b/xmake/core/base/task.lua
index deea228aa..1b1093e68 100644
--- a/xmake/core/base/task.lua
+++ b/xmake/core/base/task.lua
@@ -373,7 +373,12 @@ end
-- new a task instance
function task.new(name, info)
local instance = table.inherit(task)
- instance._NAME = name
+ local parts = name:split("::", {plain = true})
+ instance._NAME = parts[#parts]
+ table.remove(parts)
+ if #parts > 0 then
+ instance._NAMESPACE = table.concat(parts, "::")
+ end
instance._INFO = info
return instance
end
@@ -475,13 +480,24 @@ function task:name()
return self._NAME
end
+-- get the namespace
+function _instance:namespace()
+ return self._NAMESPACE
+end
+
+-- get the full name
+function _instance:fullname()
+ local namespace = self:namespace()
+ return namespace and namespace .. "::" .. self:name() or self:name()
+end
+
-- run given task
function task:run(...)
-- check
local on_run = self:get("run")
if not on_run then
- return false, string.format("task(\"%s\"): no run script, please call on_run() first!", self:name())
+ return false, string.format("task(\"%s\"): no run script, please call on_run() first!", self:fullname())
end
-- save the current directory