From bdc9a4cb46d5dd7d46dfa9a0ab2ec29aa9a2e6a8 Mon Sep 17 00:00:00 2001 From: ruki Date: Tue, 7 Jan 2025 00:59:53 +0800 Subject: add namespace for task --- xmake/core/base/task.lua | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) (limited to 'xmake/core/base/task.lua') 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 -- cgit v1.3.1 From 27b25397a70585921df496503060e3bf04f0f38b Mon Sep 17 00:00:00 2001 From: ruki Date: Tue, 7 Jan 2025 00:13:26 +0800 Subject: Update task.lua --- xmake/core/base/task.lua | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) (limited to 'xmake/core/base/task.lua') diff --git a/xmake/core/base/task.lua b/xmake/core/base/task.lua index 1b1093e68..7258aeb21 100644 --- a/xmake/core/base/task.lua +++ b/xmake/core/base/task.lua @@ -481,12 +481,12 @@ function task:name() end -- get the namespace -function _instance:namespace() +function task:namespace() return self._NAMESPACE end -- get the full name -function _instance:fullname() +function task:fullname() local namespace = self:namespace() return namespace and namespace .. "::" .. self:name() or self:name() end @@ -508,8 +508,6 @@ function task:run(...) -- restore the current directory os.cd(curdir) - - -- ok? return ok, errors end -- cgit v1.3.1 From ed685ecd74c5267781713f3bd00ce4e4e40b27d7 Mon Sep 17 00:00:00 2001 From: ruki Date: Thu, 9 Jan 2025 00:50:52 +0800 Subject: add namespace to sandbox --- xmake/core/base/interpreter.lua | 2 +- xmake/core/base/task.lua | 9 ++------- xmake/core/package/package.lua | 2 +- xmake/core/project/project.lua | 12 ++++++++++-- xmake/core/sandbox/modules/has_config.lua | 8 +++++++- xmake/core/sandbox/sandbox.lua | 25 ++++++++++++------------- 6 files changed, 33 insertions(+), 25 deletions(-) (limited to 'xmake/core/base/task.lua') diff --git a/xmake/core/base/interpreter.lua b/xmake/core/base/interpreter.lua index 72886e0d2..4e5aaf8c7 100644 --- a/xmake/core/base/interpreter.lua +++ b/xmake/core/base/interpreter.lua @@ -647,7 +647,7 @@ function interpreter:_script(script) end -- make sandbox instance with the given script - local instance, errors = sandbox.new(script, self:filter(), self:scriptdir()) + local instance, errors = sandbox.new(script, {filter = self:filter(), rootdir = self:scriptdir(), namespace = self:namespace()}) if not instance then return nil, errors end diff --git a/xmake/core/base/task.lua b/xmake/core/base/task.lua index 7258aeb21..a3fde1913 100644 --- a/xmake/core/base/task.lua +++ b/xmake/core/base/task.lua @@ -221,17 +221,12 @@ end -- bind script with a sandbox instance function task._bind_script(interp, script) - - -- make sandbox instance with the given script - local instance, errors = sandbox.new(script, interp:filter(), interp:rootdir()) + local instance, errors = sandbox.new(script, { + filter = interp:filter(), rootdir = interp:rootdir(), namespace = interp:namespace()}) if not instance then return nil, errors end - - -- check assert(instance:script()) - - -- update option script return instance:script() end diff --git a/xmake/core/package/package.lua b/xmake/core/package/package.lua index bb12282e1..741ca318c 100644 --- a/xmake/core/package/package.lua +++ b/xmake/core/package/package.lua @@ -2945,7 +2945,7 @@ function package.load_from_system(packagename) end -- make sandbox instance with the given script - instance, errors = sandbox.new(on_install, interp:filter()) + instance, errors = sandbox.new(on_install, {filter = interp:filter(), namespace = interp:namespace()}) if not instance then return nil, errors end diff --git a/xmake/core/project/project.lua b/xmake/core/project/project.lua index 61c1361db..ee2b55a63 100644 --- a/xmake/core/project/project.lua +++ b/xmake/core/project/project.lua @@ -979,8 +979,16 @@ function project.ordertargets() end -- get the given option -function project.option(name) - return project.options()[name] +function project.option(name, opt) + opt = opt or {} + local options = project.options() + if options then + local o = options[name] + if not o and opt.namespace then + o = options[opt.namespace .. "::" .. name] + end + return o + end end -- get options diff --git a/xmake/core/sandbox/modules/has_config.lua b/xmake/core/sandbox/modules/has_config.lua index 1dc36278a..2cf5a86d0 100644 --- a/xmake/core/sandbox/modules/has_config.lua +++ b/xmake/core/sandbox/modules/has_config.lua @@ -20,8 +20,14 @@ -- return module local config = require("project/config") +local sandbox = require("sandbox/sandbox") return function (...) - return config.has(table.pack(...)) + local namespace + local instance = sandbox.instance() + if instance then + namespace = instance:namespace() + end + return config.has(table.pack(...), {namespace = namespace}) end diff --git a/xmake/core/sandbox/sandbox.lua b/xmake/core/sandbox/sandbox.lua index 9bad3eb9f..2d624da98 100644 --- a/xmake/core/sandbox/sandbox.lua +++ b/xmake/core/sandbox/sandbox.lua @@ -107,21 +107,20 @@ function sandbox._new() -- bind instance to the public script envirnoment instance:bind(instance._PUBLIC) - - -- ok? return instance end -- new a sandbox instance with the given script -function sandbox.new(script, filter, rootdir) - assert(script) +function sandbox.new(script, opt) + opt = opt or {} -- new instance local self = sandbox._new() assert(self and self._PUBLIC and self._PRIVATE) - self._PRIVATE._FILTER = filter - self._PRIVATE._ROOTDIR = rootdir + self._PRIVATE._FILTER = opt.filter + self._PRIVATE._ROOTDIR = opt.rootdir + self._PRIVATE._NAMESPACE = opt.namespace -- invalid script? if type(script) ~= "function" then @@ -178,23 +177,17 @@ function sandbox:fork(script, rootdir) -- init a new sandbox instance local instance = sandbox._new() - - -- check assert(instance and instance._PUBLIC and instance._PRIVATE) - -- inherit the filter instance._PRIVATE._FILTER = self:filter() - - -- inherit the root directory instance._PRIVATE._ROOTDIR = rootdir or self:rootdir() + instance._PRIVATE._NAMESPACE = self:namespace() -- bind public scope if script then setfenv(script, instance._PUBLIC) instance._PRIVATE._SCRIPT = script end - - -- ok? return instance end @@ -246,6 +239,12 @@ function sandbox:rootdir() return self._PRIVATE._ROOTDIR end +-- get current namespace +function sandbox:namespace() + assert(self and self._PRIVATE) + return self._PRIVATE._NAMESPACE +end + -- get current instance in the sandbox modules function sandbox.instance(script) -- cgit v1.3.1