Advertisement
marcusziade

Remove Xcode file headers in swift

Jan 25th, 2023 (edited)
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 1.39 KB | None | 0 0
  1. import Foundation
  2.  
  3. func stripHeaders(directory: String) {
  4.     let fileManager = FileManager.default
  5.     do {
  6.         let files = try fileManager.contentsOfDirectory(atPath: directory)
  7.         for file in files {
  8.             if file.hasSuffix(".swift") {
  9.                 let filepath = directory + "/" + file
  10.                 let fileURL = URL(fileURLWithPath: filepath)
  11.                 var inHeader = true
  12.                 var lines: [String] = []
  13.                 do {
  14.                     let fileContent = try String(contentsOf: fileURL)
  15.                     lines = fileContent.components(separatedBy: .newlines)
  16.                     var newContent = ""
  17.                     for line in lines {
  18.                         if inHeader && line.hasPrefix("//") {
  19.                             continue
  20.                         } else if inHeader && !line.hasPrefix("//") {
  21.                             inHeader = false
  22.                         }
  23.                         newContent += line + "\n"
  24.                     }
  25.                     try newContent.write(to: fileURL, atomically: true, encoding: .utf8)
  26.                 } catch {
  27.                     print("Error while reading/writing file: \(error)")
  28.                 }
  29.             }
  30.         }
  31.     } catch {
  32.         print("Error while fetching contents of directory: \(error)")
  33.     }
  34. }
  35.  
  36. Example: stripHeaders(directory: "path/to/swift/source/files")
  37.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement