summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorruki <[email protected]>2017-02-08 22:11:42 +0800
committerruki <[email protected]>2017-02-08 22:11:42 +0800
commitf5571dea410131991ae7f836cc6a52d82890d894 (patch)
tree1da5045f7df779ec47016fc8861fac8aae754f77
parentf717d2da6d60566cfe84737950b311b4d7af9275 (diff)
improve os.rm and os.rmdir
-rwxr-xr-xcore/src/xmake/os/mkdir.c7
-rwxr-xr-xxmake/actions/clean/main.lua4
-rw-r--r--xmake/core/base/os.lua201
-rw-r--r--xmake/core/sandbox/modules/os.lua79
-rw-r--r--xmake/plugins/lua/scripts/cat.lua3
-rw-r--r--xmake/plugins/lua/scripts/cp.lua2
-rw-r--r--xmake/plugins/lua/scripts/echo.lua5
-rw-r--r--xmake/plugins/lua/scripts/lipo.lua2
-rw-r--r--xmake/plugins/lua/scripts/mkdir.lua8
-rw-r--r--xmake/plugins/lua/scripts/mv.lua7
-rw-r--r--xmake/plugins/lua/scripts/rm.lua9
-rw-r--r--xmake/plugins/lua/scripts/rmdir.lua12
-rw-r--r--xmake/plugins/lua/scripts/verbose.lua40
-rwxr-xr-xxmake/plugins/lua/xmake.lua4
14 files changed, 198 insertions, 185 deletions
diff --git a/core/src/xmake/os/mkdir.c b/core/src/xmake/os/mkdir.c
index d20218c5d..557833161 100755
--- a/core/src/xmake/os/mkdir.c
+++ b/core/src/xmake/os/mkdir.c
@@ -46,8 +46,11 @@ tb_int_t xm_os_mkdir(lua_State* lua)
tb_char_t const* path = luaL_checkstring(lua, 1);
tb_check_return_val(path, 0);
- // done os.mkdir(path)
- lua_pushboolean(lua, tb_directory_create(path));
+ // os.mkdir(path)
+ tb_file_info_t info = {0};
+ if (!tb_file_info(path, &info) || (info.type != TB_FILE_TYPE_DIRECTORY))
+ lua_pushboolean(lua, tb_directory_create(path));
+ else lua_pushboolean(lua, tb_true);
// ok
return 1;
diff --git a/xmake/actions/clean/main.lua b/xmake/actions/clean/main.lua
index 594280f0a..b7ddd0f84 100755
--- a/xmake/actions/clean/main.lua
+++ b/xmake/actions/clean/main.lua
@@ -40,7 +40,7 @@ function _remove(filedirs)
if os.exists(filedir) then
-- remove it
- os.rm(filedir, true)
+ os.rm(filedir)
-- remove "*.o/obj" files?
elseif filedir:find("%*") then
@@ -49,7 +49,7 @@ function _remove(filedirs)
for _, file in ipairs(os.match(filedir)) do
-- remove it
- os.rm(file, true)
+ os.rm(file)
end
end
end
diff --git a/xmake/core/base/os.lua b/xmake/core/base/os.lua
index 23be12632..9e8e90b35 100644
--- a/xmake/core/base/os.lua
+++ b/xmake/core/base/os.lua
@@ -32,27 +32,11 @@ local table = require("base/table")
local utils = require("base/utils")
local string = require("base/string")
--- save original tmpdir
+-- save original interfaces
+os._mkdir = os._mkdir or os.mkdir
+os._rmdir = os._rmdir or os.rmdir
os._tmpdir = os._tmpdir or os.tmpdir
--- translate arguments for wildcard
-function os._translate_args(argv)
-
- -- match all arguments
- local results = {}
- for _, arg in ipairs(table.wrap(argv)) do
- local pathes = os.match(arg, 'a')
- if #pathes > 0 then
- table.join2(results, pathes)
- else
- table.insert(results, arg)
- end
- end
-
- -- ok?
- return results
-end
-
-- copy single file or directory
function os._cp(src, dst)
@@ -93,6 +77,69 @@ function os._cp(src, dst)
return true
end
+-- move single file or directory
+function os._mv(src, dst)
+
+ -- check
+ assert(src and dst)
+
+ -- exists file or directory?
+ if os.exists(src) then
+ -- move file or directory
+ if not os.rename(src, dst) then
+ return false, string.format("cannot move %s to %s %s", src, dst, os.strerror())
+ end
+ -- not exists?
+ else
+ return false, string.format("cannot move %s to %s, not found this file %s", src, dst, os.strerror())
+ end
+
+ -- ok
+ return true
+end
+
+-- remove single file or directory
+function os._rm(filedir)
+
+ -- check
+ assert(filedir)
+
+ -- is file?
+ if os.isfile(filedir) then
+ -- remove file
+ if not os.rmfile(filedir) then
+ return false, string.format("cannot remove file %s %s", filedir, os.strerror())
+ end
+ -- is directory?
+ elseif os.isdir(filedir) then
+ -- remove directory
+ if not os.rmdir(filedir) then
+ return false, string.format("cannot remove directory %s %s", filedir, os.strerror())
+ end
+ end
+
+ -- ok
+ return true
+end
+
+-- translate arguments for wildcard
+function os.argw(argv)
+
+ -- match all arguments
+ local results = {}
+ for _, arg in ipairs(table.wrap(argv)) do
+ local pathes = os.match(arg, 'a')
+ if #pathes > 0 then
+ table.join2(results, pathes)
+ else
+ table.insert(results, arg)
+ end
+ end
+
+ -- ok?
+ return results
+end
+
-- match files or directories
--
-- @param pattern the search pattern
@@ -198,13 +245,13 @@ function os.cp(...)
end
-- get source pathes
- local srcpaths = table.slice(args, 1, #args - 1)
+ local srcpathes = table.slice(args, 1, #args - 1)
-- get destinate path
local dstpath = args[#args]
-- copy files or directories
- for _, srcpath in ipairs(os._translate_args(srcpaths)) do
+ for _, srcpath in ipairs(os.argw(srcpathes)) do
local ok, errors = os._cp(srcpath, dstpath)
if not ok then
return false, errors
@@ -215,52 +262,46 @@ function os.cp(...)
return true
end
--- move file or directory
-function os.mv(src, dst)
-
- -- check
- assert(src and dst)
+-- move files or directories
+function os.mv(...)
+
+ -- check arguments
+ local args = {...}
+ if #args < 2 then
+ return false, string.format("invalid arguments: %s", table.concat(args, ' '))
+ end
- -- exists file or directory?
- if os.exists(src) then
- -- move file or directory
- if not os.rename(src, dst) then
- return false, string.format("cannot move %s to %s %s", src, dst, os.strerror())
+ -- 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._mv(srcpath, dstpath)
+ if not ok then
+ return false, errors
end
- -- not exists?
- else
- return false, string.format("cannot move %s to %s, not found this file %s", src, dst, os.strerror())
end
-
+
-- ok
return true
end
--- remove file or directory and remove it if the super directory be empty
-function os.rm(file_or_dir, rm_superdir_if_empty)
+-- remove files or directories
+function os.rm(...)
- -- check
- assert(file_or_dir)
-
- -- is file?
- if os.isfile(file_or_dir) then
- -- remove file
- if not os.rmfile(file_or_dir) then
- return false, string.format("cannot remove file %s %s", file_or_dir, os.strerror())
- end
- -- is directory?
- elseif os.isdir(file_or_dir) then
- -- remove directory
- if not os.rmdir(file_or_dir) then
- return false, string.format("cannot remove directory %s %s", file_or_dir, os.strerror())
- end
+ -- check arguments
+ local args = {...}
+ if #args < 1 then
+ return false, string.format("invalid arguments: %s", table.concat(args, ' '))
end
- -- remove the super directory if be empty
- if rm_superdir_if_empty then
- local superdir = path.directory(file_or_dir)
- if os.isdir(superdir) then
- os.rmdir(superdir, true)
+ -- create directories
+ for _, filedir in ipairs(os.argw(args)) do
+ if not os._rm(filedir) then
+ return false, string.format("remove: %s failed!", filedir)
end
end
@@ -309,6 +350,46 @@ function os.cd(dir)
return true
end
+-- create directories
+function os.mkdir(...)
+
+ -- check arguments
+ local args = {...}
+ if #args < 1 then
+ return false, string.format("invalid arguments: %s", table.concat(args, ' '))
+ end
+
+ -- create directories
+ for _, dir in ipairs(os.argw(args)) do
+ if not os._mkdir(dir) then
+ return false, string.format("create directory: %s failed!", dir)
+ end
+ end
+
+ -- ok
+ return true
+end
+
+-- remove directories
+function os.rmdir(...)
+
+ -- check arguments
+ local args = {...}
+ if #args < 1 then
+ return false, string.format("invalid arguments: %s", table.concat(args, ' '))
+ 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)
+ end
+ end
+
+ -- ok
+ return true
+end
+
-- get the temporary directory
function os.tmpdir()
return path.join(os._tmpdir(), ".xmake")
@@ -341,7 +422,7 @@ function os.runv(shellname, argv)
local log = os.tmpfile()
-- execute it
- local ok = os.execv(shellname, os._translate_args(argv), log, log)
+ local ok = os.execv(shellname, os.argw(argv), log, log)
if ok ~= 0 then
-- make errors
@@ -379,7 +460,7 @@ function os.execv(shellname, argv, outfile, errfile)
-- open command
local ok = -1
- local proc = process.openv(shellname, os._translate_args(argv), outfile, errfile)
+ local proc = process.openv(shellname, os.argw(argv), outfile, errfile)
if proc ~= nil then
-- wait process
@@ -439,7 +520,7 @@ function os.iorunv(shellname, argv)
local errfile = os.tmpfile()
-- run command
- local ok = os.execv(shellname, os._translate_args(argv), outfile, errfile)
+ local ok = os.execv(shellname, os.argw(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 f45e68d83..d1af87a1e 100644
--- a/xmake/core/sandbox/modules/os.lua
+++ b/xmake/core/sandbox/modules/os.lua
@@ -35,6 +35,8 @@ local sandbox_os = sandbox_os or {}
-- inherit some builtin interfaces
sandbox_os.date = os.date
sandbox_os.time = os.time
+sandbox_os.argv = os.argv
+sandbox_os.argw = os.argw
sandbox_os.mtime = os.mtime
sandbox_os.mclock = os.mclock
@@ -55,38 +57,35 @@ function sandbox_os.cp(...)
end
-- move file or directory
-function sandbox_os.mv(src, dst)
+function sandbox_os.mv(...)
- -- 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.mv(src, dst)
+ local ok, errors = os.mv(unpack(args))
if not ok then
os.raise(errors)
end
-
end
--- remove file or directory
-function sandbox_os.rm(file_or_dir, rm_superdir_if_empty)
+-- remove files or directories
+function sandbox_os.rm(...)
- -- check
- assert(file_or_dir)
-
- -- format it first
- file_or_dir = vformat(file_or_dir)
+ -- format arguments
+ local args = {}
+ for _, arg in ipairs({...}) do
+ table.insert(args, vformat(arg))
+ end
-- done
- local ok, errors = os.rm(file_or_dir, rm_superdir_if_empty)
+ local ok, errors = os.rm(unpack(args))
if not ok then
os.raise(errors)
end
-
end
-- change to directory
@@ -111,37 +110,35 @@ function sandbox_os.cd(dir)
return olddir
end
--- create directory
-function sandbox_os.mkdir(dir)
-
- -- check
- assert(dir)
-
- -- format it first
- dir = vformat(dir)
+-- create directories
+function sandbox_os.mkdir(...)
+
+ -- format arguments
+ local args = {}
+ for _, arg in ipairs({...}) do
+ table.insert(args, vformat(arg))
+ end
-- done
- if not os.isdir(dir) then
- if not os.mkdir(dir) then
- os.raise("create directory: %s failed!", dir)
- end
+ local ok, errors = os.mkdir(unpack(args))
+ if not ok then
+ os.raise(errors)
end
end
--- remove directory
-function sandbox_os.rmdir(dir)
+-- remove directories
+function sandbox_os.rmdir(...)
- -- check
- assert(dir)
-
- -- format it first
- dir = vformat(dir)
+ -- format arguments
+ local args = {}
+ for _, arg in ipairs({...}) do
+ table.insert(args, vformat(arg))
+ end
-- done
- if os.isdir(dir) then
- if not os.rmdir(dir) then
- os.raise("remove directory: %s failed!", dir)
- end
+ local ok, errors = os.rmdir(unpack(args))
+ if not ok then
+ os.raise(errors)
end
end
diff --git a/xmake/plugins/lua/scripts/cat.lua b/xmake/plugins/lua/scripts/cat.lua
index 018b24f24..49536a0e8 100644
--- a/xmake/plugins/lua/scripts/cat.lua
+++ b/xmake/plugins/lua/scripts/cat.lua
@@ -26,9 +26,8 @@
function main(...)
-- cat all
- for _, v in ipairs(...) do
+ for _, v in ipairs(os.argw{...}) do
if os.isfile(v) then io.cat(v) end
end
print("")
-
end
diff --git a/xmake/plugins/lua/scripts/cp.lua b/xmake/plugins/lua/scripts/cp.lua
index 72153adb2..5da34feb6 100644
--- a/xmake/plugins/lua/scripts/cp.lua
+++ b/xmake/plugins/lua/scripts/cp.lua
@@ -24,8 +24,6 @@
-- main
function main(...)
-
- -- copy it
os.cp(...)
end
diff --git a/xmake/plugins/lua/scripts/echo.lua b/xmake/plugins/lua/scripts/echo.lua
index 1408adfc1..0714e1c23 100644
--- a/xmake/plugins/lua/scripts/echo.lua
+++ b/xmake/plugins/lua/scripts/echo.lua
@@ -26,9 +26,8 @@
function main(...)
-- echo all
- for _, v in ipairs(...) do
- printf("%s ", v:decode())
+ for _, v in ipairs(os.argw{...}) do
+ printf("%s ", v)
end
print("")
-
end
diff --git a/xmake/plugins/lua/scripts/lipo.lua b/xmake/plugins/lua/scripts/lipo.lua
index 01212a2b6..9cd9c4c96 100644
--- a/xmake/plugins/lua/scripts/lipo.lua
+++ b/xmake/plugins/lua/scripts/lipo.lua
@@ -33,7 +33,7 @@ import("core.tool.tool")
function main(...)
-- get arguments
- local args = ...
+ local args = {...}
if not args or #args ~= 1 then
raise("invalid arguments!")
end
diff --git a/xmake/plugins/lua/scripts/mkdir.lua b/xmake/plugins/lua/scripts/mkdir.lua
index a3bddc8f0..a3dafb3df 100644
--- a/xmake/plugins/lua/scripts/mkdir.lua
+++ b/xmake/plugins/lua/scripts/mkdir.lua
@@ -24,12 +24,6 @@
-- main
function main(...)
-
- -- mkdir all
- for _, dir in ipairs(...) do
- if not os.exists(dir) then
- os.mkdir(dir)
- end
- end
+ os.mkdir(...)
end
diff --git a/xmake/plugins/lua/scripts/mv.lua b/xmake/plugins/lua/scripts/mv.lua
index d00f95750..9b5c35973 100644
--- a/xmake/plugins/lua/scripts/mv.lua
+++ b/xmake/plugins/lua/scripts/mv.lua
@@ -24,11 +24,6 @@
-- main
function mv.main(...)
-
- -- mv it
- local pathes = ...
- if pathes and table.getn(pathes) == 2 then
- os.mv(pathes[1], pathes[2])
- end
+ os.mv(...)
end
diff --git a/xmake/plugins/lua/scripts/rm.lua b/xmake/plugins/lua/scripts/rm.lua
index 3f6b35a8a..2bf625349 100644
--- a/xmake/plugins/lua/scripts/rm.lua
+++ b/xmake/plugins/lua/scripts/rm.lua
@@ -24,13 +24,6 @@
-- main
function main(...)
-
- -- rm all
- for _, file_or_dir in ipairs(...) do
- if os.exists(file_or_dir) then
- os.rm(file_or_dir)
- end
- end
-
+ os.rm(...)
end
diff --git a/xmake/plugins/lua/scripts/rmdir.lua b/xmake/plugins/lua/scripts/rmdir.lua
index 15c97f7ae..76739814e 100644
--- a/xmake/plugins/lua/scripts/rmdir.lua
+++ b/xmake/plugins/lua/scripts/rmdir.lua
@@ -23,13 +23,7 @@
--
-- main
-function rmdir.main(self, ...)
-
- -- rmdir all
- for _, dir in ipairs(...) do
- if os.isdir(dir) then
- os.rmdir(dir)
- end
- end
-
+function main(...)
+ os.rmdir(...)
end
+
diff --git a/xmake/plugins/lua/scripts/verbose.lua b/xmake/plugins/lua/scripts/verbose.lua
deleted file mode 100644
index 8ba353655..000000000
--- a/xmake/plugins/lua/scripts/verbose.lua
+++ /dev/null
@@ -1,40 +0,0 @@
---!The Make-like Build Utility based on Lua
---
--- Licensed to the Apache Software Foundation (ASF) under one
--- or more contributor license agreements. See the NOTICE file
--- distributed with this work for additional information
--- regarding copyright ownership. The ASF licenses this file
--- to you under the Apache License, Version 2.0 (the
--- "License"); you may not use this file except in compliance
--- with the License. You may obtain a copy of the License at
---
--- http://www.apache.org/licenses/LICENSE-2.0
---
--- Unless required by applicable law or agreed to in writing, software
--- distributed under the License is distributed on an "AS IS" BASIS,
--- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
--- See the License for the specific language governing permissions and
--- limitations under the License.
---
--- Copyright (C) 2015 - 2017, TBOOX Open Source Group.
---
--- @author ruki
--- @file verbose.lua
---
-
--- imports
-import("core.base.option")
-
--- main
-function main(...)
-
- -- verbose all
- if option.get("verbose") then
- for _, v in ipairs(...) do
- printf("%s ", v:decode())
- end
- print("")
- end
-
-end
-
diff --git a/xmake/plugins/lua/xmake.lua b/xmake/plugins/lua/xmake.lua
index 8de34a0a2..fb5384bd5 100755
--- a/xmake/plugins/lua/xmake.lua
+++ b/xmake/plugins/lua/xmake.lua
@@ -52,9 +52,9 @@ task("lua")
-- import script
if os.isfile(name) then
- import(path.basename(name), {rootdir = path.directory(name)}).main(option.get("arguments"))
+ import(path.basename(name), {rootdir = path.directory(name)}).main(unpack(option.get("arguments")))
else
- import("scripts." .. name).main(option.get("arguments"))
+ import("scripts." .. name).main(unpack(option.get("arguments")))
end
end)