|
1 | 1 | import Foundation
|
2 | 2 |
|
3 |
| -struct BuildSettingsLiveContainer: BuildSettingsContainer { |
4 |
| - static let shared = BuildSettingsLiveContainer() |
| 3 | +extension BuildSettings { |
| 4 | + static let live = BuildSettings(bundle: .app) |
5 | 5 |
|
6 |
| - var pushNotificationAppID: String { |
7 |
| - infoPlistValue(forKey: "WPPushNotificationAppID") |
8 |
| - } |
9 |
| - |
10 |
| - var appGroupName: String { |
11 |
| - infoPlistValue(forKey: "WPAppGroupName") |
| 6 | + init(bundle: Bundle) { |
| 7 | + self.pushNotificationAppID = bundle.infoPlistValue(forKey: "WPPushNotificationAppID") |
| 8 | + self.appGroupName = bundle.infoPlistValue(forKey: "WPAppGroupName") |
| 9 | + self.appKeychainAccessGroup = bundle.infoPlistValue(forKey: "WPAppKeychainAccessGroup") |
12 | 10 | }
|
| 11 | +} |
13 | 12 |
|
14 |
| - var appKeychainAccessGroup: String { |
15 |
| - infoPlistValue(forKey: "WPAppKeychainAccessGroup") |
| 13 | +private extension Bundle { |
| 14 | + func infoPlistValue<T>(forKey key: String) -> T where T: LosslessStringConvertible { |
| 15 | + guard let object = object(forInfoDictionaryKey: key) else { |
| 16 | + fatalError("missing value for key: \(key)") |
| 17 | + } |
| 18 | + switch object { |
| 19 | + case let value as T: |
| 20 | + return value |
| 21 | + case let string as String: |
| 22 | + guard let value = T(string) else { fallthrough } |
| 23 | + return value |
| 24 | + default: |
| 25 | + fatalError("unexpected value: \(object) for key: \(key)") |
| 26 | + } |
16 | 27 | }
|
17 | 28 | }
|
18 | 29 |
|
19 |
| -private func infoPlistValue<T>(forKey key: String) -> T where T: LosslessStringConvertible { |
20 |
| - guard let object = Bundle.app.object(forInfoDictionaryKey: key) else { |
21 |
| - fatalError("missing value for key: \(key)") |
22 |
| - } |
23 |
| - switch object { |
24 |
| - case let value as T: |
25 |
| - return value |
26 |
| - case let string as String: |
27 |
| - guard let value = T(string) else { fallthrough } |
28 |
| - return value |
29 |
| - default: |
30 |
| - fatalError("unexpected value: \(object) for key: \(key)") |
31 |
| - } |
| 30 | +private extension Bundle { |
| 31 | + /// Returns the `Bundle` for the host `.app`. |
| 32 | + /// |
| 33 | + /// - If this is called from code already located in the main app's bundle or from a Pod/Framework, |
| 34 | + /// this will return the same as `Bundle.main`, aka the bundle of the app itself. |
| 35 | + /// - If this is called from an App Extension (Widget, ShareExtension, etc), this will return the bundle of the |
| 36 | + /// main app hosting said App Extension (while `Bundle.main` would return the App Extension itself) |
| 37 | + static let app: Bundle = { |
| 38 | + var url = Bundle.main.bundleURL |
| 39 | + while url.pathExtension != "app" && url.lastPathComponent != "/" { |
| 40 | + url.deleteLastPathComponent() |
| 41 | + } |
| 42 | + guard let appBundle = Bundle(url: url) else { fatalError("Unable to find the parent app bundle") } |
| 43 | + return appBundle |
| 44 | + }() |
32 | 45 | }
|
0 commit comments