Skip to content

Instantly share code, notes, and snippets.

@artemnovichkov
Created April 18, 2018 07:55
Show Gist options
  • Save artemnovichkov/7b9fe0497606b2317a15dde73f12f081 to your computer and use it in GitHub Desktop.
Save artemnovichkov/7b9fe0497606b2317a15dde73f12f081 to your computer and use it in GitHub Desktop.
Code example for the article "Swift, Plist and Two Smoking Scripts" https://medium.com/rosberryapps/swift-plist-and-two-smoking-scripts-94bb54cbeded
import Foundation
// Get project URL
guard let projectDirRawValue = getenv("PROJECT_DIR"),
let projectDirectoryPath = String(utf8String: projectDirRawValue) else {
exit(1)
}
let projectURL = URL(fileURLWithPath: projectDirectoryPath)
// Get Info.plist path
guard let productSettingsPathRawValue = getenv("PRODUCT_SETTINGS_PATH"),
let productSettingsPath = String(utf8String: productSettingsPathRawValue) else {
exit(1)
}
guard let enumerator = FileManager.default.enumerator(at: projectURL, includingPropertiesForKeys: nil) else {
exit(1)
}
var swiftFileURLs = enumerator.allObjects
.compactMap { $0 as? URL }
.filter { $0.pathExtension == "swift" }
var keys: [String] = []
for url in swiftFileURLs {
let contents = try String(contentsOf: url, encoding: .utf8)
for (pattern, key) in patterns {
if contents.contains(pattern) {
keys.append(key)
}
}
}
guard let productSettings = NSDictionary(contentsOfFile: productSettingsPath) else {
exit(1)
}
for key in keys {
guard let value = productSettings[key] as? String else {
// Missing key
exit(1)
}
if value.isEmpty {
// Empty description
exit(1)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment