diff options
| author | ruki <[email protected]> | 2015-06-20 19:47:54 +0800 |
|---|---|---|
| committer | ruki <[email protected]> | 2015-06-20 19:47:54 +0800 |
| commit | 12e518a819bb848c67452655abdfcce5c0d90bd2 (patch) | |
| tree | bed9d450f1e0f63be5a5f57fcb62672e8bcf4765 /xmake/scripts | |
| parent | 2ee74d41c449192a66a2bc5cc1ca64fbc952c626 (diff) | |
copy subfiles with pattern
Diffstat (limited to 'xmake/scripts')
| -rw-r--r-- | xmake/scripts/action/action.lua | 7 | ||||
| -rw-r--r-- | xmake/scripts/base/main.lua | 7 | ||||
| -rw-r--r-- | xmake/scripts/base/os.lua | 139 |
3 files changed, 85 insertions, 68 deletions
diff --git a/xmake/scripts/action/action.lua b/xmake/scripts/action/action.lua index 5602bf4ab..f3f2a1ba8 100644 --- a/xmake/scripts/action/action.lua +++ b/xmake/scripts/action/action.lua @@ -26,6 +26,7 @@ local action = action or {} -- load modules local os = require("base/os") local path = require("base/path") +local utils = require("base/utils") local global = require("base/global") local config = require("base/config") local project = require("base/project") @@ -45,6 +46,12 @@ function action._load_project() local options = xmake._OPTIONS assert(options) + -- enter the project directory + if not os.cd(xmake._PROJECT_DIR) then + -- error + return string.format("not found project: %s!", xmake._PROJECT_DIR) + end + -- check the project file if not os.isfile(xmake._PROJECT_FILE) then return string.format("not found the project file: %s", xmake._PROJECT_FILE) diff --git a/xmake/scripts/base/main.lua b/xmake/scripts/base/main.lua index 852ee9104..8e3773f82 100644 --- a/xmake/scripts/base/main.lua +++ b/xmake/scripts/base/main.lua @@ -152,13 +152,6 @@ function main._init() end xmake._PROJECT_FILE = projectfile assert(projectfile) - - -- enter the project directory - if not os.cd(xmake._PROJECT_DIR) then - -- error - utils.error("not found project: %s!", xmake._PROJECT_DIR) - return false - end end -- the main function diff --git a/xmake/scripts/base/os.lua b/xmake/scripts/base/os.lua index 75b555651..c20c2cb63 100644 --- a/xmake/scripts/base/os.lua +++ b/xmake/scripts/base/os.lua @@ -28,6 +28,67 @@ local path = require("base/path") local utils = require("base/utils") local string = require("base/string") +-- 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") +-- local file = os.match("./src/test.c") +-- @endcode +-- +function os.match(pattern, findir) + + -- get the excludes + local excludes = pattern:match("|.*$") + if excludes then excludes = excludes:split("|") end + + -- translate excludes + if excludes then + local _excludes = {} + for _, exclude in ipairs(excludes) do + exclude = path.translate(exclude) + exclude = exclude:gsub("([%+%.%-%^%$%(%)%%])", "%%%1") + exclude = exclude:gsub("%*%*", "\001") + exclude = exclude:gsub("%*", "\002") + exclude = exclude:gsub("\001", ".*") + exclude = exclude:gsub("\002", "[^/]*") + table.insert(_excludes, exclude) + end + excludes = _excludes + end + + -- translate path and remove some repeat separators + pattern = path.translate(pattern:gsub("|.*$", "")) + + -- 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, excludes) +end + -- copy file or directory function os.cp(src, dst) @@ -46,6 +107,23 @@ function os.cp(src, dst) if not os.cpdir(src, dst) then return false, string.format("cannot copy directory %s to %s!", src, dst) end + -- cp dir/*? + elseif src:find("%*") then + + -- get the root directory + local starpos = src:find("%*") + + -- match all files + local files = os.match((src:gsub("%*+", "**"))) + if files then + for _, file in ipairs(files) do + local dstfile = string.format("%s/%s", dst, file:sub(starpos)) + if not os.cpfile(file, dstfile) then + return false, string.format("cannot copy file %s to %s!", file, dstfile) + end + end + end + -- not exists? else return false, string.format("cannot copy file %s, not found this file!", src) @@ -144,66 +222,5 @@ 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") --- local file = os.match("./src/test.c") --- @endcode --- -function os.match(pattern, findir) - - -- get the excludes - local excludes = pattern:match("|.*$") - if excludes then excludes = excludes:split("|") end - - -- translate excludes - if excludes then - local _excludes = {} - for _, exclude in ipairs(excludes) do - exclude = path.translate(exclude) - exclude = exclude:gsub("([%+%.%-%^%$%(%)%%])", "%%%1") - exclude = exclude:gsub("%*%*", "\001") - exclude = exclude:gsub("%*", "\002") - exclude = exclude:gsub("\001", ".*") - exclude = exclude:gsub("\002", "[^/]*") - table.insert(_excludes, exclude) - end - excludes = _excludes - end - - -- translate path and remove some repeat separators - pattern = path.translate(pattern:gsub("|.*$", "")) - - -- 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, excludes) -end - -- return module: os return os |
