FileManager+Utils.swift 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. /*
  2. * Copyright 2019 Google
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. import Foundation
  17. // Extensions to FileManager that make scripting easier or cleaner for error reporting.
  18. public extension FileManager {
  19. // MARK: - Helper Enum Declarations
  20. /// Describes a type of file to be searched for.
  21. enum SearchFileType {
  22. /// All files, not including folders.
  23. case allFiles
  24. /// All folders with a `.bundle` extension.
  25. case bundles
  26. /// A directory with an optional name. If name is `nil`, all directories will be matched.
  27. case directories(name: String?)
  28. /// All folders with a `.framework` extension.
  29. case frameworks
  30. /// All headers with a `.h` extension.
  31. case headers
  32. /// All files with the `.storyboard` extension.
  33. case storyboards
  34. }
  35. // MARK: - Error Declarations
  36. /// Errors that can be used to propagate up through the script related to files.
  37. enum FileError: Error {
  38. case directoryNotFound(path: String)
  39. case failedToCreateDirectory(path: String, error: Error)
  40. case writeToFileFailed(file: String, error: Error)
  41. }
  42. /// Errors that can occur during a recursive search operation.
  43. enum RecursiveSearchError: Error {
  44. case failedToCreateEnumerator(forDirectory: URL)
  45. }
  46. // MARK: - Directory Management
  47. /// Convenience function to determine if there's a directory at the given file URL using existing
  48. /// FileManager calls.
  49. func directoryExists(at url: URL) -> Bool {
  50. var isDir: ObjCBool = false
  51. let exists = fileExists(atPath: url.path, isDirectory: &isDir)
  52. return exists && isDir.boolValue
  53. }
  54. /// Convenience function to determine if a given file URL is a directory.
  55. func isDirectory(at url: URL) -> Bool {
  56. return directoryExists(at: url)
  57. }
  58. /// Returns the URL to the source Pod cache directory, and creates it if it doesn't exist.
  59. func sourcePodCacheDirectory(withSubdir subdir: String = "") throws -> URL {
  60. let cacheDir = FileManager.default.temporaryDirectory(withName: "cache")
  61. let cacheRoot = cacheDir.appendingPathComponents([subdir])
  62. if directoryExists(at: cacheRoot) {
  63. return cacheRoot
  64. }
  65. // The cache root folder doesn't exist yet, create it.
  66. try createDirectory(at: cacheRoot, withIntermediateDirectories: true)
  67. return cacheRoot
  68. }
  69. /// Removes a directory or file if it exists. This is helpful to clean up error handling for checks that
  70. /// shouldn't fail. The only situation this could potentially fail is permission errors or if a
  71. /// folder is open in Finder, and in either state the user needs to close the window or fix the
  72. /// permissions. A fatal error will be thrown in those situations.
  73. func removeIfExists(at url: URL) {
  74. guard directoryExists(at: url) || fileExists(atPath: url.path) else { return }
  75. do {
  76. try removeItem(at: url)
  77. } catch {
  78. fatalError("""
  79. Tried to remove directory \(url) but it failed - close any Finder windows and try again.
  80. Error: \(error)
  81. """)
  82. }
  83. }
  84. /// Enable a single unique temporary workspace per execution with a sortable and readable timestamp.
  85. private static func timeStamp() -> String {
  86. let formatter = DateFormatter()
  87. formatter.dateFormat = "YYYY-MM-dd'T'HH-mm-ss"
  88. return formatter.string(from: Date())
  89. }
  90. static let unique: String = timeStamp()
  91. /// Allow clients to override default location for temporary directory creation
  92. static var buildRoot: URL?
  93. static func registerBuildRoot(buildRoot: URL) {
  94. FileManager.buildRoot = buildRoot
  95. }
  96. /// Returns a deterministic path of a temporary directory for the given name. Note: This does
  97. /// *not* create the directory if it doesn't exist, merely generates the name for creation.
  98. func temporaryDirectory(withName name: String) -> URL {
  99. // Get access to the temporary directory. This could be passed in via `LaunchArgs`, or use the
  100. // default temporary directory.
  101. let tempDir: URL
  102. if let root = FileManager.buildRoot {
  103. tempDir = root
  104. } else
  105. if #available(OSX 10.12, *) {
  106. tempDir = temporaryDirectory
  107. } else {
  108. tempDir = URL(fileURLWithPath: NSTemporaryDirectory())
  109. }
  110. // Organize all temporary directories into a "ZipRelease" directory.
  111. let unique = FileManager.unique
  112. let zipDir = tempDir.appendingPathComponent("ZipRelease/" + unique, isDirectory: true)
  113. return zipDir.appendingPathComponent(name, isDirectory: true)
  114. }
  115. // MARK: Searching
  116. /// Recursively search for a set of items in a particular directory.
  117. func recursivelySearch(for type: SearchFileType, in dir: URL) throws -> [URL] {
  118. // Throw an error so an appropriate error can be logged from the caller.
  119. guard directoryExists(at: dir) else {
  120. throw FileError.directoryNotFound(path: dir.path)
  121. }
  122. // We have a directory, create an enumerator to do a recursive search.
  123. let keys: [URLResourceKey] = [.nameKey, .isDirectoryKey]
  124. guard let dirEnumerator = enumerator(at: dir, includingPropertiesForKeys: keys) else {
  125. // Throw an error so an appropriate error can be logged from the caller.
  126. throw RecursiveSearchError.failedToCreateEnumerator(forDirectory: dir)
  127. }
  128. // Recursively search using the enumerator, adding any matches to the array.
  129. var matches: [URL] = []
  130. var foundXcframework = false // Ignore .frameworks after finding an xcframework.
  131. while let fileURL = dirEnumerator.nextObject() as? URL {
  132. switch type {
  133. case .allFiles:
  134. // Skip directories, include everything else.
  135. guard !isDirectory(at: fileURL) else { continue }
  136. matches.append(fileURL)
  137. case let .directories(name):
  138. // Skip any non-directories.
  139. guard directoryExists(at: fileURL) else { continue }
  140. // Get the name of the directory we're searching for. If there's not a specific name
  141. // being searched for, add it as a match and move on.
  142. guard let name = name else {
  143. matches.append(fileURL)
  144. continue
  145. }
  146. // If the last path component is a match, it's a directory we're looking for!
  147. if fileURL.lastPathComponent == name {
  148. matches.append(fileURL)
  149. }
  150. case .bundles:
  151. // The only thing of interest is the path extension being ".bundle".
  152. if fileURL.pathExtension == "bundle" {
  153. matches.append(fileURL)
  154. }
  155. case .headers:
  156. if fileURL.pathExtension == "h" {
  157. matches.append(fileURL)
  158. }
  159. case .storyboards:
  160. // The only thing of interest is the path extension being ".storyboard".
  161. if fileURL.pathExtension == "storyboard" {
  162. matches.append(fileURL)
  163. }
  164. case .frameworks:
  165. // We care if it's a directory and has a .xcframework or .framework extension.
  166. if directoryExists(at: fileURL) {
  167. if fileURL.pathExtension == "xcframework" {
  168. matches.append(fileURL)
  169. foundXcframework = true
  170. } else if !foundXcframework, fileURL.pathExtension == "framework" {
  171. matches.append(fileURL)
  172. }
  173. }
  174. }
  175. }
  176. return matches
  177. }
  178. }