diff options
| author | ruki <[email protected]> | 2023-11-17 22:31:31 +0800 |
|---|---|---|
| committer | ruki <[email protected]> | 2023-11-17 22:31:31 +0800 |
| commit | 02c004d42d70979f3ca983dd960291761800e2a6 (patch) | |
| tree | a4ce4eb532be9d9fbf8e90cbe45524170d7435e5 /xmake/core/base/os.lua | |
| parent | bd74082ad5740e0b97ffe237216dd533eb3d0383 (diff) | |
improve os.rm to support remove emptydirs
Diffstat (limited to 'xmake/core/base/os.lua')
| -rw-r--r-- | xmake/core/base/os.lua | 30 |
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 |
