summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorruki <[email protected]>2023-11-17 22:31:31 +0800
committerruki <[email protected]>2023-11-17 22:31:31 +0800
commit02c004d42d70979f3ca983dd960291761800e2a6 (patch)
treea4ce4eb532be9d9fbf8e90cbe45524170d7435e5
parentbd74082ad5740e0b97ffe237216dd533eb3d0383 (diff)
improve os.rm to support remove emptydirs
-rw-r--r--xmake/core/base/os.lua30
-rw-r--r--xmake/core/sandbox/modules/os.lua8
-rw-r--r--xmake/modules/private/action/clean/remove_files.lua10
-rw-r--r--xmake/modules/private/utils/batchcmds.lua6
4 files changed, 36 insertions, 18 deletions
diff --git a/xmake/core/base/os.lua b/xmake/core/base/os.lua
index 7ae34ccc2..ed5490b7d 100644
--- a/xmake/core/base/os.lua
+++ b/xmake/core/base/os.lua
@@ -159,6 +159,19 @@ function os._rm(filedir)
return true
end
+-- remove empty parent directories of this file path
+function os._rm_empty_parentdirs(filepath)
+ local parentdir = path.directory(filepath)
+ while parentdir and os.isdir(parentdir) and os.emptydir(parentdir) do
+ local ok, errors = os._rm(parentdir)
+ if not ok then
+ return false, errors
+ end
+ parentdir = path.directory(parentdir)
+ end
+ return true
+end
+
-- get the ramdisk root directory
-- https://github.com/xmake-io/xmake/issues/3408
function os._ramdir()
@@ -464,7 +477,7 @@ function os.mv(srcpath, dstpath, opt)
end
-- remove files or directories
-function os.rm(filepath)
+function os.rm(filepath, opt)
-- check arguments
if not filepath then
@@ -472,16 +485,29 @@ function os.rm(filepath)
end
-- remove file or directories
+ opt = opt or {}
filepath = tostring(filepath)
local filepathes = os._match_wildcard_pathes(filepath)
if type(filepathes) == "string" then
- return os._rm(filepathes)
+ local ok, errors = os._rm(filepathes)
+ if not ok then
+ return false, errors
+ end
+ if opt.emptydirs then
+ return os._rm_empty_parentdirs(filepathes)
+ end
else
for _, _filepath in ipairs(filepathes) do
local ok, errors = os._rm(_filepath)
if not ok then
return false, errors
end
+ if opt.emptydirs then
+ ok, errors = os._rm_empty_parentdirs(filepath)
+ if not ok then
+ return false, errors
+ end
+ end
end
end
return true
diff --git a/xmake/core/sandbox/modules/os.lua b/xmake/core/sandbox/modules/os.lua
index c23ffb261..9004f66ee 100644
--- a/xmake/core/sandbox/modules/os.lua
+++ b/xmake/core/sandbox/modules/os.lua
@@ -152,12 +152,12 @@ function sandbox_os.vmv(srcpath, dstpath, opt)
end
-- remove file or directory with the verbose info
-function sandbox_os.vrm(filepath)
+function sandbox_os.vrm(filepath, opt)
assert(filepath)
if option.get("verbose") then
utils.cprint("${dim}> remove %s", filepath)
end
- return sandbox_os.rm(filepath)
+ return sandbox_os.rm(filepath, opt)
end
-- link file or directory with the verbose info
@@ -182,9 +182,9 @@ function sandbox_os.trymv(srcpath, dstpath)
end
-- try to remove files or directories
-function sandbox_os.tryrm(filepath)
+function sandbox_os.tryrm(filepath, opt)
assert(filepath)
- return os.rm(vformat(filepath))
+ return os.rm(vformat(filepath), opt)
end
-- change to directory
diff --git a/xmake/modules/private/action/clean/remove_files.lua b/xmake/modules/private/action/clean/remove_files.lua
index 606bb405d..3d62c6b78 100644
--- a/xmake/modules/private/action/clean/remove_files.lua
+++ b/xmake/modules/private/action/clean/remove_files.lua
@@ -25,14 +25,6 @@ import("core.base.option")
function main(filedirs, opt)
opt = opt or {}
for _, filedir in ipairs(filedirs) do
- os.tryrm(filedir)
- if option.get("all") or opt.emptydir then
- -- remove it if the parent directory is empty
- local parentdir = path.directory(filedir)
- while parentdir and os.isdir(parentdir) and os.emptydir(parentdir) do
- os.tryrm(parentdir)
- parentdir = path.directory(parentdir)
- end
- end
+ os.tryrm(filedir, {emptydirs = option.get("all") or opt.emptydir})
end
end
diff --git a/xmake/modules/private/utils/batchcmds.lua b/xmake/modules/private/utils/batchcmds.lua
index cbe924f53..0000b7111 100644
--- a/xmake/modules/private/utils/batchcmds.lua
+++ b/xmake/modules/private/utils/batchcmds.lua
@@ -140,7 +140,7 @@ end
function _runcmd_rm(cmd, opt)
local filepath = cmd.filepath
if not opt.dryrun then
- os.tryrm(filepath)
+ os.tryrm(filepath, opt)
end
end
@@ -148,7 +148,7 @@ end
function _runcmd_tryrm(cmd, opt)
local filepath = cmd.filepath
if not opt.dryrun then
- os.tryrm(filepath)
+ os.tryrm(filepath, opt)
end
end
@@ -156,7 +156,7 @@ end
function _runcmd_rmdir(cmd, opt)
local dir = cmd.dir
if not opt.dryrun and os.isdir(dir) then
- os.tryrm(dir)
+ os.tryrm(dir, opt)
end
end