Skip to content

Instantly share code, notes, and snippets.

@yycking
Last active June 11, 2019 09:01
Show Gist options
  • Select an option

  • Save yycking/e5247fdfd9094cae76744c68748b2920 to your computer and use it in GitHub Desktop.

Select an option

Save yycking/e5247fdfd9094cae76744c68748b2920 to your computer and use it in GitHub Desktop.
Dynamic change app version on LaunchScreen.storyboard or localizable storyboard when precompile
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)
}
}
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)
  • build phases
  • run script sh: /usr/bin/env xcrun --sdk macosx swift
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment