Last active
March 25, 2019 22:13
-
-
Save apouche/2bba1fb5f9ffdbd44e10a0cfb7fd8627 to your computer and use it in GitHub Desktop.
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/swift | |
// | |
// rifier.swift | |
// rifier | |
// | |
// Created by Johan on 2019-03-25. | |
// Copyright © 2019 Johan Attali. All rights reserved. | |
// | |
import Foundation | |
extension Array where Element : Hashable { | |
func unique() -> [Element] { | |
return Array(Set(self)) | |
} | |
} | |
private struct Regex { | |
var pattern: String { | |
didSet { | |
updateRegex() | |
} | |
} | |
var expressionOptions: NSRegularExpression.Options { | |
didSet { | |
updateRegex() | |
} | |
} | |
var matchingOptions: NSRegularExpression.MatchingOptions | |
var regex: NSRegularExpression? | |
init(pattern: String, expressionOptions: NSRegularExpression.Options, matchingOptions: NSRegularExpression.MatchingOptions) { | |
self.pattern = pattern | |
self.expressionOptions = expressionOptions | |
self.matchingOptions = matchingOptions | |
updateRegex() | |
} | |
init(pattern: String) { | |
self.pattern = pattern | |
expressionOptions = NSRegularExpression.Options.caseInsensitive | |
matchingOptions = NSRegularExpression.MatchingOptions() | |
updateRegex() | |
} | |
mutating func updateRegex() { | |
do { regex = try NSRegularExpression(pattern: pattern, options: expressionOptions) } | |
catch _ { } | |
} | |
} | |
// MARK: - Private Methods | |
private extension String { | |
func matchRegex(_ pattern: Regex) -> [NSTextCheckingResult]? { | |
guard let regex = pattern.regex else { return nil } | |
let range: NSRange = NSMakeRange(0, self.count) | |
return regex.matches(in: self, options: pattern.matchingOptions, range: range) | |
} | |
func replaceRegex(_ pattern: Regex, template: String, take:UInt = UInt.max) -> String { | |
guard let regex = pattern.regex else { return self } | |
var n = take | |
var str = self | |
while let matches = str.matchRegex(pattern), let match = matches.first { | |
guard n > 0 else { break } | |
str = regex.stringByReplacingMatches(in: str, | |
options: pattern.matchingOptions, | |
range: match.range, | |
withTemplate: template) | |
n = n - 1 | |
} | |
return str | |
} | |
} | |
// MARK: - Public Methods | |
extension String { | |
func matches(_ pattern: String) -> Bool { | |
return matchRegex(Regex(pattern: pattern))?.count ?? 0 > 0 | |
} | |
func replace(_ pattern: String, template: String, take:UInt = UInt.max) -> String { | |
return replaceRegex(Regex(pattern: pattern), template: template, take: take) | |
} | |
func substring(pattern: String) -> String? { | |
guard let range = self.rangeOfPattern(pattern) else { return nil } | |
let start = self.index(startIndex, offsetBy: range.location) | |
let end = self.index(startIndex, offsetBy: range.location+range.length) | |
return String(self[start..<end]) | |
} | |
func rangeOfPattern(_ patternString: String) -> NSRange? { | |
guard let result = matchRegex(Regex(pattern: patternString)) else { return nil } | |
return result.first?.range | |
} | |
func rangesOfPattern(_ patternString: String) -> [NSRange]? { | |
guard let result = matchRegex(Regex(pattern: patternString)) else { return nil } | |
return result.map({$0.range}) | |
} | |
} | |
func main() { | |
if (CommandLine.arguments.count < 2) { | |
fatalError("You must provide the images folder path") | |
} | |
let path = NSString(string: CommandLine.arguments[1]).expandingTildeInPath | |
var isDir: ObjCBool = false | |
FileManager.default.fileExists(atPath: path, isDirectory: &isDir) | |
if (!isDir.boolValue) { | |
fatalError("\(path) is not a directory") | |
} | |
do { | |
let output = NSMutableString() | |
var tabs: [String] = [] | |
let files:[String] = try FileManager.default.contentsOfDirectory(atPath: path) | |
output.append("import UIKit") | |
output.append("\n") | |
output.append("\n") | |
output.append("final class R {}") | |
output.append("\n") | |
output.append("\n") | |
output.append("extension R {") | |
output.append("\n") | |
tabs.append("\t") | |
output.append("\(tabs.joined())class image {") | |
output.append("\n") | |
tabs.append("\t") | |
let write: () -> () = { | |
files | |
.filter({ $0.matches("\\.png$") }) | |
.map({ $0.replace("(@\\w{2})?\\.png$", template: "") }) | |
.unique() | |
.sorted() | |
.forEach({ file in | |
let renamed = file.replace("[^\\w]", template: "_").lowercased() | |
output.append("\(tabs.joined())static var \(renamed): UIImage = UIImage(named: \"\(file)\")") | |
output.append("\n") | |
}) | |
} | |
write() | |
tabs.removeLast() | |
output.append("\(tabs.joined())}") | |
output.append("\n") | |
tabs.removeLast() | |
output.append("\(tabs.joined())}") | |
output.append("\n") | |
output.append("extension UIImage {") | |
output.append("\n") | |
tabs.append("\t") | |
write() | |
tabs.removeLast() | |
output.append("\(tabs.joined())}") | |
output.append("\n") | |
print(output) | |
} catch (_) { | |
fatalError("couldn't list files in \(path)") | |
} | |
} | |
main() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment