summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorruki <[email protected]>2020-03-01 12:49:09 +0800
committerruki <[email protected]>2020-03-01 12:49:09 +0800
commitce93139a61debcca7ca3f1878775f1385e78f53a (patch)
tree5a08edb0fce4c6fff0083cc0394dee3f62362b2f
parent6018704f4bfdf1906b6a45f4bb92679cefc57629 (diff)
improve filesystem ops
-rw-r--r--xmake/core/base/os.lua146
-rw-r--r--xmake/core/sandbox/modules/os.lua150
-rw-r--r--xmake/plugins/lua/scripts/cat.lua4
-rw-r--r--xmake/plugins/lua/scripts/echo.lua4
4 files changed, 106 insertions, 198 deletions
diff --git a/xmake/core/base/os.lua b/xmake/core/base/os.lua
index 92545d261..a947cfc3b 100644
--- a/xmake/core/base/os.lua
+++ b/xmake/core/base/os.lua
@@ -199,26 +199,12 @@ function os._is_arch(arch, ...)
end
end
--- translate arguments for wildcard
-function os.argw(argv)
-
- -- match all arguments
- local results = {}
- for _, arg in ipairs(table.wrap(argv)) do
- if arg:find("*", 1, true) then
- local pathes = os.filedirs(arg)
- if #pathes > 0 then
- table.join2(results, pathes)
- else
- table.insert(results, arg)
- end
- else
- table.insert(results, arg)
- end
+-- match wildcard files
+function os._match_wildcard_pathes(v)
+ if v:find("*", 1, true) then
+ return (os.filedirs(v))
end
-
- -- ok?
- return results
+ return v
end
-- make string from arguments list
@@ -380,87 +366,81 @@ function os.filedirs(pattern, callback)
end
-- copy files or directories
-function os.cp(...)
+function os.cp(srcpath, dstpath)
-- check arguments
- local args = {...}
- if #args < 2 then
- return false, string.format("invalid arguments: %s", table.concat(args, ' '))
+ if not srcpath or not dstpath then
+ return false, string.format("invalid arguments!")
end
- -- get source pathes
- local srcpathes = table.slice(args, 1, #args - 1)
-
- -- get destinate path
- local dstpath = args[#args]
-
-- copy files or directories
- for _, srcpath in ipairs(os.argw(srcpathes)) do
- local ok, errors = os._cp(srcpath, dstpath)
- if not ok then
- return false, errors
+ local srcpathes = os._match_wildcard_pathes(srcpath)
+ if type(srcpathes) == "string" then
+ return os._cp(srcpathes, dstpath)
+ else
+ for _, _srcpath in ipairs(srcpathes) do
+ local ok, errors = os._cp(_srcpath, dstpath)
+ if not ok then
+ return false, errors
+ end
end
end
-
- -- ok
return true
end
-- move files or directories
-function os.mv(...)
+function os.mv(srcpath, dstpath)
-- check arguments
- local args = {...}
- if #args < 2 then
- return false, string.format("invalid arguments: %s", table.concat(args, ' '))
+ if not srcpath or not dstpath then
+ return false, string.format("invalid arguments!")
end
- -- get source pathes
- local srcpathes = table.slice(args, 1, #args - 1)
-
- -- get destinate path
- local dstpath = args[#args]
-
- -- move files or directories
- for _, srcpath in ipairs(os.argw(srcpathes)) do
- local ok, errors = os._mv(srcpath, dstpath)
- if not ok then
- return false, errors
+ -- copy files or directories
+ local srcpathes = os._match_wildcard_pathes(srcpath)
+ if type(srcpathes) == "string" then
+ return os._mv(srcpathes, dstpath)
+ else
+ for _, _srcpath in ipairs(srcpathes) do
+ local ok, errors = os._mv(_srcpath, dstpath)
+ if not ok then
+ return false, errors
+ end
end
end
-
- -- ok
return true
end
-- remove files or directories
-function os.rm(...)
+function os.rm(filepath)
-- check arguments
- local args = {...}
- if #args < 1 then
- return false, string.format("invalid arguments: %s", table.concat(args, ' '))
+ if not filepath then
+ return false, string.format("invalid arguments!")
end
- -- remove directories
- for _, filedir in ipairs(os.argw(args)) do
- local ok, errors = os._rm(filedir)
- if not ok then
- return false, errors
+ -- remove file or directories
+ local filepathes = os._match_wildcard_pathes(filepath)
+ if type(filepathes) == "string" then
+ return os._rm(filepathes)
+ else
+ for _, _filepath in ipairs(filepathes) do
+ local ok, errors = os._rm(_filepath)
+ if not ok then
+ return false, errors
+ end
end
end
-
- -- ok
return true
end
-- link file or directory to the new symfile
-function os.ln(filedir, symfile)
+function os.ln(srcpath, dstpath)
if os.host() == "windows" then
return false, string.format("symlink is not supported!")
end
- if not os.link(filedir, symfile) then
- return false, string.format("cannot link %s to %s, error: %s", filedir, symfile, os.strerror())
+ if not os.link(srcpath, dstpath) then
+ return false, string.format("cannot link %s to %s, error: %s", srcpath, dstpath, os.strerror())
end
return true
end
@@ -512,42 +492,38 @@ function os.cd(dir)
end
-- create directories
-function os.mkdir(...)
+function os.mkdir(dir)
-- check arguments
- local args = {...}
- if #args < 1 then
- return false, string.format("invalid arguments: %s", table.concat(args, ' '))
+ if not dir then
+ return false, string.format("invalid arguments!")
end
-- create directories
- for _, dir in ipairs(os.argw(args)) do
- if not os._mkdir(dir) then
- return false, string.format("cannot create directory: %s, error: %s", dir, os.strerror())
+ local dirs = table.wrap(os._match_wildcard_pathes(dir))
+ for _, _dir in ipairs(dirs) do
+ if not os._mkdir(_dir) then
+ return false, string.format("cannot create directory: %s, error: %s", _dir, os.strerror())
end
end
-
- -- ok
return true
end
-- remove directories
-function os.rmdir(...)
+function os.rmdir(dir)
-- check arguments
- local args = {...}
- if #args < 1 then
- return false, string.format("invalid arguments: %s", table.concat(args, ' '))
+ if not dir then
+ return false, string.format("invalid arguments!")
end
- -- create directories
- for _, dir in ipairs(os.argw(args)) do
- if not os._rmdir(dir) then
- return false, string.format("remove directory: %s failed!", dir)
+ -- remove directories
+ local dirs = table.wrap(os._match_wildcard_pathes(dir))
+ for _, _dir in ipairs(dirs) do
+ if not os._rmdir(_dir) then
+ return false, string.format("cannot remove directory: %s, error: %s", _dir, os.strerror())
end
end
-
- -- ok
return true
end
diff --git a/xmake/core/sandbox/modules/os.lua b/xmake/core/sandbox/modules/os.lua
index da79d2270..e19a487d9 100644
--- a/xmake/core/sandbox/modules/os.lua
+++ b/xmake/core/sandbox/modules/os.lua
@@ -45,7 +45,6 @@ sandbox_os.date = os.date
sandbox_os.time = os.time
sandbox_os.args = os.args
sandbox_os.argv = os.argv
-sandbox_os.argw = os.argw
sandbox_os.mtime = os.mtime
sandbox_os.raise = os.raise
sandbox_os.fscase = os.fscase
@@ -78,142 +77,93 @@ sandbox_os.SYSERR_NOT_PERM = os.SYSERR_NOT_PERM
sandbox_os.SYSERR_NOT_FILEDIR = os.SYSERR_NOT_FILEDIR
-- copy file or directory
-function sandbox_os.cp(...)
-
- -- format arguments
- local args = {}
- for _, arg in ipairs({...}) do
- table.insert(args, vformat(arg))
- end
-
- -- done
- local ok, errors = os.cp(unpack(args))
+function sandbox_os.cp(srcpath, dstpath)
+ assert(srcpath and dstpath)
+ local ok, errors = os.cp(vformat(srcpath), vformat(dstpath))
if not ok then
os.raise(errors)
end
end
-- move file or directory
-function sandbox_os.mv(...)
-
- -- format arguments
- local args = {}
- for _, arg in ipairs({...}) do
- table.insert(args, vformat(arg))
- end
-
- -- done
- local ok, errors = os.mv(unpack(args))
+function sandbox_os.mv(srcpath, dstpath)
+ assert(srcpath and dstpath)
+ local ok, errors = os.mv(vformat(srcpath), vformat(dstpath))
if not ok then
os.raise(errors)
end
end
-- remove files or directories
-function sandbox_os.rm(...)
-
- -- format arguments
- local args = {}
- for _, arg in ipairs({...}) do
- table.insert(args, vformat(arg))
- end
-
- -- remove it
- local ok, errors = os.rm(unpack(args))
+function sandbox_os.rm(filepath)
+ assert(filepath)
+ local ok, errors = os.rm(vformat(filepath))
if not ok then
os.raise(errors)
end
end
-- link file or directory to the new symfile
-function sandbox_os.ln(filedir, symfile)
- local ok, errors = os.ln(filedir, symfile)
+function sandbox_os.ln(srcpath, dstpath)
+ assert(srcpath and dstpath)
+ local ok, errors = os.ln(vformat(srcpath), vformat(dstpath))
if not ok then
os.raise(errors)
end
end
-- copy file or directory with the verbose info
-function sandbox_os.vcp(...)
+function sandbox_os.vcp(srcpath, dstpath)
+ assert(srcpath and dstpath)
if option.get("verbose") then
- local srcfile, dstfile = ...
- if srcfile and dstfile then
- utils.cprint("${dim}> copy %s to %s ..", srcfile, dstfile)
- end
+ utils.cprint("${dim}> copy %s to %s ..", srcpath, dstpath)
end
- return sandbox_os.cp(...)
+ return sandbox_os.cp(srcpath, dstpath)
end
-- move file or directory with the verbose info
-function sandbox_os.vmv(...)
+function sandbox_os.vmv(srcpath, dstpath)
+ assert(srcpath and dstpath)
if option.get("verbose") then
- local srcfile, dstfile = ...
- if srcfile and dstfile then
- utils.cprint("${dim}> move %s to %s ..", srcfile, dstfile)
- end
+ utils.cprint("${dim}> move %s to %s ..", srcpath, dstpath)
end
- return sandbox_os.mv(...)
+ return sandbox_os.mv(srcpath, dstpath)
end
-- remove file or directory with the verbose info
-function sandbox_os.vrm(...)
+function sandbox_os.vrm(filepath)
+ assert(filepath)
if option.get("verbose") then
- local file = ...
- if file then
- utils.cprint("${dim}> remove %s ..", file)
- end
+ utils.cprint("${dim}> remove %s", filepath)
end
- return sandbox_os.rm(...)
+ return sandbox_os.rm(filepath)
end
-- link file or directory with the verbose info
-function sandbox_os.vln(...)
+function sandbox_os.vln(srcpath, dstpath)
+ assert(srcpath and dstpath)
if option.get("verbose") then
- local srcfile, dstfile = ...
- if srcfile and dstfile then
- utils.cprint("${dim}> link %s to %s ..", srcfile, dstfile)
- end
+ utils.cprint("${dim}> link %s to %s", srcpath, dstpath)
end
- return sandbox_os.ln(...)
+ return sandbox_os.ln(srcpath, dstpath)
end
-- try to copy file or directory
-function sandbox_os.trycp(...)
-
- -- format arguments
- local args = {}
- for _, arg in ipairs({...}) do
- table.insert(args, vformat(arg))
- end
-
- -- done
- return os.cp(unpack(args))
+function sandbox_os.trycp(srcpath, dstpath)
+ assert(srcpath and dstpath)
+ return os.cp(vformat(srcpath), vformat(dstpath))
end
-- try to move file or directory
-function sandbox_os.trymv(...)
-
- -- format arguments
- local args = {}
- for _, arg in ipairs({...}) do
- table.insert(args, vformat(arg))
- end
-
- -- done
- return os.mv(unpack(args))
+function sandbox_os.trymv(srcpath, dstpath)
+ assert(srcpath and dstpath)
+ return os.mv(vformat(srcpath), vformat(dstpath))
end
-- try to remove files or directories
-function sandbox_os.tryrm(...)
-
- -- format arguments
- local args = {}
- for _, arg in ipairs({...}) do
- table.insert(args, vformat(arg))
- end
-
- -- remove it
- return os.rm(unpack(args))
+function sandbox_os.tryrm(filepath)
+ assert(filepath)
+ return os.rm(vformat(filepath))
end
-- change to directory
@@ -236,32 +186,18 @@ function sandbox_os.cd(dir)
end
-- create directories
-function sandbox_os.mkdir(...)
-
- -- format arguments
- local args = {}
- for _, arg in ipairs({...}) do
- table.insert(args, vformat(arg))
- end
-
- -- done
- local ok, errors = os.mkdir(unpack(args))
+function sandbox_os.mkdir(dir)
+ assert(dir)
+ local ok, errors = os.mkdir(vformat(dir))
if not ok then
os.raise(errors)
end
end
-- remove directories
-function sandbox_os.rmdir(...)
-
- -- format arguments
- local args = {}
- for _, arg in ipairs({...}) do
- table.insert(args, vformat(arg))
- end
-
- -- done
- local ok, errors = os.rmdir(unpack(args))
+function sandbox_os.rmdir(dir)
+ assert(dir)
+ local ok, errors = os.rmdir(vformat(dir))
if not ok then
os.raise(errors)
end
diff --git a/xmake/plugins/lua/scripts/cat.lua b/xmake/plugins/lua/scripts/cat.lua
index 22f41b393..3abfd0c9a 100644
--- a/xmake/plugins/lua/scripts/cat.lua
+++ b/xmake/plugins/lua/scripts/cat.lua
@@ -20,9 +20,7 @@
-- main
function main(...)
-
- -- cat all
- for _, v in ipairs(os.argw{...}) do
+ for _, v in ipairs(table.pack(...)) do
if os.isfile(v) then io.cat(v) end
end
print("")
diff --git a/xmake/plugins/lua/scripts/echo.lua b/xmake/plugins/lua/scripts/echo.lua
index e25aef448..d24da06f8 100644
--- a/xmake/plugins/lua/scripts/echo.lua
+++ b/xmake/plugins/lua/scripts/echo.lua
@@ -20,9 +20,7 @@
-- main
function main(...)
-
- -- echo all
- for _, v in ipairs(os.argw{...}) do
+ for _, v in ipairs(table.pack(...)) do
printf("%s ", v)
end
print("")