Created
April 6, 2017 06:25
-
-
Save heriwijoyo/e030e278e87a3fcd2affc45eb3dd26af to your computer and use it in GitHub Desktop.
Swift Script to Fixing Inconvenience Indentation
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
#!/usr/bin/env xcrun swift | |
import Foundation | |
func isFolderPath(pathURL: NSURL) -> Bool { | |
var isDir: ObjCBool = false | |
guard let path = pathURL.path else { | |
return false | |
} | |
if NSFileManager.defaultManager().fileExistsAtPath(path, isDirectory: &isDir) { | |
return isDir ? true : false | |
} | |
return false | |
} | |
func removeWhiteSpaceAt(fileURL: NSURL) { | |
do { | |
let beforeColonRegex = try NSRegularExpression(pattern: "(\\s{2,}:)", options: .CaseInsensitive) | |
let afterColonRegex = try NSRegularExpression(pattern: "(:([^\\n]|:\\s{2,}))\\s{2,}", options: .CaseInsensitive) | |
let equalRegex = try NSRegularExpression(pattern: "(\\s{2,}=\\s*)", options: .CaseInsensitive) | |
let mapperRegex = try NSRegularExpression(pattern: "\\s*<-\\s*", options: .CaseInsensitive) | |
let originalText = try String(contentsOfURL: fileURL) | |
var trimmedText = beforeColonRegex.stringByReplacingMatchesInString(originalText, options: .ReportCompletion, range: NSMakeRange(0, originalText.characters.count), withTemplate: ":") | |
trimmedText = afterColonRegex.stringByReplacingMatchesInString(trimmedText, options: .ReportCompletion, range: NSMakeRange(0, trimmedText.characters.count), withTemplate: ": ") | |
trimmedText = equalRegex.stringByReplacingMatchesInString(trimmedText, options: .ReportCompletion, range: NSMakeRange(0, trimmedText.characters.count), withTemplate: " = ") | |
trimmedText = mapperRegex.stringByReplacingMatchesInString(trimmedText, options: .ReportCompletion, range: NSMakeRange(0, trimmedText.characters.count), withTemplate: " <- ") | |
try trimmedText.writeToURL(fileURL, atomically: true, encoding: NSUTF8StringEncoding) | |
} catch { | |
print(error) | |
} | |
} | |
func scanSwiftFileAtURL(pathURL: NSURL) { | |
let fileManager = NSFileManager.defaultManager() | |
do { | |
let contents = try fileManager.contentsOfDirectoryAtURL(pathURL, includingPropertiesForKeys: nil, options: []) | |
let swiftFiles = contents.filter { $0.pathExtension == "swift" } | |
let _ = swiftFiles.map { removeWhiteSpaceAt($0) } | |
for content in contents { | |
let isDir = isFolderPath(content) | |
if isDir { | |
scanSwiftFileAtURL(content) | |
} | |
} | |
} | |
catch let error as NSError { | |
print(error.localizedDescription) | |
} | |
} | |
let scanPath = Process.arguments.count > 1 ? "/\(Process.arguments[1])" : "" | |
let fileManager = NSFileManager.defaultManager() | |
let currentDir = fileManager.currentDirectoryPath | |
let absoluteScanPath = "\(currentDir)\(scanPath)" | |
if let scanURL = NSURL(string: absoluteScanPath) { | |
scanSwiftFileAtURL(scanURL) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment