- build phases
- run script sh: /usr/bin/env xcrun --sdk macosx swift
Last active
June 11, 2019 09:01
-
-
Save yycking/e5247fdfd9094cae76744c68748b2920 to your computer and use it in GitHub Desktop.
Dynamic change app version on LaunchScreen.storyboard or localizable storyboard when precompile
This file contains hidden or 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 | |
| guard let SRCROOT = ProcessInfo.processInfo.environment["SRCROOT"] else { | |
| fputs("not define SRCROOT\n", stderr) | |
| exit(-1) | |
| } | |
| guard let PROJECT_NAME = ProcessInfo.processInfo.environment["PROJECT_NAME"] else { | |
| fputs("not define PROJECT_NAME\n", stderr) | |
| exit(-1) | |
| } | |
| let projectDir = "\(SRCROOT)/\(PROJECT_NAME)" | |
| guard | |
| let en = NSDictionary(contentsOfFile: "\(projectDir)/en.lproj/Localizable.strings"), | |
| var storyoards = try? FileManager.default.contentsOfDirectory(atPath: "\(projectDir)/en.lproj") | |
| else { | |
| exit(-1) | |
| } | |
| storyoards = storyoards.filter { (file) -> Bool in | |
| return file.hasSuffix(".strings") | |
| }.filter { (file) -> Bool in | |
| return !file.hasPrefix("Localizable.") | |
| } | |
| guard var languages = try? FileManager.default.contentsOfDirectory(atPath: "\(projectDir)") | |
| else { | |
| exit(-1) | |
| } | |
| languages = languages.filter { (file) -> Bool in | |
| return file.hasSuffix(".lproj") | |
| }.filter { (file) -> Bool in | |
| return !file.hasPrefix("en.") && !file.hasPrefix("Base.") | |
| } | |
| for language in languages { | |
| guard | |
| let localizable = NSDictionary(contentsOfFile: "\(projectDir)/\(language)/Localizable.strings") else { | |
| continue | |
| } | |
| for storyoard in storyoards { | |
| let enXibPath = "\(projectDir)/en.lproj/\(storyoard)" | |
| guard | |
| var text = try? NSString(contentsOfFile: enXibPath, encoding: String.Encoding.utf8.rawValue), | |
| let xib = NSDictionary(contentsOfFile: enXibPath) else { | |
| continue | |
| } | |
| for (_, value) in xib { | |
| guard | |
| let value = value as? String, | |
| let locKey = en.allKeys(for: value).first as? String, | |
| let locValue = localizable.value(forKey: locKey) as? String | |
| else { | |
| continue | |
| } | |
| text = text.replacingOccurrences(of: "\"\(value)\"", with: "\"\(locValue)\"") as NSString | |
| } | |
| let newFile = "\(projectDir)/\(language)/\(storyoard)" | |
| try? FileManager.default.removeItem(atPath: newFile) | |
| try? text.write(toFile: newFile, atomically: false, encoding: String.Encoding.utf8.rawValue) | |
| } | |
| } |
This file contains hidden or 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 | |
| guard let SRCROOT = ProcessInfo.processInfo.environment["SRCROOT"] else { | |
| fputs("not define SRCROOT\n", stderr) | |
| exit(-1) | |
| } | |
| guard let PROJECT_NAME = ProcessInfo.processInfo.environment["PROJECT_NAME"] else { | |
| fputs("not define PROJECT_NAME\n", stderr) | |
| exit(-1) | |
| } | |
| let infoFile = "\(SRCROOT)/\(PROJECT_NAME)/info.plist" | |
| guard let info = NSDictionary(contentsOfFile: infoFile) else { | |
| fputs("info.plist not found\n", stderr) | |
| exit(-1) | |
| } | |
| guard let version = info["CFBundleShortVersionString"] else { | |
| fputs("version found\n", stderr) | |
| exit(-1) | |
| } | |
| let storyBoardFile = "\(SRCROOT)/\(PROJECT_NAME)/Base.lproj/LaunchScreen.storyboard" | |
| guard var storyBoard = try? String(contentsOfFile: storyBoardFile, encoding: .utf8) else { | |
| fputs("cannot load storyBoard\n", stderr) | |
| exit(-1) | |
| } | |
| guard let regex = try? NSRegularExpression(pattern: "text=\"Ver\\. [0-9]\\.[0-9]\\.[0-9]\"") else { | |
| fputs("Regular Expression fail type\n", stderr) | |
| exit(-1) | |
| } | |
| let totalRange = NSRange(location: 0, length: storyBoard.count) | |
| let versionRange = regex.rangeOfFirstMatch(in: storyBoard, options: [], range:totalRange) | |
| guard let range = Range(versionRange, in: storyBoard) else { | |
| fputs("Regular Expression not found\n", stderr) | |
| exit(-1) | |
| } | |
| storyBoard.replaceSubrange(range, with: "text=\"Ver. \(version)\"") | |
| try? storyBoard.write(toFile: storyBoardFile, atomically: false, encoding: .utf8) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment