diff options
| author | ruki <[email protected]> | 2018-04-22 21:55:01 +0800 |
|---|---|---|
| committer | ruki <[email protected]> | 2018-04-22 21:55:01 +0800 |
| commit | d1de6e6006a786f46016d30db4a8f90037039143 (patch) | |
| tree | 46dcaa8a2e670f52265c2d9abb0a408275b8a99d | |
| parent | 291020d00ec56964b62a89e19935a6c13b4baf26 (diff) | |
improve find_file and find_path
| -rw-r--r-- | xmake/core/sandbox/modules/import/lib/detect/find_file.lua | 73 | ||||
| -rw-r--r-- | xmake/core/sandbox/modules/import/lib/detect/find_path.lua | 55 |
2 files changed, 59 insertions, 69 deletions
diff --git a/xmake/core/sandbox/modules/import/lib/detect/find_file.lua b/xmake/core/sandbox/modules/import/lib/detect/find_file.lua index f382b88a3..9594b3744 100644 --- a/xmake/core/sandbox/modules/import/lib/detect/find_file.lua +++ b/xmake/core/sandbox/modules/import/lib/detect/find_file.lua @@ -33,6 +33,23 @@ local table = require("base/table") local raise = require("sandbox/modules/raise") local vformat = require("sandbox/modules/vformat") +-- find the given file path or directory +function sandbox_lib_detect_find_file._find(filedir, name) + + -- get file path + local filepath = nil + if os.isfile(filedir) then + filepath = filedir + else + filepath = path.join(filedir, name) + end + + -- file exists? + for _, file in ipairs(os.files(filepath)) do + return file + end +end + -- find file -- -- @param name the file name @@ -55,8 +72,8 @@ function sandbox_lib_detect_find_file.main(name, pathes, opt) -- init options opt = opt or {} - -- translate pathes - local pathes_translated = {} + -- find file + local suffixes = table.wrap(opt.suffixes) for _, _path in ipairs(table.wrap(pathes)) do -- format path for builtin variables @@ -70,48 +87,24 @@ function sandbox_lib_detect_find_file.main(name, pathes, opt) else _path = vformat(_path) end - table.insert(pathes_translated, _path) - end - pathes = pathes_translated - - -- 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 - -- get file path - local filepath = nil - if os.isfile(_path) then - filepath = _path + -- find file with suffixes + if #suffixes > 0 then + for _, suffix in ipairs(table.wrap(opt.suffixes)) do + local filedir = path.join(_path, suffix) + local results = sandbox_lib_detect_find_file._find(filedir, name) + if results then + return results + end + end else - filepath = path.join(_path, name) - end - - -- file exists? - for _, file in ipairs(os.files(filepath)) do - result = file - break - end - - -- found? - if result then - break + -- find file in the given path + local results = sandbox_lib_detect_find_file._find(_path, name) + if results then + return results + end end end - - -- ok? - return result end -- return module diff --git a/xmake/core/sandbox/modules/import/lib/detect/find_path.lua b/xmake/core/sandbox/modules/import/lib/detect/find_path.lua index e994cd46e..10f844752 100644 --- a/xmake/core/sandbox/modules/import/lib/detect/find_path.lua +++ b/xmake/core/sandbox/modules/import/lib/detect/find_path.lua @@ -32,6 +32,15 @@ local table = require("base/table") local raise = require("sandbox/modules/raise") local vformat = require("sandbox/modules/vformat") +-- find the given file path or directory +function sandbox_lib_detect_find_path._find(filedir, name) + + -- path exists? + for _, p in ipairs(os.filedirs(path.join(filedir, name))) do + return filedir + end +end + -- find path -- -- @param name the path name @@ -56,24 +65,9 @@ function sandbox_lib_detect_find_path.main(name, pathes, opt) -- init options opt = opt or {} - -- init pathes - pathes = table.wrap(pathes) - - -- append suffixes to pathes + -- find path 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 + for _, _path in ipairs(table.wrap(pathes)) do -- format path for builtin variables if type(_path) == "function" then @@ -87,20 +81,23 @@ function sandbox_lib_detect_find_path.main(name, pathes, opt) _path = vformat(_path) end - -- path exists? - for _, p in ipairs(os.filedirs(path.join(_path, name))) do - result = _path - break - end - - -- found? - if result then - break + -- find file with suffixes + if #suffixes > 0 then + for _, suffix in ipairs(table.wrap(opt.suffixes)) do + local filedir = path.join(_path, suffix) + local results = sandbox_lib_detect_find_path._find(filedir, name) + if results then + return results + end + end + else + -- find file in the given path + local results = sandbox_lib_detect_find_path._find(_path, name) + if results then + return results + end end end - - -- ok? - return result end -- return module |
