|
| 1 | +//===----------------------------------------------------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift.org open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2014 - 2018 Apple Inc. and the Swift project authors |
| 6 | +// Licensed under Apache License v2.0 with Runtime Library Exception |
| 7 | +// |
| 8 | +// See https://swift.org/LICENSE.txt for license information |
| 9 | +// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors |
| 10 | +// |
| 11 | +//===----------------------------------------------------------------------===// |
| 12 | + |
| 13 | +import Foundation |
| 14 | + |
| 15 | +/// Iterator for looping over lists of files and directories. Directories are automatically |
| 16 | +/// traversed recursively, and we check for files with a ".swift" extension. |
| 17 | +struct FileIterator: Sequence, IteratorProtocol { |
| 18 | + |
| 19 | + /// List of file and directory paths to iterate over. |
| 20 | + let paths: [String] |
| 21 | + |
| 22 | + /// Iterator for "paths" list. |
| 23 | + var pathIterator: Array<String>.Iterator |
| 24 | + |
| 25 | + /// Iterator for recursing through directories. |
| 26 | + var dirIterator: FileManager.DirectoryEnumerator? = nil |
| 27 | + |
| 28 | + /// Keep track of the current directory we're recursing through. |
| 29 | + var currentDirectory: String = "" |
| 30 | + |
| 31 | + /// Keep track of paths we have visited to prevent duplicates. |
| 32 | + var visited: Set<String> = [] |
| 33 | + |
| 34 | + /// The file extension to check for when recursing through directories. |
| 35 | + let fileSuffix = ".swift" |
| 36 | + |
| 37 | + /// The input is a list of paths as Strings. Some will be file paths, and others directories. |
| 38 | + public init(paths: [String]) { |
| 39 | + self.paths = paths |
| 40 | + self.pathIterator = self.paths.makeIterator() |
| 41 | + } |
| 42 | + |
| 43 | + /// Iterate through the "paths" list, and emit the file paths in it. If we encounter a directory, |
| 44 | + /// recurse through it and emit .swift file paths. |
| 45 | + mutating func next() -> String? { |
| 46 | + var output: String? = nil |
| 47 | + while output == nil { |
| 48 | + // Check if we're recursing through a directory |
| 49 | + if dirIterator != nil { |
| 50 | + output = nextInDirectory() |
| 51 | + } |
| 52 | + else { |
| 53 | + guard let next = pathIterator.next() else { return nil } |
| 54 | + var isDir: ObjCBool = false |
| 55 | + if FileManager.default.fileExists(atPath: next, isDirectory: &isDir) { |
| 56 | + if isDir.boolValue { |
| 57 | + dirIterator = FileManager.default.enumerator(atPath: next) |
| 58 | + currentDirectory = next |
| 59 | + } |
| 60 | + else { output = next } |
| 61 | + } |
| 62 | + else { |
| 63 | + // If a path doesn't exist, allow it pass down into the SwiftFormat API so it can throw |
| 64 | + // the appropriate exception. We don't want to kill the entire process if this happens. |
| 65 | + output = next |
| 66 | + } |
| 67 | + } |
| 68 | + if let out = output, visited.contains(out) { output = nil } |
| 69 | + } |
| 70 | + if let out = output { visited.insert(out) } |
| 71 | + return output |
| 72 | + } |
| 73 | + |
| 74 | + /// Use the FileManager API to recurse through directories and emit .swift file paths. |
| 75 | + private mutating func nextInDirectory() -> String? { |
| 76 | + var output: String? = nil |
| 77 | + while output == nil { |
| 78 | + var isDir: ObjCBool = false |
| 79 | + if let item = dirIterator?.nextObject() as? String { |
| 80 | + if item.hasSuffix(fileSuffix) |
| 81 | + && FileManager.default.fileExists( |
| 82 | + atPath: currentDirectory + "/" + item, isDirectory: &isDir) |
| 83 | + && !isDir.boolValue |
| 84 | + { |
| 85 | + output = currentDirectory + "/" + item |
| 86 | + } |
| 87 | + } |
| 88 | + else { break } |
| 89 | + } |
| 90 | + // If we've exhausted the files in the directory recursion, unset the directory iterator. |
| 91 | + if output == nil { dirIterator = nil } |
| 92 | + return output |
| 93 | + } |
| 94 | +} |
0 commit comments