summaryrefslogtreecommitdiff
path: root/xmake/modules/private/action/require/impl/utils
diff options
context:
space:
mode:
authorruki <[email protected]>2021-02-12 23:00:24 +0800
committerruki <[email protected]>2021-02-12 23:00:24 +0800
commit5cd7e9ab5bccd3bf469eb5da49f70085a9753696 (patch)
treee2be745258683c23af1aa578d7314a246159dccb /xmake/modules/private/action/require/impl/utils
parentcc7678dfae7d945b34b8abc70f18e26b8045fd9e (diff)
move require action modules
Diffstat (limited to 'xmake/modules/private/action/require/impl/utils')
-rw-r--r--xmake/modules/private/action/require/impl/utils/filter.lua146
-rw-r--r--xmake/modules/private/action/require/impl/utils/get_requires.lua57
-rw-r--r--xmake/modules/private/action/require/impl/utils/url_filename.lua25
3 files changed, 228 insertions, 0 deletions
diff --git a/xmake/modules/private/action/require/impl/utils/filter.lua b/xmake/modules/private/action/require/impl/utils/filter.lua
new file mode 100644
index 000000000..11bdf70de
--- /dev/null
+++ b/xmake/modules/private/action/require/impl/utils/filter.lua
@@ -0,0 +1,146 @@
+--!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, 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()
+ , subhost = os.subhost()
+ , 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, strval)
+
+ -- @note cannot cache it, because the package instance will be changed
+ return function (variable)
+
+ -- init maps
+ local maps =
+ {
+ version = function ()
+ if strval then
+ -- set_urls("https://sqlite.org/2018/sqlite-autoconf-$(version)000.tar.gz",
+ -- {version = function (version) return version:gsub("%.", "") end})
+ local version_filter = package:url_version(strval)
+ if version_filter then
+ local v = version_filter(package:version())
+ if v ~= nil then
+ -- may be semver version object
+ v = tostring(v)
+ end
+ return v
+ end
+ end
+ return package:version_str()
+ 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, strval))
+
+ -- handle string value
+ strval = _filter():handle(strval)
+
+ -- register filter handler
+ _filter():register("package", nil)
+
+ -- ok
+ return strval
+end
+
diff --git a/xmake/modules/private/action/require/impl/utils/get_requires.lua b/xmake/modules/private/action/require/impl/utils/get_requires.lua
new file mode 100644
index 000000000..9a0ad76c1
--- /dev/null
+++ b/xmake/modules/private/action/require/impl/utils/get_requires.lua
@@ -0,0 +1,57 @@
+--!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, TBOOX Open Source Group.
+--
+-- @author ruki
+-- @file get_requires.lua
+--
+
+-- imports
+import("core.base.option")
+import("core.project.project")
+
+-- get requires and extra config
+function main(requires)
+
+ -- init requires
+ local requires_extra = nil
+ if not requires then
+ requires, requires_extra = project.requires_str()
+ end
+ if not requires or #requires == 0 then
+ return
+ end
+
+ -- get extra info
+ local extra = option.get("extra")
+ local extrainfo = nil
+ if extra then
+ local v, err = string.deserialize(extra)
+ if err then
+ raise(err)
+ else
+ extrainfo = v
+ end
+ end
+
+ -- force to use the given requires extra info
+ if extrainfo then
+ requires_extra = requires_extra or {}
+ for _, require_str in ipairs(requires) do
+ requires_extra[require_str] = extrainfo
+ end
+ end
+ return requires, requires_extra
+end
diff --git a/xmake/modules/private/action/require/impl/utils/url_filename.lua b/xmake/modules/private/action/require/impl/utils/url_filename.lua
new file mode 100644
index 000000000..e17e8673b
--- /dev/null
+++ b/xmake/modules/private/action/require/impl/utils/url_filename.lua
@@ -0,0 +1,25 @@
+--!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, TBOOX Open Source Group.
+--
+-- @author ruki
+-- @file url_filename.lua
+--
+
+-- get filename from url
+function main(url)
+ local urlpath = url:split('?', {plain = true})[1]
+ return path.filename(urlpath)
+end