Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import Foundation
- func stripHeaders(directory: String) {
- let fileManager = FileManager.default
- do {
- let files = try fileManager.contentsOfDirectory(atPath: directory)
- for file in files {
- if file.hasSuffix(".swift") {
- let filepath = directory + "/" + file
- let fileURL = URL(fileURLWithPath: filepath)
- var inHeader = true
- var lines: [String] = []
- do {
- let fileContent = try String(contentsOf: fileURL)
- lines = fileContent.components(separatedBy: .newlines)
- var newContent = ""
- for line in lines {
- if inHeader && line.hasPrefix("//") {
- continue
- } else if inHeader && !line.hasPrefix("//") {
- inHeader = false
- }
- newContent += line + "\n"
- }
- try newContent.write(to: fileURL, atomically: true, encoding: .utf8)
- } catch {
- print("Error while reading/writing file: \(error)")
- }
- }
- }
- } catch {
- print("Error while fetching contents of directory: \(error)")
- }
- }
- Example: stripHeaders(directory: "path/to/swift/source/files")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement