Skip to content

Commit

Permalink
Genstrings swift 3 update
Browse files Browse the repository at this point in the history
  • Loading branch information
marmelroy committed Sep 18, 2016
1 parent 274d7bd commit 65a1209
Showing 1 changed file with 48 additions and 45 deletions.
93 changes: 48 additions & 45 deletions genstrings.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,51 +3,54 @@
import Foundation

class GenStrings {

let fileManager = NSFileManager.defaultManager()

var str = "Hello, playground"
let fileManager = FileManager.default
let acceptedFileExtensions = ["swift"]
let excludedFolderNames = ["Carthage"]
let excludedFileNames = ["genstrings.swift"]
var regularExpresions = [String:NSRegularExpression]()

let localizedRegex = "(?<=\")([^\"]*)(?=\".(localized|localizedFormat))|(?<=(Localized|NSLocalizedString)\\(\")([^\"]*?)(?=\")"

enum GenstringsError:ErrorType {
enum GenstringsError: Error {
case Error
}

// Performs the genstrings functionality
func perform() {
let rootPath = NSURL(fileURLWithPath:fileManager.currentDirectoryPath)
let allFiles = fetchFilesInFolder(rootPath)
let rootPath = URL(fileURLWithPath:fileManager.currentDirectoryPath)
let allFiles = fetchFilesInFolder(rootPath: rootPath)
// We use a set to avoid duplicates
var localizableStrings = Set<String>()
for filePath in allFiles {
let stringsInFile = localizableStringsInFile(filePath)
let stringsInFile = localizableStringsInFile(filePath: filePath)
localizableStrings = localizableStrings.union(stringsInFile)
}
// We sort the strings
let sortedStrings = localizableStrings.sort({ $0 < $1 })
let sortedStrings = localizableStrings.sorted(by: { $0 < $1 })
var processedStrings = String()
for string in sortedStrings {
processedStrings.appendContentsOf("\"\(string)\" = \"\(string)\"; \n")
processedStrings.append("\"\(string)\" = \"\(string)\"; \n")
}
print(processedStrings)
}

// Applies regex to a file at filePath.
func localizableStringsInFile(filePath: NSURL) -> Set<String> {
if let fileContentsData = NSData(contentsOfURL: filePath), let fileContentsString = NSString(data: fileContentsData, encoding: NSUTF8StringEncoding) {
do {
let localizedStringsArray = try regexMatches(localizedRegex, string: fileContentsString as String).map({fileContentsString.substringWithRange($0.range)})
return Set(localizedStringsArray)
} catch {}
}

// Applies regex to a file at filePath.
func localizableStringsInFile(filePath: URL) -> Set<String> {
do {
let fileContentsData = try Data(contentsOf: filePath)
guard let fileContentsString = NSString(data: fileContentsData, encoding: String.Encoding.utf8.rawValue) else {
return Set<String>()
}
let localizedStringsArray = try regexMatches(pattern: localizedRegex, string: fileContentsString as String).map({fileContentsString.substring(with: $0.range)})
return Set(localizedStringsArray)
} catch {}
return Set<String>()
}

//MARK: Regex

func regexWithPattern(pattern: String) throws -> NSRegularExpression {
var safeRegex = regularExpresions
if let regex = safeRegex[pattern] {
Expand All @@ -56,61 +59,61 @@ class GenStrings {
else {
do {
let currentPattern: NSRegularExpression
currentPattern = try NSRegularExpression(pattern: pattern, options:NSRegularExpressionOptions.CaseInsensitive)
currentPattern = try NSRegularExpression(pattern: pattern, options:NSRegularExpression.Options.caseInsensitive)
safeRegex.updateValue(currentPattern, forKey: pattern)
self.regularExpresions = safeRegex
regularExpresions = safeRegex
return currentPattern
}
catch {
throw GenstringsError.Error
}
}
}

func regexMatches(pattern: String, string: String) throws -> [NSTextCheckingResult] {
do {
let internalString = string
let currentPattern = try regexWithPattern(pattern)
let currentPattern = try regexWithPattern(pattern: pattern)
// NSRegularExpression accepts Swift strings but works with NSString under the hood. Safer to bridge to NSString for taking range.
let nsString = internalString as NSString
let stringRange = NSMakeRange(0, nsString.length)
let matches = currentPattern.matchesInString(internalString, options: [], range: stringRange)
let matches = currentPattern.matches(in: internalString, options: [], range: stringRange)
return matches
}
catch {
throw GenstringsError.Error
}
}

//MARK: File manager
func fetchFilesInFolder(rootPath: NSURL) -> [NSURL] {
var files = [NSURL]()

func fetchFilesInFolder(rootPath: URL) -> [URL] {
var files = [URL]()
do {
let directoryContents = try fileManager.contentsOfDirectoryAtURL(rootPath, includingPropertiesForKeys: [], options: .SkipsHiddenFiles)
let directoryContents = try fileManager.contentsOfDirectory(at: rootPath as URL, includingPropertiesForKeys: [], options: .skipsHiddenFiles)
for urlPath in directoryContents {
if let stringPath = urlPath.path, lastPathComponent = urlPath.lastPathComponent, pathExtension = urlPath.pathExtension {
var isDir : ObjCBool = false
if fileManager.fileExistsAtPath(stringPath, isDirectory:&isDir) {
if isDir {
if !excludedFolderNames.contains(lastPathComponent) {
let dirFiles = fetchFilesInFolder(urlPath)
files.appendContentsOf(dirFiles)
}
} else {
if acceptedFileExtensions.contains(pathExtension) && !excludedFileNames.contains(lastPathComponent) {
files.append(urlPath)
}
let stringPath = urlPath.path
let lastPathComponent = urlPath.lastPathComponent
let pathExtension = urlPath.pathExtension
var isDir : ObjCBool = false
if fileManager.fileExists(atPath: stringPath, isDirectory:&isDir) {
if isDir.boolValue {
if !excludedFolderNames.contains(lastPathComponent) {
let dirFiles = fetchFilesInFolder(rootPath: urlPath)
files.append(contentsOf: dirFiles)
}
} else {
if acceptedFileExtensions.contains(pathExtension) && !excludedFileNames.contains(lastPathComponent) {
files.append(urlPath)
}
}
}
}
} catch {}
return files
}

}

let genStrings = GenStrings()
genStrings.perform()


genStrings.perform()

0 comments on commit 65a1209

Please sign in to comment.