diff options
| author | ruki <[email protected]> | 2017-06-13 20:51:15 +0800 |
|---|---|---|
| committer | ruki <[email protected]> | 2017-06-13 20:51:15 +0800 |
| commit | 0ee5a8ae20143a27ca051e56712beeddb02e7b4a (patch) | |
| tree | 7b56ea1b629a0d37b7124fb7abaafdeb273ffd8a | |
| parent | 48529599248c9bdd43b53def4be7113d226060ef (diff) | |
fix filter escape and find_path bug
| -rw-r--r-- | xmake/core/base/filter.lua | 15 | ||||
| -rw-r--r-- | xmake/core/sandbox/modules/import/lib/detect/find_directory.lua | 106 | ||||
| -rw-r--r-- | xmake/core/sandbox/modules/string.lua | 11 | ||||
| -rw-r--r-- | xmake/modules/detect/sdk/find_xcode_dir.lua | 4 |
4 files changed, 127 insertions, 9 deletions
diff --git a/xmake/core/base/filter.lua b/xmake/core/base/filter.lua index f9744bf61..465a55499 100644 --- a/xmake/core/base/filter.lua +++ b/xmake/core/base/filter.lua @@ -31,6 +31,10 @@ local table = require("base/table") local utils = require("base/utils") local string = require("base/string") +-- globals +local escape_table1 = {["$"] = "\001", ["("] = "\002", [")"] = "\003"} +local escape_table2 = {["\001"] = "$", ["\002"] = "(", ["\003"] = ")"} + -- new filter instance function filter.new() @@ -155,10 +159,19 @@ function filter:handle(value) -- check assert(type(value) == "string") + -- escape "%$", "%(", "%)" to "\001", "\002", "\003" + value = value:gsub("%%([%$%(%)])", function (ch) return escape_table1[ch] end) + -- filter the builtin variables return (value:gsub("%$%((.-)%)", function (variable) + + -- escape "%$", "%(", "%)" to "$", "(", ")" + variable = variable:gsub("[\001\002\003]", function (ch) return escape_table2[ch] end) + + -- get variable value return self:get(variable) or "" - end)) + + end):gsub("[\001\002\003]", function (ch) return escape_table2[ch] end)) end -- return module: filter diff --git a/xmake/core/sandbox/modules/import/lib/detect/find_directory.lua b/xmake/core/sandbox/modules/import/lib/detect/find_directory.lua new file mode 100644 index 000000000..e4c6c00d4 --- /dev/null +++ b/xmake/core/sandbox/modules/import/lib/detect/find_directory.lua @@ -0,0 +1,106 @@ +--!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 find_directory.lua +-- + +-- define module +local sandbox_lib_detect_find_directory = sandbox_lib_detect_find_directory or {} + +-- load modules +local os = require("base/os") +local path = require("base/path") +local utils = require("base/utils") +local table = require("base/table") +local raise = require("sandbox/modules/raise") +local vformat = require("sandbox/modules/vformat") + +-- find directory +-- +-- @param name the directory name +-- @param pathes the search pathes (.e.g dirs, pathes, winreg pathes) +-- @param opt the options, .e.g {suffixes = {"/aa", "/bb"}} +-- +-- @return the directory path +-- +-- @code +-- +-- local dir = find_directory("bin", { "/usr", "/usr/local"}) +-- local dir = find_directory("xxx/test", { "/usr/include", "/usr/local/include/**"}) +-- local dir = find_directory("xxx/test", { "$(env PATH)", "$(reg HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\XXXX;Name)"}) +-- local dir = find_directory("xxx/test/dir*", { "$(env PATH)", function () return val("HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\XXXX;Name"):match("\"(.-)\"") end}) +-- +-- @endcode +-- +function sandbox_lib_detect_find_directory.main(name, pathes, opt) + + -- init options + opt = opt or {} + + -- init pathes + pathes = table.wrap(pathes) + + -- append suffixes to pathes + local suffixes = table.wrap(opt.suffixes) + if #suffixes > 0 then + local pathes_new = {} + for _, parent in ipairs(pathes) do + for _, suffix in ipairs(suffixes) do + table.insert(pathes_new, path.join(parent, suffix)) + end + end + pathes = pathes_new + end + + -- find file + local result = nil + for _, _path in ipairs(pathes) do + + -- format path for builtin variables + if type(_path) == "function" then + local ok, results = sandbox.load(_path) + if ok then + _path = results or "" + else + raise(results) + end + else + _path = vformat(_path) + end + + -- directory exists? + for _, dir in ipairs(os.dirs(path.join(_path, name))) do + result = dir + break + end + + -- found? + if result then + break + end + end + + -- ok? + return result +end + +-- return module +return sandbox_lib_detect_find_directory diff --git a/xmake/core/sandbox/modules/string.lua b/xmake/core/sandbox/modules/string.lua index 3079c6e23..d698c9e71 100644 --- a/xmake/core/sandbox/modules/string.lua +++ b/xmake/core/sandbox/modules/string.lua @@ -46,12 +46,14 @@ function sandbox_string.vformat(format, ...) local instance = sandbox.instance() assert(instance) - -- ignore %$(...) - format = format:gsub("%%%$", "__$__") - -- format string if exists arguments local result = format if #{...} > 0 then + + -- escape "%$", "%(", "%)" to '$', '(', ')' + format = format:gsub("%%([%$%(%)])", "%%%%%1") + + -- try to format it result = string.format(format, ...) end assert(result) @@ -62,9 +64,6 @@ function sandbox_string.vformat(format, ...) result = filter:handle(result) end - -- escape to $(...) - result = result:gsub("__%$__", "$") - -- ok? return result end diff --git a/xmake/modules/detect/sdk/find_xcode_dir.lua b/xmake/modules/detect/sdk/find_xcode_dir.lua index 65e5b4f31..728ba1245 100644 --- a/xmake/modules/detect/sdk/find_xcode_dir.lua +++ b/xmake/modules/detect/sdk/find_xcode_dir.lua @@ -23,7 +23,7 @@ -- -- imports -import("lib.detect.find_path") +import("lib.detect.find_directory") -- find xcode directory -- @@ -36,5 +36,5 @@ import("lib.detect.find_path") -- @endcode -- function main() - return find_path("Xcode.app", {"/Applications"}) or find_path("Xcode*.app", {"/Applications"}) + return find_directory("Xcode.app", {"/Applications"}) or find_directory("Xcode*.app", {"/Applications"}) end |
