Merge pull request #9803 from GULPF/walkdirrec-relative

Add `relative` parameter to walkDirRec
This commit is contained in:
Andreas Rumpf 2018-12-06 09:29:35 +01:00 • committed by GitHub
commit 160a034643
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 31 additions and 7 deletions

View file

@ -1564,11 +1564,14 @@ iterator walkDir*(dir: string; relative=false): tuple[kind: PathComponent, path:
k = getSymlinkFileKind(y) k = getSymlinkFileKind(y)
yield (k, y) yield (k, y)
iterator walkDirRec*(dir: string, yieldFilter = {pcFile}, iterator walkDirRec*(dir: string,
followFilter = {pcDir}): string {.tags: [ReadDirEffect].} = yieldFilter = {pcFile}, followFilter = {pcDir},
relative = false): string {.tags: [ReadDirEffect].} =
## Recursively walks over the directory `dir` and yields for each file ## Recursively walks over the directory `dir` and yields for each file
## or directory in `dir`. ## or directory in `dir`.
## The full path for each file or directory is returned. ## If ``relative`` is true the resulting path is
## shortened to be relative to ``dir``, otherwise the full path is returned.
##
## **Warning**: ## **Warning**:
## Modifying the directory structure while the iterator ## Modifying the directory structure while the iterator
## is traversing may result in undefined behavior! ## is traversing may result in undefined behavior!
@ -1591,13 +1594,15 @@ iterator walkDirRec*(dir: string, yieldFilter = {pcFile},
## ``pcLinkToDir`` follow symbolic links to directories ## ``pcLinkToDir`` follow symbolic links to directories
## --------------------- --------------------------------------------- ## --------------------- ---------------------------------------------
## ##
var stack = @[dir] var stack = @[""]
while stack.len > 0: while stack.len > 0:
for k, p in walkDir(stack.pop()): let d = stack.pop()
for k, p in walkDir(dir / d, relative = true):
let rel = d / p
if k in {pcDir, pcLinkToDir} and k in followFilter: if k in {pcDir, pcLinkToDir} and k in followFilter:
stack.add(p) stack.add rel
if k in yieldFilter: if k in yieldFilter:
yield p yield if relative: rel else: dir / rel
proc rawRemoveDir(dir: string) {.noNimScript.} = proc rawRemoveDir(dir: string) {.noNimScript.} =
when defined(windows): when defined(windows):

View file

@ -170,6 +170,25 @@ block modificationTime:
echo getLastModificationTime("a") == tm echo getLastModificationTime("a") == tm
removeFile("a") removeFile("a")
block walkDirRec:
createDir("walkdir_test/a/b")
open("walkdir_test/a/b/file_1", fmWrite).close()
open("walkdir_test/a/file_2", fmWrite).close()
for p in walkDirRec("walkdir_test"):
doAssert p.fileExists
doAssert p.startsWith("walkdir_test")
var s: seq[string]
for p in walkDirRec("walkdir_test", {pcFile}, {pcDir}, relative=true):
s.add(p)
doAssert s.len == 2
doAssert "a" / "b" / "file_1" in s
doAssert "a" / "file_2" in s
removeDir("walkdir_test")
block normalizedPath: block normalizedPath:
when defined(posix): when defined(posix):
block relative: block relative: