summaryrefslogtreecommitdiff
path: root/xmake/core/base/os.lua
diff options
context:
space:
mode:
authorruki <[email protected]>2023-11-17 11:03:51 +0800
committerGitHub <[email protected]>2023-11-17 11:03:51 +0800
commitcf3afe37b546efb93079fd6314c0769884ca0fb1 (patch)
tree732f74fe2fd2922f3c95e8542de82cb6ee108f1c /xmake/core/base/os.lua
parentb6bb372d19bd2f6ac931d3e0e096b4fa07024cfb (diff)
parente0f074c79be1f712d09df9f7b4b32245ba95f1c9 (diff)
Merge pull request #4392 from xmake-io/emptydirs
improve os.rm to support remove emptydirs
Diffstat (limited to 'xmake/core/base/os.lua')
-rw-r--r--xmake/core/base/os.lua30
1 files changed, 28 insertions, 2 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