Skip to content

Instantly share code, notes, and snippets.

@rockbruno
Last active September 8, 2021 09:26
Show Gist options
  • Save rockbruno/d59f4ef5eadfe2e215959a52e452008d to your computer and use it in GitHub Desktop.
Save rockbruno/d59f4ef5eadfe2e215959a52e452008d to your computer and use it in GitHub Desktop.
iOS File Header remover script
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)
}
@adamdahan
Copy link

nice

@bocato
Copy link

bocato commented Oct 3, 2019

Nice! I’ll use that, thanks :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment