diff options
| author | ruki <[email protected]> | 2018-05-02 23:13:20 +0800 |
|---|---|---|
| committer | ruki <[email protected]> | 2018-05-02 13:20:01 +0800 |
| commit | cb8d8dcf2b7a698e7a6c31f1e58722b505132f32 (patch) | |
| tree | 52c3aed8cbb74447258b76b53258a4ed94f89e5b | |
| parent | 0bafe409d0687ebd429037907f15617fe4b42d53 (diff) | |
improve os.match and find_file
| -rw-r--r-- | core/src/xmake/os/find.c | 23 | ||||
| -rw-r--r-- | xmake/core/base/os.lua | 21 | ||||
| -rw-r--r-- | xmake/core/sandbox/modules/import/lib/detect/find_file.lua | 7 | ||||
| -rw-r--r-- | xmake/core/sandbox/modules/import/lib/detect/find_path.lua | 7 | ||||
| -rw-r--r-- | xmake/core/sandbox/modules/os.lua | 16 |
5 files changed, 49 insertions, 25 deletions
diff --git a/core/src/xmake/os/find.c b/core/src/xmake/os/find.c index 3638a8219..79f281f2b 100644 --- a/core/src/xmake/os/find.c +++ b/core/src/xmake/os/find.c @@ -133,8 +133,27 @@ static tb_bool_t xm_os_find_walk(tb_char_t const* path, tb_file_info_t const* in } } - // save this path - if (!excluded) lua_rawseti(lua, -3, (tb_int_t)(++*pcount)); + // does not exclude this path? + if (!excluded) + { + // save it + lua_rawseti(lua, -3, (tb_int_t)(++*pcount)); + + // do callback function + if (lua_isfunction(lua, 6)) + { + // do callback(path, isdir) + lua_pushvalue(lua, 6); + lua_pushstring(lua, path); + lua_pushboolean(lua, info->type == TB_FILE_TYPE_DIRECTORY); + lua_call(lua, 2, 1); + + // is continue? + tb_bool_t is_continue = lua_toboolean(lua, -1); + lua_pop(lua, 1); + if (!is_continue) return tb_false; + } + } // pop this return value else lua_pop(lua, 1); } diff --git a/xmake/core/base/os.lua b/xmake/core/base/os.lua index fba49b64d..32028b231 100644 --- a/xmake/core/base/os.lua +++ b/xmake/core/base/os.lua @@ -201,10 +201,13 @@ end -- @code -- local dirs, count = os.match("./src/*", true) -- local files, count = os.match("./src/**.c") --- local file = os.match("./src/test.c") +-- local file = os.match("./src/test.c", 'f', function (filepath, isdir) +-- return true -- continue it +-- return false -- break it +-- end) -- @endcode -- -function os.match(pattern, mode) +function os.match(pattern, mode, callback) -- get the excludes local excludes = pattern:match("|.*$") @@ -279,25 +282,25 @@ function os.match(pattern, mode) pattern = pattern:gsub("\002", "[^/]*") -- find it - return os.find(rootdir, pattern, recurse, mode, excludes) + return os.find(rootdir, pattern, recurse, mode, excludes, callback) end -- match directories -- -- @note only return {} without count to simplify code, .e.g unpack(os.dirs("")) -- -function os.dirs(pattern) - return (os.match(pattern, 'd')) +function os.dirs(pattern, callback) + return (os.match(pattern, 'd', callback)) end -- match files -function os.files(pattern) - return (os.match(pattern, 'f')) +function os.files(pattern, callback) + return (os.match(pattern, 'f', callback)) end -- match files and directories -function os.filedirs(pattern) - return (os.match(pattern, 'a')) +function os.filedirs(pattern, callback) + return (os.match(pattern, 'a', callback)) end -- copy files or directories 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 9594b3744..1623eac23 100644 --- a/xmake/core/sandbox/modules/import/lib/detect/find_file.lua +++ b/xmake/core/sandbox/modules/import/lib/detect/find_file.lua @@ -44,9 +44,10 @@ function sandbox_lib_detect_find_file._find(filedir, name) filepath = path.join(filedir, name) end - -- file exists? - for _, file in ipairs(os.files(filepath)) do - return file + -- find the first file + local results = os.files(filepath, function (file, isdir) return false end) + if results and #results > 0 then + return results[1] end end 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 0532a1bf8..5228609cd 100644 --- a/xmake/core/sandbox/modules/import/lib/detect/find_path.lua +++ b/xmake/core/sandbox/modules/import/lib/detect/find_path.lua @@ -35,9 +35,10 @@ 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 path.directory(p) + -- find the first path + local results = os.filedirs(path.join(filedir, name), function (file, isdir) return false end) + if results and #results > 0 then + return path.directory(results[1]) end end diff --git a/xmake/core/sandbox/modules/os.lua b/xmake/core/sandbox/modules/os.lua index b75df1d66..16e5878d4 100644 --- a/xmake/core/sandbox/modules/os.lua +++ b/xmake/core/sandbox/modules/os.lua @@ -385,23 +385,23 @@ function sandbox_os.execv(program, argv, opt) end -- match files or directories -function sandbox_os.match(pattern, mode) - return os.match(vformat(pattern), mode) +function sandbox_os.match(pattern, mode, callback) + return os.match(vformat(pattern), mode, callback) end -- match directories -function sandbox_os.dirs(pattern) - return sandbox_os.match(pattern, 'd') +function sandbox_os.dirs(pattern, callback) + return sandbox_os.match(pattern, 'd', callback) end -- match files -function sandbox_os.files(pattern) - return sandbox_os.match(pattern, 'f') +function sandbox_os.files(pattern, callback) + return sandbox_os.match(pattern, 'f', callback) end -- match files and directories -function sandbox_os.filedirs(pattern) - return sandbox_os.match(pattern, 'a') +function sandbox_os.filedirs(pattern, callback) + return sandbox_os.match(pattern, 'a', callback) end -- is directory? |
