summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorruki <[email protected]>2017-02-08 20:40:34 +0800
committerruki <[email protected]>2017-02-08 20:40:34 +0800
commitf717d2da6d60566cfe84737950b311b4d7af9275 (patch)
treee2250e917c8e8064a84da8bd5d491c375fd3fc50
parentf32315b12be847b6231b025388d72c6b529313c4 (diff)
improve os.cp
-rw-r--r--xmake/core/base/os.lua115
-rw-r--r--xmake/core/sandbox/modules/os.lua16
-rw-r--r--xmake/plugins/lua/scripts/cp.lua8
3 files changed, 72 insertions, 67 deletions
diff --git a/xmake/core/base/os.lua b/xmake/core/base/os.lua
index 6c447ded6..23be12632 100644
--- a/xmake/core/base/os.lua
+++ b/xmake/core/base/os.lua
@@ -35,8 +35,8 @@ local string = require("base/string")
-- save original tmpdir
os._tmpdir = os._tmpdir or os.tmpdir
--- translate argv for wildcard
-function os._translate_argv(argv)
+-- translate arguments for wildcard
+function os._translate_args(argv)
-- match all arguments
local results = {}
@@ -53,6 +53,46 @@ function os._translate_argv(argv)
return results
end
+-- copy single file or directory
+function os._cp(src, dst)
+
+ -- check
+ assert(src and dst)
+
+ -- is file?
+ if os.isfile(src) then
+
+ -- the destination is directory? append the filename
+ if os.isdir(dst) then
+ dst = path.join(dst, path.filename(src))
+ end
+
+ -- copy file
+ if not os.cpfile(src, dst) then
+ return false, string.format("cannot copy file %s to %s %s", src, dst, os.strerror())
+ end
+ -- is directory?
+ elseif os.isdir(src) then
+
+ -- the destination directory exists? append the filename
+ if os.isdir(dst) then
+ dst = path.join(dst, path.filename(path.translate(src)))
+ end
+
+ -- copy directory
+ if not os.cpdir(src, dst) then
+ return false, string.format("cannot copy directory %s to %s %s", src, dst, os.strerror())
+ end
+
+ -- not exists?
+ else
+ return false, string.format("cannot copy file %s, not found this file %s", src, os.strerror())
+ end
+
+ -- ok
+ return true
+end
+
-- match files or directories
--
-- @param pattern the search pattern
@@ -148,58 +188,29 @@ function os.filedirs(pattern)
return os.match(pattern, 'a')
end
--- copy file or directory
-function os.cp(src, dst)
-
- -- check
- assert(src and dst)
-
- -- is file?
- if os.isfile(src) then
-
- -- the destination is directory? append the filename
- if os.isdir(dst) then
- dst = path.join(dst, path.filename(src))
- end
-
- -- copy file
- if not os.cpfile(src, dst) then
- return false, string.format("cannot copy file %s to %s %s", src, dst, os.strerror())
- end
- -- is directory?
- elseif os.isdir(src) then
-
- -- the destination directory exists? append the filename
- if os.isdir(dst) then
- dst = path.join(dst, path.filename(path.translate(src)))
- end
+-- copy files or directories
+function os.cp(...)
+
+ -- check arguments
+ local args = {...}
+ if #args < 2 then
+ return false, string.format("invalid arguments: %s", table.concat(args, ' '))
+ end
- -- copy directory
- if not os.cpdir(src, dst) then
- return false, string.format("cannot copy directory %s to %s %s", src, dst, os.strerror())
- end
- -- cp dir/*?
- elseif src:find("%*") then
+ -- get source pathes
+ local srcpaths = table.slice(args, 1, #args - 1)
- -- get the root directory
- local starpos = src:find("%*")
+ -- get destinate path
+ local dstpath = args[#args]
- -- 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 %s", file, dstfile, os.strerror())
- end
- end
+ -- copy files or directories
+ for _, srcpath in ipairs(os._translate_args(srcpaths)) do
+ local ok, errors = os._cp(srcpath, dstpath)
+ if not ok then
+ return false, errors
end
-
- -- not exists?
- else
- return false, string.format("cannot copy file %s, not found this file %s", src, os.strerror())
end
-
+
-- ok
return true
end
@@ -330,7 +341,7 @@ function os.runv(shellname, argv)
local log = os.tmpfile()
-- execute it
- local ok = os.execv(shellname, os._translate_argv(argv), log, log)
+ local ok = os.execv(shellname, os._translate_args(argv), log, log)
if ok ~= 0 then
-- make errors
@@ -368,7 +379,7 @@ function os.execv(shellname, argv, outfile, errfile)
-- open command
local ok = -1
- local proc = process.openv(shellname, os._translate_argv(argv), outfile, errfile)
+ local proc = process.openv(shellname, os._translate_args(argv), outfile, errfile)
if proc ~= nil then
-- wait process
@@ -428,7 +439,7 @@ function os.iorunv(shellname, argv)
local errfile = os.tmpfile()
-- run command
- local ok = os.execv(shellname, os._translate_argv(argv), outfile, errfile)
+ local ok = os.execv(shellname, os._translate_args(argv), outfile, errfile)
-- get output and error data
local outdata = io.readall(outfile)
diff --git a/xmake/core/sandbox/modules/os.lua b/xmake/core/sandbox/modules/os.lua
index 26c6b40bd..f45e68d83 100644
--- a/xmake/core/sandbox/modules/os.lua
+++ b/xmake/core/sandbox/modules/os.lua
@@ -39,21 +39,19 @@ sandbox_os.mtime = os.mtime
sandbox_os.mclock = os.mclock
-- copy file or directory
-function sandbox_os.cp(src, dst)
+function sandbox_os.cp(...)
- -- check
- assert(src and dst)
-
- -- format it first
- src = vformat(src)
- dst = vformat(dst)
+ -- format arguments
+ local args = {}
+ for _, arg in ipairs({...}) do
+ table.insert(args, vformat(arg))
+ end
-- done
- local ok, errors = os.cp(src, dst)
+ local ok, errors = os.cp(unpack(args))
if not ok then
os.raise(errors)
end
-
end
-- move file or directory
diff --git a/xmake/plugins/lua/scripts/cp.lua b/xmake/plugins/lua/scripts/cp.lua
index e3c423389..72153adb2 100644
--- a/xmake/plugins/lua/scripts/cp.lua
+++ b/xmake/plugins/lua/scripts/cp.lua
@@ -25,11 +25,7 @@
-- main
function main(...)
- -- cp it
- local pathes = ...
- if pathes and #pathes == 2 then
- os.cp(pathes[1], pathes[2])
- end
-
+ -- copy it
+ os.cp(...)
end