-
-
Save bocato/ef67d4525e297c49ee9224741ffdc03b to your computer and use it in GitHub Desktop.
iOS File Header remover script
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
func findFiles(rootPath: String, suffix: String, ignoreDirs: Bool = true) -> [String]? { | |
var result = [String]() | |
let fileManager = FileManager.default | |
if let paths = fileManager.subpaths(atPath: rootPath) { | |
let swiftPaths = paths.filter { $0.hasSuffix(suffix) } | |
for path in swiftPaths { | |
var isDir : ObjCBool = false | |
let fullPath = (rootPath as NSString).appendingPathComponent(path) | |
if fileManager.fileExists(atPath: fullPath, isDirectory: &isDir) { | |
if ignoreDirs == false || (ignoreDirs && isDir.boolValue == false) { | |
result.append(fullPath) | |
} | |
} | |
} | |
} | |
return result.count > 0 ? result : nil | |
} | |
var files = findFiles(rootPath: "./", suffix: ".swift") ?? [] | |
func open(_ file: String) -> String { | |
return try! String(contentsOfFile: file, encoding: .utf8) | |
} | |
func save(_ result: String, _ file: String) { | |
try? result.write(toFile: file, atomically: false, encoding: .utf8) | |
} | |
for file in files { | |
var lines = Array(open(file).components(separatedBy: "\n").reversed()) | |
while lines.last?.hasPrefix("//") == true || lines.last == "" { | |
_ = lines.popLast() | |
} | |
let result = lines.reversed().joined(separator: "\n") | |
save(result, file) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment