Skip to content

Instantly share code, notes, and snippets.

@tadija
Last active May 1, 2021 23:40
Show Gist options
  • Save tadija/b5220447b71468937702d1162b5f2591 to your computer and use it in GitHub Desktop.
Save tadija/b5220447b71468937702d1162b5f2591 to your computer and use it in GitHub Desktop.
AEEnvironment
/**
* https://gist.github.com/tadija/b5220447b71468937702d1162b5f2591
* Revision 8
* Copyright © 2017-2021 Marko Tadić
* Licensed under the MIT license
*/
import Foundation
public protocol AEEnvironment {
associatedtype Mode
associatedtype Configuration
}
public extension AEEnvironment where
Mode: CaseIterable & RawRepresentable,
Mode.RawValue == String,
Configuration: CaseIterable & RawRepresentable,
Configuration.RawValue == String
{
// MARK: Build Configuration
static var buildConfiguration: String {
guard let bc = settings["Configuration"] as? String else {
fatalError("{ Configuration : $(CONFIGURATION) } - not found in `Settings`")
}
return bc
}
static var mode: Mode? {
parseBuildConfiguration()
}
static var configuration: Configuration? {
parseBuildConfiguration()
}
// MARK: Helpers
private static func parseBuildConfiguration<T: CaseIterable & RawRepresentable>() -> T?
where T.RawValue == String
{
T.self.allCases.first {
buildConfiguration.lowercased()
.contains($0.rawValue.lowercased())
}
}
}
public extension AEEnvironment {
// MARK: Bundle Settings
static var settings: NSDictionary {
guard let dict = bundleObject(forKey: "Settings") as? NSDictionary else {
fatalError("`Settings` Dictionary not found in `Info.plist`")
}
return dict
}
// MARK: Build Information
static var bundleID: String {
bundleString(forKey: kCFBundleIdentifierKey as String)
}
static var bundleName: String {
bundleString(forKey: kCFBundleNameKey as String)
}
static var bundleVersion: String {
bundleString(forKey: "CFBundleShortVersionString")
}
static var bundleBuild: String {
bundleString(forKey: kCFBundleVersionKey as String)
}
static var bundleVersionBuild: String {
"Version \(bundleVersion) (build: \(bundleBuild))"
}
static var bundleVersionBuildCompact: String {
"\(bundleVersion) (\(bundleBuild))"
}
// MARK: Helpers
static func bundleObject(forKey key: String) -> Any? {
Bundle.main.object(forInfoDictionaryKey: key)
}
static func bundleString(forKey key: String) -> String {
bundleObject(forKey: key) as? String ?? "n/a"
}
}
open class DPEnvironment: AEEnvironment {
// MARK: Mode
public enum Mode: String, CaseIterable {
case debug, release
}
public static var isDebug: Bool {
mode == .debug
}
public static var isRelease: Bool {
mode == .release
}
// MARK: Configuration
public enum Configuration: String, CaseIterable {
case develop, production
}
public static var isDevelop: Bool {
configuration == .develop
}
public static var isProduction: Bool {
configuration == .production
}
}
open class DSPEnvironment: AEEnvironment {
// MARK: Mode
public enum Mode: String, CaseIterable {
case debug, release
}
public static var isDebug: Bool {
mode == .debug
}
public static var isRelease: Bool {
mode == .release
}
// MARK: Configuration
public enum Configuration: String, CaseIterable {
case develop, stage, production
}
public static var isDevelop: Bool {
configuration == .develop
}
public static var isStage: Bool {
configuration == .stage
}
public static var isProduction: Bool {
configuration == .production
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment