diff options
| author | ruki <[email protected]> | 2015-05-19 11:42:14 +0800 |
|---|---|---|
| committer | ruki <[email protected]> | 2015-05-19 11:42:14 +0800 |
| commit | 050d1882cf578ca81f92666d42bc005b7c0eb914 (patch) | |
| tree | 3da2dadd83c155ab2e07811d703172918e03159f /xmake/scripts/base/os.lua | |
| parent | f1de3034b9afbb88f83501730bf27c31078818ed (diff) | |
find and match files or directories
Diffstat (limited to 'xmake/scripts/base/os.lua')
| -rw-r--r-- | xmake/scripts/base/os.lua | 45 |
1 files changed, 45 insertions, 0 deletions
diff --git a/xmake/scripts/base/os.lua b/xmake/scripts/base/os.lua index 7da3d9dcd..31434f37c 100644 --- a/xmake/scripts/base/os.lua +++ b/xmake/scripts/base/os.lua @@ -23,6 +23,10 @@ -- define module: os local os = os or {} +-- load modules +local path = require("base/path") +local utils = require("base/utils") + -- copy file or directory function os.cp(src, dst) @@ -139,5 +143,46 @@ function os.cd(path) return true end +-- match files or directories +-- +-- @param pattern the search pattern +-- uses "*" to match any part of a file or directory name, +-- uses "**" to recurse into subdirectories. +-- +-- @param findir true: find directory, false: find file +-- @return the result array and count +-- +-- @code +-- local dirs, count = os.match("./src/*", true) +-- local files, count = os.match("./src/**.c") +-- @endcode +-- +function os.match(pattern, findir) + + -- translate path and remove some repeat separators + pattern = path.translate(pattern) + + -- get the root directory + local rootdir = pattern + local starpos = pattern:find("%*") + if starpos then + rootdir = rootdir:sub(1, starpos - 1) + end + rootdir = path.directory(rootdir) + + -- is recurse? + local recurse = pattern:find("**", nil, true) + + -- convert pattern to a lua pattern + pattern = pattern:gsub("([%+%.%-%^%$%(%)%%])", "%%%1") + pattern = pattern:gsub("%*%*", "\001") + pattern = pattern:gsub("%*", "\002") + pattern = pattern:gsub("\001", ".*") + pattern = pattern:gsub("\002", "[^/]*") + + -- find it + return os.find(rootdir, pattern, recurse, findir) +end + -- return module: os return os |
