diff options
| author | ruki <[email protected]> | 2016-02-02 21:32:48 +0800 |
|---|---|---|
| committer | ruki <[email protected]> | 2016-02-15 19:10:27 +0800 |
| commit | d4373c2a34711642990dfa8d7a51bee831702d46 (patch) | |
| tree | a8409c762494ff7601f03a5289760fcc5222cbe2 | |
| parent | 63093714d76f9895790524a20aa2a0f0fcb8007e (diff) | |
base: add filter module
| -rw-r--r-- | xmake/core/base/filter.lua | 91 | ||||
| -rw-r--r-- | xmake/core/base/interpreter.lua | 64 | ||||
| -rw-r--r-- | xmake/core/base/project.lua | 14 | ||||
| -rw-r--r-- | xmake/core/sandbox/builtin/string.lua | 6 |
4 files changed, 125 insertions, 50 deletions
diff --git a/xmake/core/base/filter.lua b/xmake/core/base/filter.lua new file mode 100644 index 000000000..bbf1fb6cc --- /dev/null +++ b/xmake/core/base/filter.lua @@ -0,0 +1,91 @@ +--!The Automatic Cross-platform Build Tool +-- +-- XMake is free software; you can redistribute it and/or modify +-- it under the terms of the GNU Lesser General Public License as published by +-- the Free Software Foundation; either version 2.1 of the License, or +-- (at your option) any later version. +-- +-- XMake is distributed in the hope that it will be useful, +-- but WITHOUT ANY WARRANTY; without even the implied warranty of +-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +-- GNU Lesser General Public License for more details. +-- +-- You should have received a copy of the GNU Lesser General Public License +-- along with XMake; +-- If not, see <a href="http://www.gnu.org/licenses/"> http://www.gnu.org/licenses/</a> +-- +-- Copyright (C) 2009 - 2015, ruki All rights reserved. +-- +-- @author ruki +-- @file filter.lua +-- + +-- define module: filter +local filter = filter or {} + +-- load modules +local table = require("base/table") +local utils = require("base/utils") +local string = require("base/string") + +-- init filter +function filter.init(handler) + + -- init an filter instance + local self = {_HANDLER = handler} + + -- inherit the interfaces of filter + for k, v in pairs(filter) do + if type(v) == "function" then + self[k] = v + end + end + + -- ok + return self +end + +-- filter the builtin variables: "hello $(variable)" for string +function filter.handle(self, value) + + -- check + assert(type(value) == "string") + + -- return it directly if no handler + local handler = self._HANDLER + if handler == nil then + return value + end + + -- filter the builtin variables + return (value:gsub("%$%((.-)%)", function (variable) + + -- check + assert(variable) + + -- is upper? + local isupper = false + local c = string.char(variable:byte()) + if c >= 'A' and c <= 'Z' then isupper = true end + + -- handler it + local result = handler(variable:lower()) + + -- convert to upper? + if isupper and result and type(result) == "string" then + result = result:upper() + end + + -- invalid builtin variable? + if result == nil then + utils.error("invalid variable: $(%s)", variable) + utils.abort() + end + + -- ok? + return result + end)) +end + +-- return module: filter +return filter diff --git a/xmake/core/base/interpreter.lua b/xmake/core/base/interpreter.lua index 7118055b0..c2d94a2f8 100644 --- a/xmake/core/base/interpreter.lua +++ b/xmake/core/base/interpreter.lua @@ -29,6 +29,7 @@ local path = require("base/path") local table = require("base/table") local utils = require("base/utils") local string = require("base/string") +local filter = require("base/filter") local sandbox = require("base/sandbox") -- traceback @@ -255,44 +256,23 @@ function interpreter._clear(self) end -- filter values -function interpreter._filter(self, values, filter) +function interpreter._filter(self, values) -- check - assert(self and values and filter) + assert(self and values) - -- replace value - local replace = function (value) - - -- replace the builtin variables - return (value:gsub("%$%((.-)%)", function (variable) - - -- check - assert(variable) - - -- is upper? - local isupper = false - local c = string.char(variable:byte()) - if c >= 'A' and c <= 'Z' then isupper = true end - - -- filter it - local result = filter(variable:lower()) - - -- convert to upper? - if isupper and result and type(result) == "string" then - result = result:upper() - end - - -- ok? - return result - end)) + -- return values directly if no filter + local _filter = self._PRIVATE._FILTER + if _filter == nil then + return values end -- done local results = {} if table.is_dictionary(values) then - -- replace keyvalues + -- filter keyvalues for key, value in pairs(values) do - results[replace(key)] = replace(value) + results[_filter:handle(key)] = _filter:handle(value) end else for _, value in ipairs(utils.wrap(values)) do @@ -300,8 +280,8 @@ function interpreter._filter(self, values, filter) -- string? if type(value) == "string" then - -- replace value - value = replace(value) + -- filter value + value = _filter:handle(value) -- array? elseif table.is_array(value) then @@ -309,7 +289,7 @@ function interpreter._filter(self, values, filter) -- replace values local values = {} for _, v in ipairs(value) do - table.insert(values, replace(v)) + table.insert(values, _filter:handle(v)) end value = values end @@ -324,7 +304,7 @@ function interpreter._filter(self, values, filter) end -- handle scope -function interpreter._handle(self, scope, remove_repeat, enable_filter, filter) +function interpreter._handle(self, scope, remove_repeat, enable_filter) -- check assert(scope) @@ -339,8 +319,8 @@ function interpreter._handle(self, scope, remove_repeat, enable_filter, filter) end -- filter values - if filter and enable_filter then - values = self:_filter(values, filter) + if enable_filter then + values = self:_filter(values) end -- unwrap it if be only one @@ -364,9 +344,6 @@ function interpreter._make(self, scope_kind, remove_repeat, enable_filter) local scopes = self._PRIVATE._SCOPES assert(scopes and scopes._ROOT) - -- the filter - local filter = self._PRIVATE._FILTER - -- make results local results = {} if scope_kind then @@ -409,13 +386,13 @@ function interpreter._make(self, scope_kind, remove_repeat, enable_filter) end -- add this scope - results[scope_name] = self:_handle(scope_values, remove_repeat, enable_filter, filter) + results[scope_name] = self:_handle(scope_values, remove_repeat, enable_filter) end else -- only uses the root scope kind - results = self:_handle(scopes._ROOT["__rootkind"], remove_repeat, enable_filter, filter) + results = self:_handle(scopes._ROOT["__rootkind"], remove_repeat, enable_filter) end @@ -507,14 +484,13 @@ function interpreter.mtimes(self) end -- set filter -function interpreter.filter_set(self, filter) +function interpreter.filter_set(self, _filter) -- check assert(self and self._PRIVATE) - assert(filter == nil or type(filter) == "function") - -- set it - self._PRIVATE._FILTER = filter + -- set filter + self._PRIVATE._FILTER = _filter end -- set root directory diff --git a/xmake/core/base/project.lua b/xmake/core/base/project.lua index c1c6c162b..966474cdb 100644 --- a/xmake/core/base/project.lua +++ b/xmake/core/base/project.lua @@ -31,6 +31,7 @@ local path = require("base/path") local utils = require("base/utils") local table = require("base/table") local config = require("base/config") +local filter = require("base/filter") local linker = require("base/linker") local compiler = require("base/compiler") local platform = require("base/platform") @@ -450,22 +451,27 @@ function project._interpreter() interp:api_register("add_pkgs", interpreter.api_builtin_add_subdirs) -- set filter - interp:filter_set(function (variable) + interp:filter_set(filter.init(function (variable) + + -- check + assert(variable) -- attempt to get it directly from the configure local result = config.get(variable) if not result or type(result) ~= "string" then - -- get the other keys + -- $(projectdir) if variable == "projectdir" then result = xmake._PROJECT_DIR + -- $(os) elseif variable == "os" then result = platform.os() + -- $(prefix): pass to makeconf + elseif variable == "prefix" then result = "$(prefix)" end end -- ok? return result - - end) + end)) -- save interpreter project._INTERPRETER = interp diff --git a/xmake/core/sandbox/builtin/string.lua b/xmake/core/sandbox/builtin/string.lua index 142842c5b..e996c24a3 100644 --- a/xmake/core/sandbox/builtin/string.lua +++ b/xmake/core/sandbox/builtin/string.lua @@ -22,6 +22,7 @@ -- load modules local string = require("base/string") +local filter = require("base/filter") -- define module local sandbox_builtin_string = sandbox_builtin_string or {} @@ -29,8 +30,9 @@ local sandbox_builtin_string = sandbox_builtin_string or {} -- format string with the builtin variables function sandbox_builtin_string.vformat(format, ...) - -- done - return string.format(format, ...) + -- TODO + -- format and filter it +-- return filter.done(string.format(format, ...), filter.handler_for_project) end -- inherit the public interfaces of string |
