summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorruki <[email protected]>2017-09-27 23:14:42 +0800
committerruki <[email protected]>2017-09-27 13:28:16 +0800
commitca581595f689b8ec3de720560ab3946745889936 (patch)
tree1db701e711fccff6e4b52e58d0ed98b6fcb2b15b
parent38a81629aa72364c097e8ad6c80257165411e18a (diff)
improve package filter
-rw-r--r--xmake/actions/require/action/build.lua18
-rw-r--r--xmake/actions/require/action/download.lua3
-rw-r--r--xmake/actions/require/action/filter.lua135
-rw-r--r--xmake/actions/require/action/install.lua3
-rw-r--r--xmake/actions/require/action/test.lua3
-rw-r--r--xmake/core/base/filter.lua12
-rw-r--r--xmake/core/package/package.lua26
-rw-r--r--xmake/core/sandbox/modules/import/core/base/filter.lua38
-rw-r--r--xmake/core/sandbox/modules/import/core/sandbox/sandbox.lua13
-rw-r--r--xmake/core/sandbox/sandbox.lua21
-rw-r--r--xmake/modules/net/http/download.lua1
11 files changed, 229 insertions, 44 deletions
diff --git a/xmake/actions/require/action/build.lua b/xmake/actions/require/action/build.lua
index 3b98ac91e..78501b7dc 100644
--- a/xmake/actions/require/action/build.lua
+++ b/xmake/actions/require/action/build.lua
@@ -26,6 +26,7 @@
import("core.base.option")
import("core.project.config")
import("core.sandbox.sandbox")
+import("filter")
-- build for xmake file
function _build_for_xmakefile(package, buildfile)
@@ -158,21 +159,6 @@ function _on_build_package(package)
raise("attempt to build package %s failed!", package:fullname())
end
--- run script
-function _run_script(script, package)
-
- -- TODO
- -- register filter handler before building
--- sandbox.filter_register(script, "package.build", function (var)
--- end)
-
- -- run it
- script(package)
-
- -- cancel filter handler before building
--- sandbox.filter_register(script, "package.build", nil)
-end
-
-- build the given package
function main(package)
@@ -191,7 +177,7 @@ function main(package)
for i = 1, 3 do
local script = scripts[i]
if script ~= nil then
- _run_script(script, package)
+ filter.call(script, package)
end
end
diff --git a/xmake/actions/require/action/download.lua b/xmake/actions/require/action/download.lua
index aa28d432e..e94251d9a 100644
--- a/xmake/actions/require/action/download.lua
+++ b/xmake/actions/require/action/download.lua
@@ -24,6 +24,7 @@
-- imports
import("core.base.option")
+import("filter")
import("net.http")
import("devel.git")
import("utils.archive")
@@ -157,7 +158,7 @@ function main(package)
local url_alias = package:url_alias(url)
-- filter url
- url = package:filter():handle(url)
+ url = filter.handle(url, package)
-- download url
local ok = try
diff --git a/xmake/actions/require/action/filter.lua b/xmake/actions/require/action/filter.lua
new file mode 100644
index 000000000..c56387dd1
--- /dev/null
+++ b/xmake/actions/require/action/filter.lua
@@ -0,0 +1,135 @@
+--!A cross-platform build utility based on Lua
+--
+-- Licensed to the Apache Software Foundation (ASF) under one
+-- or more contributor license agreements. See the NOTICE file
+-- distributed with this work for additional information
+-- regarding copyright ownership. The ASF licenses this file
+-- to you 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 - 2017, TBOOX Open Source Group.
+--
+-- @author ruki
+-- @file filter.lua
+--
+
+-- imports
+import("core.base.filter")
+import("core.base.option")
+import("core.base.global")
+import("core.project.config")
+import("core.project.project")
+import("core.sandbox.sandbox")
+
+-- get filter
+function _filter()
+
+ -- init filter
+ if _g.filter == nil then
+ _g.filter = filter.new()
+ _g.filter:register("common", function (variable)
+
+ -- attempt to get it directly from the configure
+ local result = config.get(variable)
+ if result == nil then
+
+ -- init maps
+ _g.common_maps = _g.common_maps or
+ {
+ host = os.host()
+ , tmpdir = function () return os.tmpdir() end
+ , curdir = function () return os.curdir() end
+ , scriptdir = function () return os.scriptdir() end
+ , globaldir = global.directory()
+ , configdir = config.directory()
+ , projectdir = project.directory()
+ , programdir = os.programdir()
+ }
+
+ -- map it
+ result = _g.common_maps[variable]
+ end
+
+ -- is script? call it
+ if type(result) == "function" then
+ result = result()
+ end
+
+ -- ok?
+ return result
+ end)
+ end
+
+ -- ok
+ return _g.filter
+end
+
+-- the package handler
+function _handler(package)
+
+ -- @note cannot cache it, because the package instance will be changed
+ return function (variable)
+
+ -- init maps
+ local maps =
+ {
+ version = function () return package:version_str() end
+ , buildir = function () return package:buildir() end
+ }
+
+ -- get value
+ local result = maps[variable]
+ if type(result) == "function" then
+ result = result()
+ end
+
+ -- ok?
+ return result
+ end
+end
+
+-- attach filter to the given script and call it
+function call(script, package)
+
+ -- get sandbox filter and handlers of the given script
+ local sandbox_filter = sandbox.filter(script)
+ local sandbox_handlers = sandbox_filter:handlers()
+
+ -- switch to the handlers of the current filter
+ sandbox_filter:set_handlers(_filter():handlers())
+
+ -- register package handler
+ sandbox_filter:register("package", _handler(package))
+
+ -- call it
+ script(package)
+
+ -- restore handlers
+ sandbox_filter:set_handlers(sandbox_handlers)
+end
+
+-- handle the string value of package
+function handle(strval, package)
+
+ -- register filter handler
+ _filter():register("package", _handler(package))
+
+ -- handle string value
+ strval = _filter():handle(strval)
+
+ -- register filter handler
+ _filter():register("package", nil)
+
+ -- ok
+ return strval
+end
+
diff --git a/xmake/actions/require/action/install.lua b/xmake/actions/require/action/install.lua
index 583276ffe..6816cbd95 100644
--- a/xmake/actions/require/action/install.lua
+++ b/xmake/actions/require/action/install.lua
@@ -27,6 +27,7 @@ import("core.base.option")
import("core.project.target")
import("build")
import("test")
+import("filter")
-- install for xmake file
function _install_for_xmakefile(package)
@@ -241,7 +242,7 @@ function main(package)
for i = 1, 3 do
local script = scripts[i]
if script ~= nil then
- script(package)
+ filter.call(script, package)
end
end
diff --git a/xmake/actions/require/action/test.lua b/xmake/actions/require/action/test.lua
index cc5337370..7879226ac 100644
--- a/xmake/actions/require/action/test.lua
+++ b/xmake/actions/require/action/test.lua
@@ -24,6 +24,7 @@
-- imports
import("core.base.option")
+import("filter")
-- test the given package
function main(package)
@@ -43,7 +44,7 @@ function main(package)
for i = 1, 3 do
local script = scripts[i]
if script ~= nil then
- script(package)
+ filter.call(script, package)
end
end
diff --git a/xmake/core/base/filter.lua b/xmake/core/base/filter.lua
index 878b7dfe7..3ab7cc2b4 100644
--- a/xmake/core/base/filter.lua
+++ b/xmake/core/base/filter.lua
@@ -94,10 +94,18 @@ function filter.reg(path)
return (winreg.query(path))
end
+-- set handlers
+function filter:set_handlers(handlers)
+ self._HANDLERS = handlers
+end
+
+-- get handlers
+function filter:handlers()
+ return self._HANDLERS
+end
+
-- register handler
function filter:register(name, handler)
-
- -- set handler
self._HANDLERS[name] = handler
end
diff --git a/xmake/core/package/package.lua b/xmake/core/package/package.lua
index 8a0b1b83b..c74c7e474 100644
--- a/xmake/core/package/package.lua
+++ b/xmake/core/package/package.lua
@@ -32,7 +32,6 @@ local io = require("base/io")
local path = require("base/path")
local utils = require("base/utils")
local table = require("base/table")
-local filter = require("base/filter")
local global = require("base/global")
local interpreter = require("base/interpreter")
local sandbox = require("sandbox/sandbox")
@@ -40,6 +39,7 @@ local config = require("project/config")
local project = require("project/project")
local platform = require("platform/platform")
local sandbox = require("sandbox/sandbox")
+local sandbox_os = require("sandbox/modules/os")
local sandbox_module = require("sandbox/modules/import/core/sandbox/module")
-- new an instance
@@ -57,20 +57,6 @@ function _instance.new(name, info, rootdir)
instance._VENDOR = nameinfo[1]
instance._INFO = info
instance._ROOTDIR = rootdir
- instance._FILTER = filter.new()
-
- -- register filter handler
- instance._FILTER:register("package", function (variable)
-
- -- init maps
- local maps =
- {
- version = instance:version_str()
- }
-
- -- map it
- return maps[variable]
- end)
-- ok
return instance
@@ -112,11 +98,6 @@ function _instance:vendor()
return self._VENDOR
end
--- get the package filter
-function _instance:filter()
- return self._FILTER
-end
-
-- get urls
function _instance:urls()
return self._URLS or table.wrap(self:get("urls"))
@@ -186,6 +167,11 @@ function _instance:kind()
return self:get("kind")
end
+-- get the build directory of this package
+function _instance:buildir()
+ return path.join(self:cachedir(), "build")
+end
+
-- get the cached directory of this package
function _instance:cachedir()
return path.join(package.cachedir(), self:fullname(), self:version_str())
diff --git a/xmake/core/sandbox/modules/import/core/base/filter.lua b/xmake/core/sandbox/modules/import/core/base/filter.lua
new file mode 100644
index 000000000..4a8f1462d
--- /dev/null
+++ b/xmake/core/sandbox/modules/import/core/base/filter.lua
@@ -0,0 +1,38 @@
+--!A cross-platform build utility based on Lua
+--
+-- Licensed to the Apache Software Foundation (ASF) under one
+-- or more contributor license agreements. See the NOTICE file
+-- distributed with this work for additional information
+-- regarding copyright ownership. The ASF licenses this file
+-- to you 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 - 2017, TBOOX Open Source Group.
+--
+-- @author ruki
+-- @file filter.lua
+--
+
+-- define module
+local sandbox_core_base_filter = sandbox_core_base_filter or {}
+
+-- load modules
+local filter = require("base/filter")
+local raise = require("sandbox/modules/raise")
+
+-- new filter instance
+function sandbox_core_base_filter.new()
+ return filter.new()
+end
+
+-- return module
+return sandbox_core_base_filter
diff --git a/xmake/core/sandbox/modules/import/core/sandbox/sandbox.lua b/xmake/core/sandbox/modules/import/core/sandbox/sandbox.lua
index 778e5078c..2a041611b 100644
--- a/xmake/core/sandbox/modules/import/core/sandbox/sandbox.lua
+++ b/xmake/core/sandbox/modules/import/core/sandbox/sandbox.lua
@@ -79,5 +79,18 @@ function sandbox_core_sandbox.interactive()
end
end
+-- get the filter of the current sandbox for the given script
+function sandbox_core_sandbox.filter(script)
+
+ -- get the current sandbox instance
+ local instance = sandbox.instance(script)
+ if not instance then
+ raise("cannot get sandbox instance!")
+ end
+
+ -- get it
+ return instance:filter()
+end
+
-- return module
return sandbox_core_sandbox
diff --git a/xmake/core/sandbox/sandbox.lua b/xmake/core/sandbox/sandbox.lua
index 85562e908..9a64de78d 100644
--- a/xmake/core/sandbox/sandbox.lua
+++ b/xmake/core/sandbox/sandbox.lua
@@ -303,10 +303,27 @@ function sandbox:rootdir()
end
-- get current instance in the sandbox modules
-function sandbox.instance()
+function sandbox.instance(script)
- -- find self instance for the current sandbox
+ -- get the sandbox instance from the given script
local instance = nil
+ if script then
+ local scope = getfenv(script)
+ if scope then
+
+ -- enable to read _SANDBOX
+ rawset(scope, "_SANDBOX_READABLE", true)
+
+ -- attempt to get it
+ instance = scope._SANDBOX
+
+ -- disable to read _SANDBOX
+ rawset(scope, "_SANDBOX_READABLE", nil)
+ end
+ if instance then return instance end
+ end
+
+ -- find self instance for the current sandbox
local level = 2
while level < 32 do
diff --git a/xmake/modules/net/http/download.lua b/xmake/modules/net/http/download.lua
index f319d1123..f672bd46d 100644
--- a/xmake/modules/net/http/download.lua
+++ b/xmake/modules/net/http/download.lua
@@ -46,7 +46,6 @@ function _curl_download(tool, url, outputfile)
table.insert(argv, "-A")
table.insert(argv, user_agent)
end
- print(user_agent)
-- set url
table.insert(argv, url)