summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorruki <[email protected]>2022-10-17 23:04:46 +0800
committerruki <[email protected]>2022-10-17 23:04:46 +0800
commit2e160359e659e9760f9c2c2c9a5ea8537dcd56c8 (patch)
tree7c306798c204c623203c8ea0c35365ebeaac1962
parentcd0b3f31b288a0676099218677b03456e266fd9f (diff)
load components
-rw-r--r--xmake/core/package/component.lua30
-rw-r--r--xmake/modules/private/action/require/impl/package.lua5
2 files changed, 34 insertions, 1 deletions
diff --git a/xmake/core/package/component.lua b/xmake/core/package/component.lua
index 6ebaf4a41..780fa06f5 100644
--- a/xmake/core/package/component.lua
+++ b/xmake/core/package/component.lua
@@ -33,6 +33,7 @@ local hashset = require("base/hashset")
local scopeinfo = require("base/scopeinfo")
local interpreter = require("base/interpreter")
local language = require("language/language")
+local sandbox = require("sandbox/sandbox")
-- new an instance
function _instance.new(name, opt)
@@ -88,6 +89,34 @@ function _instance:extraconf_set(name, item, key, value)
return self._INFO:extraconf_set(name, item, key, value)
end
+-- get on_component script
+function _instance:_on_component()
+ local script = self:package():get("component")
+ local result = nil
+ if type(script) == "function" then
+ result = script
+ elseif type(script) == "table" then
+ result = script[self:name()]
+ result = result or script["__generic__"]
+ end
+ return result
+end
+
+-- load this component
+function _instance:_load()
+ local loaded = self._LOADED
+ if not loaded then
+ local script = self:_on_component()
+ if script then
+ local ok, errors = sandbox.load(script, self:package(), self)
+ if not ok then
+ os.raise("load component(%s) failed, %s", self:name(), errors or "unknown errors")
+ end
+ end
+ self._LOADED = true
+ end
+end
+
-- the interpreter
function component._interpreter()
local interp = component._INTERPRETER
@@ -110,7 +139,6 @@ function component.apis()
}
end
-
-- new component
function component.new(name, opt)
return _instance.new(name, opt)
diff --git a/xmake/modules/private/action/require/impl/package.lua b/xmake/modules/private/action/require/impl/package.lua
index 0608a3960..450096c1b 100644
--- a/xmake/modules/private/action/require/impl/package.lua
+++ b/xmake/modules/private/action/require/impl/package.lua
@@ -832,6 +832,11 @@ function _load_package(packagename, requireinfo, opt)
on_load(package)
end
+ -- load all components
+ for _, component in pairs(package:components()) do
+ component:_load()
+ end
+
-- load environments from the manifest to enable the environments of on_install()
package:envs_load()