Skip to content

Commit

Permalink
Merge pull request JohnSundell#31 from LarsJK/renaming-recursive-sequ…
Browse files Browse the repository at this point in the history
…ence

Pass Folder instead of path to Iterator so that changes propagate
  • Loading branch information
JohnSundell authored Oct 21, 2017
2 parents 47afbf7 + f94ec93 commit 4338378
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 19 deletions.
38 changes: 19 additions & 19 deletions Sources/Files.swift
Original file line number Diff line number Diff line change
Expand Up @@ -777,7 +777,7 @@ public final class Folder: FileSystem.Item, FileSystemIterable {
* If `recursive = true` the folder tree will be traversed depth-first
*/
public func makeFileSequence(recursive: Bool = false, includeHidden: Bool = false) -> FileSystemSequence<File> {
return FileSystemSequence(path: path, recursive: recursive, includeHidden: includeHidden, using: fileManager)
return FileSystemSequence(folder: self, recursive: recursive, includeHidden: includeHidden, using: fileManager)
}

/**
Expand All @@ -789,7 +789,7 @@ public final class Folder: FileSystem.Item, FileSystemIterable {
* If `recursive = true` the folder tree will be traversed depth-first
*/
public func makeSubfolderSequence(recursive: Bool = false, includeHidden: Bool = false) -> FileSystemSequence<Folder> {
return FileSystemSequence(path: path, recursive: recursive, includeHidden: includeHidden, using: fileManager)
return FileSystemSequence(folder: self, recursive: recursive, includeHidden: includeHidden, using: fileManager)
}

/**
Expand Down Expand Up @@ -870,22 +870,22 @@ public class FileSystemSequence<T: FileSystem.Item>: Sequence where T: FileSyste
forEach { item = $0 }
return item
}
private let path: String

private let folder: Folder
private let recursive: Bool
private let includeHidden: Bool
private let fileManager: FileManager
fileprivate init(path: String, recursive: Bool, includeHidden: Bool, using fileManager: FileManager) {
self.path = path

fileprivate init(folder: Folder, recursive: Bool, includeHidden: Bool, using fileManager: FileManager) {
self.folder = folder
self.recursive = recursive
self.includeHidden = includeHidden
self.fileManager = fileManager
}

/// Create an iterator to use to iterate over the sequence
public func makeIterator() -> FileSystemIterator<T> {
return FileSystemIterator(path: path, recursive: recursive, includeHidden: includeHidden, using: fileManager)
return FileSystemIterator(folder: folder, recursive: recursive, includeHidden: includeHidden, using: fileManager)
}

/// Move all the items in this sequence to a new folder. See `FileSystem.Item.move(to:)` for more info.
Expand All @@ -896,20 +896,21 @@ public class FileSystemSequence<T: FileSystem.Item>: Sequence where T: FileSyste

/// Iterator used to iterate over an instance of `FileSystemSequence`
public class FileSystemIterator<T: FileSystem.Item>: IteratorProtocol where T: FileSystemIterable {
private let path: String
private let folder: Folder
private let recursive: Bool
private let includeHidden: Bool
private let fileManager: FileManager
private var itemNames: [String]
private lazy var itemNames: [String] = {
self.fileManager.itemNames(inFolderAtPath: self.folder.path)
}()
private lazy var childIteratorQueue = [FileSystemIterator]()
private var currentChildIterator: FileSystemIterator?
fileprivate init(path: String, recursive: Bool, includeHidden: Bool, using fileManager: FileManager) {
self.path = path

fileprivate init(folder: Folder, recursive: Bool, includeHidden: Bool, using fileManager: FileManager) {
self.folder = folder
self.recursive = recursive
self.includeHidden = includeHidden
self.fileManager = fileManager
self.itemNames = fileManager.itemNames(inFolderAtPath: path)
}

/// Advance the iterator to the next element
Expand All @@ -935,12 +936,11 @@ public class FileSystemIterator<T: FileSystem.Item>: IteratorProtocol where T: F
return next()
}

let nextItemPath = path + nextItemName
let nextItemPath = folder.path + nextItemName
let nextItem = try? T(path: nextItemPath, using: fileManager)

if recursive && nextItem?.kind != .file {
let childPath = nextItemPath + "/"
let child = FileSystemIterator(path: childPath, recursive: true, includeHidden: includeHidden, using: fileManager)

if recursive, let folder = (nextItem as? Folder) ?? (try? Folder(path: nextItemPath)) {
let child = FileSystemIterator(folder: folder, recursive: true, includeHidden: includeHidden, using: fileManager)
childIteratorQueue.append(child)
}

Expand Down
25 changes: 25 additions & 0 deletions Tests/FilesTests/FilesTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,30 @@ class FilesTests: XCTestCase {
XCTAssertEqual(sequence.count, 6)
}
}

func testRenamingFoldersWhileEnumeratingSubfoldersRecursively() {
performTest {
let subfolder1 = try folder.createSubfolder(named: "1")
let subfolder2 = try folder.createSubfolder(named: "2")

try subfolder1.createSubfolder(named: "1A")
try subfolder1.createSubfolder(named: "1B")

try subfolder2.createSubfolder(named: "2A")
try subfolder2.createSubfolder(named: "2B")

let sequence = folder.makeSubfolderSequence(recursive: true)

for folder in sequence {
try folder.rename(to: "Folder " + folder.name)
}

let expectedNames = ["Folder 1", "Folder 1A", "Folder 1B", "Folder 2", "Folder 2A", "Folder 2B"]

XCTAssertEqual(sequence.names.sorted(), expectedNames)
XCTAssertEqual(sequence.count, 6)
}
}

func testFirstAndLastInFileSequence() {
performTest {
Expand Down Expand Up @@ -662,6 +686,7 @@ class FilesTests: XCTestCase {
("testEnumeratingFilesRecursively", testEnumeratingFilesRecursively),
("testEnumeratingSubfolders", testEnumeratingSubfolders),
("testEnumeratingSubfoldersRecursively", testEnumeratingSubfoldersRecursively),
("testRenamingFoldersWhileEnumeratingSubfoldersRecursively", testRenamingFoldersWhileEnumeratingSubfoldersRecursively),
("testFirstAndLastInFileSequence", testFirstAndLastInFileSequence),
("testParent", testParent),
("testRootFolderParentIsNil", testRootFolderParentIsNil),
Expand Down

0 comments on commit 4338378

Please sign in to comment.