1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
|
--!A cross-platform build utility based on Lua
--
-- Licensed under the Apache License, Version 2.0 (the "License");
-- you may not use this file except in compliance with the License.
-- You may obtain a copy of the License at
--
-- http://www.apache.org/licenses/LICENSE-2.0
--
-- Unless required by applicable law or agreed to in writing, software
-- distributed under the License is distributed on an "AS IS" BASIS,
-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-- See the License for the specific language governing permissions and
-- limitations under the License.
--
-- Copyright (C) 2015-present, Xmake Open Source Community.
--
-- @author ruki
-- @file component.lua
--
-- define module
local component = component or {}
local _instance = _instance or {}
-- load modules
local os = require("base/os")
local io = require("base/io")
local path = require("base/path")
local utils = require("base/utils")
local table = require("base/table")
local option = require("base/option")
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)
opt = opt or {}
local instance = table.inherit(_instance)
instance._NAME = name
instance._INFO = scopeinfo.new("component", {}, {interpreter = component._interpreter()})
instance._PACKAGE = opt.package
return instance
end
-- get the component name
function _instance:name()
return self._NAME
end
-- get the type: component
function _instance:type()
return "component"
end
-- get the it's package
function _instance:package()
return self._PACKAGE
end
-- get the component configuration
function _instance:get(name)
return self._INFO:get(name)
end
-- set the value to the component info
function _instance:set(name, ...)
self._INFO:apival_set(name, ...)
end
-- add the value to the component info
function _instance:add(name, ...)
self._INFO:apival_add(name, ...)
end
-- get the extra configuration
function _instance:extraconf(name, item, key)
local conf = self._INFO:extraconf(name, item, key)
if conf == nil and self:base() then
conf = self:base():extraconf(name, item, key)
end
return conf
end
-- set the extra configuration
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
if not interp then
interp = interpreter.new()
interp:api_define(component.apis())
interp:api_define(language.apis())
component._INTERPRETER = interp
end
return interp
end
-- get component apis
function component.apis()
return {
values = {
-- component.add_xxx
"component.add_extsources"
}
}
end
-- new component
function component.new(name, opt)
return _instance.new(name, opt)
end
-- return module
return component
|