summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorruki <[email protected]>2017-01-10 09:30:02 +0800
committerruki <[email protected]>2017-01-10 20:29:16 +0800
commit022b1a411cdb692b3690d0f3f176b2420b8272aa (patch)
treebad5d57973ddb366a341c65164ebe04765101a92
parentf1d83ac2119e609b872f4481e8991964d326031f (diff)
add set_module
-rw-r--r--xmake/core/base/interpreter.lua81
-rw-r--r--xmake/core/platform/environment.lua27
-rw-r--r--xmake/core/platform/platform.lua67
-rw-r--r--xmake/core/sandbox/sandbox.lua19
4 files changed, 89 insertions, 105 deletions
diff --git a/xmake/core/base/interpreter.lua b/xmake/core/base/interpreter.lua
index a284732e8..1220aa5e7 100644
--- a/xmake/core/base/interpreter.lua
+++ b/xmake/core/base/interpreter.lua
@@ -922,6 +922,51 @@ function interpreter:api_register_add_array(scope_kind, ...)
self:_api_register_xxx_values(scope_kind, "add", implementation, ...)
end
+-- register api for set_module
+function interpreter:api_register_set_module(scope_kind, ...)
+
+ -- check
+ assert(self)
+
+ -- define implementation
+ local implementation = function (self, scope, name, modulename)
+
+ -- is module name?
+ if type(modulename) ~= "string" then
+ os.raise("set_%s(): invalid module name!", name)
+ end
+
+ -- import module as script
+ local script, errors = loadstring(string.format("import(\"%s\", {inherit = true})", modulename))
+ if not script then
+ os.raise("set_%s(): %s", name, errors)
+ end
+
+ -- make sandbox instance with the given script
+ local instance, errors = sandbox.new(script, self:filter(), self:scriptdir())
+ if not instance then
+ os.raise("set_%s(): %s", name, errors)
+ end
+
+ -- import the module
+ local module, errors = instance:import()
+ if not module then
+ os.raise("set_%s(): %s", name, errors)
+ end
+
+ -- init the module
+ if module.init then
+ module.init()
+ end
+
+ -- save module
+ scope[name] = module
+ end
+
+ -- register implementation
+ self:_api_register_xxx_values(scope_kind, "set", implementation, ...)
+end
+
-- register api for on_script
function interpreter:api_register_on_script(scope_kind, ...)
@@ -931,6 +976,18 @@ function interpreter:api_register_on_script(scope_kind, ...)
-- define implementation
local implementation = function (self, scope, name, script)
+ -- this script is module name? import it first
+ if type(script) == "string" then
+
+ -- import module as script
+ local modulename = script
+ script = function (...)
+
+ -- import it
+ return import(modulename).main(...)
+ end
+ end
+
-- make sandbox instance with the given script
local instance, errors = sandbox.new(script, self:filter(), self:scriptdir())
if not instance then
@@ -954,6 +1011,18 @@ function interpreter:api_register_before_script(scope_kind, ...)
-- define implementation
local implementation = function (self, scope, name, script)
+ -- this script is module name? import it first
+ if type(script) == "string" then
+
+ -- import module as script
+ local modulename = script
+ script = function (...)
+
+ -- import it
+ return import(modulename).main(...)
+ end
+ end
+
-- make sandbox instance with the given script
local instance, errors = sandbox.new(script, self:filter(), self:scriptdir())
if not instance then
@@ -977,6 +1046,18 @@ function interpreter:api_register_after_script(scope_kind, ...)
-- define implementation
local implementation = function (self, scope, name, script)
+ -- this script is module name? import it first
+ if type(script) == "string" then
+
+ -- import module as script
+ local modulename = script
+ script = function (...)
+
+ -- import it
+ return import(modulename).main(...)
+ end
+ end
+
-- make sandbox instance with the given script
local instance, errors = sandbox.new(script, self:filter(), self:scriptdir())
if not instance then
diff --git a/xmake/core/platform/environment.lua b/xmake/core/platform/environment.lua
index 211cb6386..237a3b6eb 100644
--- a/xmake/core/platform/environment.lua
+++ b/xmake/core/platform/environment.lua
@@ -29,29 +29,11 @@ local environment = environment or {}
local platform = require("platform/platform")
local sandbox = require("sandbox/sandbox")
--- load the given environment from the given platform
-function environment.load(plat)
-
- -- load platform
- local instance, errors = platform.load(plat)
- if not instance then
- return nil, errors
- end
-
- -- get environment
- return instance:environment()
-end
-
-- enter the environment for the current platform
function environment.enter(name)
- -- load the environment module
- local module, errors = environment.load()
- if not module and errors then
- return false, errors
- end
-
-- enter it
+ local module = platform.get("environment")
if module then
local ok, errors = sandbox.load(module.enter, name)
if not ok then
@@ -66,13 +48,8 @@ end
-- leave the environment for the current platform
function environment.leave(name)
- -- load the environment module
- local module, errors = environment.load()
- if not module and errors then
- return false, errors
- end
-
-- leave it
+ local module = platform.get("environment")
if module then
local ok, errors = sandbox.load(module.leave, name)
if not ok then
diff --git a/xmake/core/platform/platform.lua b/xmake/core/platform/platform.lua
index 941b86824..c090744cc 100644
--- a/xmake/core/platform/platform.lua
+++ b/xmake/core/platform/platform.lua
@@ -36,60 +36,6 @@ local sandbox = require("sandbox/sandbox")
local config = require("project/config")
local global = require("project/global")
--- load the platform module
-function _instance:_load(modulename)
-
- -- return it directly if cached
- local cachename = "_" .. modulename:upper()
- if self[cachename] then
- return self[cachename]
- end
-
- -- no this module?
- if not self._INFO[modulename] then
- return nil
- end
-
- -- get the script path
- local scriptpath = path.join(self._ROOTDIR, self._INFO[modulename] .. ".lua")
-
- -- not exists?
- if not scriptpath or not os.isfile(scriptpath) then
- return nil, string.format("the %s of %s not found!", modulename, self._NAME)
- end
-
- -- load script
- local script, errors = loadfile(scriptpath)
- if script then
-
- -- make sandbox instance with the given script
- local instance, errors = sandbox.new(script, nil, self._ROOTDIR)
- if not instance then
- return nil, errors
- end
-
- -- import the module
- local module, errors = instance:import()
- if not module then
- return nil, errors
- end
-
- -- init the module
- if module.init then
- module.init()
- end
-
- -- save it to the cache
- self[cachename] = module
-
- -- ok?
- return module
- end
-
- -- failed
- return nil, errors
-end
-
-- new an instance
function _instance.new(name, info, rootdir)
@@ -169,13 +115,6 @@ function _instance:tooldirs()
return self._INFO.tooldirs
end
--- get the environment
-function _instance:environment()
-
- -- load environment
- return self:_load("environment")
-end
-
-- the directories of platform
function platform._directories()
@@ -209,7 +148,6 @@ function platform._interpreter()
, "platform.set_archs"
, "platform.set_menu"
, "platform.set_tooldirs"
- , "platform.set_environment"
}
, script =
{
@@ -219,6 +157,11 @@ function platform._interpreter()
, "platform.on_install"
, "platform.on_uninstall"
}
+ , module =
+ {
+ -- platform.set_xxx
+ "platform.set_environment"
+ }
}
-- save interpreter
diff --git a/xmake/core/sandbox/sandbox.lua b/xmake/core/sandbox/sandbox.lua
index bdf4c614a..fe5648cbd 100644
--- a/xmake/core/sandbox/sandbox.lua
+++ b/xmake/core/sandbox/sandbox.lua
@@ -170,25 +170,8 @@ function sandbox.new(script, filter, rootdir)
-- save root directory
self._PRIVATE._ROOTDIR = rootdir
- -- this script is module name? import it first
- if type(script) == "string" then
-
- -- import module as script
- local modulename = script
- script = function (...)
-
- -- import it
- return import(modulename).main(...)
- end
- end
-
- -- no script?
- if script == nil then
- return nil, "no script!"
- end
-
-- invalid script?
- if script ~= nil and type(script) ~= "function" then
+ if type(script) ~= "function" then
return nil, "invalid script!"
end