make os.match to get exclude patterns for dirs and files, os.rmdir now gets all dirs and filter out . and .. manually

This commit is contained in:
Alexey Orlov 2014-09-23 18:41:45 +03:00
parent 65a89ab098
commit fd590f9df9
2 changed files with 67 additions and 10 deletions

View File

@ -252,11 +252,24 @@
-- directory name, "**" to recurse into subdirectories. -- directory name, "**" to recurse into subdirectories.
-- @param matchFiles -- @param matchFiles
-- True to match against files, false to match directories. -- True to match against files, false to match directories.
-- @param excludeDirs
-- Lua Pattern.
-- If any encountered dir matches this pattern, it will be discarded (both from results and from recursive decent).
-- If it is nil, it will be set to "%..*", to keep backward compatibility.
-- To make it match all dirs, you need to pass un-matchable pattern; we use "a^" for this internally.
-- @param excludeFiles
-- Lua Pattern.
-- If any encountered file matches this pattern, it will be discarded from results.
-- If it is nil, it will be set to "a^" to match no files.
-- @return -- @return
-- A table containing the matched file or directory names. -- A table containing the matched file or directory names.
--- ---
function os.match(mask, matchFiles) function os.match(mask, matchFiles, excludeDirs, excludeFiles)
-- patch up exclude masks to default values
excludeDirs = excludeDirs and excludeDirs or "%..*"
excludeFiles = excludeFiles and excludeFiles or "a^"
-- Strip any extraneous weirdness from the mask to ensure a good -- Strip any extraneous weirdness from the mask to ensure a good
-- match against the paths returned by the OS. I don't know if I've -- match against the paths returned by the OS. I don't know if I've
-- caught all the possibilities here yet; will add more as I go. -- caught all the possibilities here yet; will add more as I go.
@ -293,7 +306,7 @@
local isfile = os.matchisfile(m) local isfile = os.matchisfile(m)
if (matchFiles and isfile) or (not matchFiles and not isfile) then if (matchFiles and isfile) or (not matchFiles and not isfile) then
local fname = os.matchname(m) local fname = os.matchname(m)
if isfile or not fname:startswith(".") then if (isfile and not fname:match(excludeFiles)) or (not isfile and not fname:match(excludeDirs)) then
fname = path.join(basedir, fname) fname = path.join(basedir, fname)
if fname:match(mask) == fname then if fname:match(mask) == fname then
table.insert(result, fname) table.insert(result, fname)
@ -309,7 +322,8 @@
while os.matchnext(m) do while os.matchnext(m) do
if not os.matchisfile(m) then if not os.matchisfile(m) then
local dirname = os.matchname(m) local dirname = os.matchname(m)
if (not dirname:startswith(".")) then -- always exclude phony dirs: . and ..
if not (dirname == "." or dirname == ".." or dirname:match(excludeDirs)) then
matchwalker(path.join(basedir, dirname)) matchwalker(path.join(basedir, dirname))
end end
end end
@ -334,7 +348,7 @@
--- ---
function os.matchdirs(mask) function os.matchdirs(mask)
return os.match(mask, false) return os.match(mask, false, "%..*", nil)
end end
@ -349,7 +363,7 @@
--- ---
function os.matchfiles(mask) function os.matchfiles(mask)
return os.match(mask, true) return os.match(mask, true, "%..*", nil)
end end
@ -439,10 +453,12 @@
local builtin_rmdir = os.rmdir local builtin_rmdir = os.rmdir
function os.rmdir(p) function os.rmdir(p)
-- recursively remove subdirectories -- recursively remove subdirectories: exclude no dirs apart builtin . and ..
local dirs = os.matchdirs(p .. "/*") local dirs = os.match(p .. "/*", false, "a^", nil)
for _, dname in ipairs(dirs) do for _, dname in ipairs(dirs) do
os.rmdir(dname) if not (dname:endswith("/.") or dname:endswith("/..")) then
os.rmdir(dname)
end
end end
-- remove any files -- remove any files

View File

@ -78,8 +78,9 @@
end end
function suite.matchfiles_SkipsDotDirs_OnRecursive() function suite.matchfiles_SkipsDotDirs_OnRecursive()
local result = os.matchfiles("**.lua") local result = os.matchfiles("../**.lua*")
test.isfalse(table.contains(result, ".svn/text-base/testfx.lua.svn-base")) test.istrue(table.contains(result, "../src/_manifest.lua"))
test.isfalse(table.contains(result, "../.hg/store/data/scripts/test.lua.i"))
end end
function suite.matchfiles_OnSubfolderMatch() function suite.matchfiles_OnSubfolderMatch()
@ -111,6 +112,31 @@
--
-- os.match() tests for exclude patterns
--
function suite.match_DirsCustomExcludeDirs()
local result = os.match("*", false, "a.*")
test.istrue(table.contains(result, ".."))
test.isfalse(table.contains(result, "actions"))
test.isfalse(table.contains(result, "api"))
end
function suite.match_FilesCustomExcludeDirs()
local result = os.match("../**.lua*", true, "src")
test.isfalse(table.contains(result, "../src/_manifest.lua"))
test.istrue(table.contains(result, "../.hg/store/data/scripts/test.lua.i"))
end
function suite.match_FilesCustomExcludeFiles()
local result = os.match("**.lua", true, nil, "ok.*")
test.istrue(table.contains(result, "folder/premake5.lua"))
test.isfalse(table.contains(result, "folder/ok.lua"))
end
-- --
-- os.pathsearch() tests -- os.pathsearch() tests
-- --
@ -130,3 +156,18 @@
function suite.pathsearch_NilPathsAllowed() function suite.pathsearch_NilPathsAllowed()
test.isequal(_TESTS_DIR, os.pathsearch("_manifest.lua", nil, _TESTS_DIR, nil)) test.isequal(_TESTS_DIR, os.pathsearch("_manifest.lua", nil, _TESTS_DIR, nil))
end end
--
-- os.mkdir() and os.rmdir() tests
--
function suite.mkdirrmdir_WorksWithDottesDirs()
os.mkdir("__TEST/.TEST")
test.istrue(table.contains(os.matchdirs("*"), "__TEST"))
test.istrue(table.contains(os.match("**", false, "a^"), "__TEST/.TEST"))
os.rmdir("__TEST")
test.isfalse(table.contains(os.matchdirs("*"), "__TEST"))
test.isfalse(table.contains(os.match("**", false, "a^"), "__TEST/.TEST"))
end