summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorruki <[email protected]>2017-06-01 10:15:52 +0800
committerruki <[email protected]>2017-06-01 10:15:52 +0800
commitf5fd7001a09b782c3aa2263738a07c38d7ddc351 (patch)
treec5030721bad058d65fd5c5a8a86b47344a45acac
parent1a622c46441db211da1cb381c485a031a5c2a19d (diff)
improve filter and add get api
-rw-r--r--xmake/core/base/filter.lua99
-rw-r--r--xmake/core/sandbox/modules/get.lua52
2 files changed, 122 insertions, 29 deletions
diff --git a/xmake/core/base/filter.lua b/xmake/core/base/filter.lua
index bd998cfb4..b84c5f8e6 100644
--- a/xmake/core/base/filter.lua
+++ b/xmake/core/base/filter.lua
@@ -70,7 +70,24 @@ function filter.shell(cmd)
end
-- return the shell result
- return outdata or ""
+ return outdata
+end
+
+-- filter the environment variables
+function filter.env(name)
+ return os.getenv(name)
+end
+
+-- filter the winreg path
+function filter.reg(path)
+
+ -- must be windows
+ if os.host() ~= "windows" then
+ return
+ end
+
+ -- query registry value
+ return (winreg.query(regpath))
end
-- register handler
@@ -80,11 +97,62 @@ function filter:register(name, handler)
self._HANDLERS[name] = handler
end
+-- get variable value
+function filter:get(variable, handler)
+
+ -- check
+ assert(variable)
+
+ -- is shell?
+ if variable:startswith("shell ") then
+ return filter.shell(variable:sub(7))
+ -- is environment variable?
+ elseif variable:startswith("env ") then
+ return filter.env(variable:sub(5))
+ elseif variable:startswith("reg ") then
+ return filter:reg(variable:sub(5))
+ end
+
+ -- parse variable:mode
+ local varmode = variable:split(':')
+ local mode = varmode[2]
+ variable = varmode[1]
+
+ -- handler it
+ local result = nil
+ if handler then
+ result = handler(variable)
+ else
+ for name, handler in pairs(self._HANDLERS) do
+ result = handler(variable)
+ if result then
+ break
+ end
+ end
+ end
+
+ -- TODO need improve
+ -- handle mode
+ if mode then
+ if mode == "upper" then
+ result = result:upper()
+ elseif mode == "lower" then
+ result = result:lower()
+ end
+ end
+
+ -- ok?
+ return result
+end
+
-- filter the builtin variables: "hello $(variable)" for string
--
-- .e.g
--
-- print("$(host)")
+-- print("$(env PATH)")
+-- print("$(shell echo hello xmake!)")
+-- print("$(reg HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\XXXX;Name)")
--
function filter:handle(value)
@@ -97,34 +165,7 @@ function filter:handle(value)
-- filter the builtin variables
value, count = value:gsub("%$%((.-)%)", function (variable)
-
- -- check
- assert(variable)
-
- -- is shell?
- if variable:startswith("shell ") then
- return filter.shell(variable:sub(7, -1))
- end
-
- -- parse variable:mode
- local varmode = variable:split(':')
- local mode = varmode[2]
- variable = varmode[1]
-
- -- handler it
- local result = handler(variable)
-
- -- handle mode
- if mode then
- if mode == "upper" then
- result = result:upper()
- elseif mode == "lower" then
- result = result:lower()
- end
- end
-
- -- ok?
- return result
+ return self:get(variable, handler) or ""
end)
-- end?
diff --git a/xmake/core/sandbox/modules/get.lua b/xmake/core/sandbox/modules/get.lua
new file mode 100644
index 000000000..fe02addae
--- /dev/null
+++ b/xmake/core/sandbox/modules/get.lua
@@ -0,0 +1,52 @@
+--!The Make-like 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 get.lua
+--
+
+-- load modules
+local sandbox = require("sandbox/sandbox")
+
+-- get the variable value of filter
+--
+-- .e.g
+--
+-- get("host")
+-- get("env PATH")
+-- get("shell echo hello xmake!")
+-- get("reg HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\XXXX;Name")
+--
+function get(name)
+
+ -- get the current sandbox instance
+ local instance = sandbox.instance()
+ assert(instance)
+
+ -- get filter from the current sandbox
+ local filter = instance:filter()
+ if filter then
+ return filter:get(name)
+ end
+end
+
+-- return module
+return get
+