Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions Sources/FuturedHelpers/Helpers/ConfigKey.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import Foundation

/// Protocol for configuration keys that provides default implementation for retrieving values from Info.plist
/// with automatic Base64 decoding support
public protocol ConfigKey: RawRepresentable where RawValue == String {
/// Default implementation that retrieves and decodes values from Info.plist
var value: String { get throws }
}

public extension ConfigKey {
/// Default implementation that reads from Info.plist and handles Base64 decoding
var value: String {
get throws {
guard let rawValue = Bundle.main.object(forInfoDictionaryKey: rawValue) as? String else {
throw ConfigKeyError.valueNotFound(key: rawValue)
}

// Try to decode as Base64, fallback to original value if not encoded
guard let data = Data(base64Encoded: rawValue),
let decodedString = String(data: data, encoding: .utf8) else {
// Not valid Base64, return original value
return rawValue
}
return decodedString
}
}
}

/// Error type for configuration key handling
public enum ConfigKeyError: LocalizedError {
case valueNotFound(key: String)

public var errorDescription: String? {
switch self {
case let .valueNotFound(key):
"Value for \"\(key)\" was not found in .xcconfig file. Check if the value with this key is imported from GitHub secrets to .xcconfig file and whether it is specified in Info.plist."
}
}
}
12 changes: 12 additions & 0 deletions Templates/SwiftUI App.xctemplate/Beta.xcconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// Beta configuration

// Do not add any key-value pairs here, they should be injected via CI

// NOTE: Each key added here must also be added to Info.plist as:
// <key>KEY_NAME</key>
// <string>$(KEY_NAME)</string>
// Then access it in code using the ConfigKey protocol from FuturedHelpers:
// enum AppConfigKey: String, ConfigKey {
// case apiKey = "API_KEY"
// }
// let value = try AppConfigKey.apiKey.value
18 changes: 18 additions & 0 deletions Templates/SwiftUI App.xctemplate/Debug.xcconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Debug configuration
#include? "Local.xcconfig"

// TODO: Add API configuration
// API_KEY = <specify in Local.xcconfig so the key will not be part of git history>
// API_BASE_URL =

// TODO: Add Swift compilation flags
// OTHER_SWIFT_FLAGS = $(inherited) -D LOG_ENABLED

// IMPORTANT: Each key added must also be added to Info.plist as:
// <key>KEY_NAME</key>
// <string>$(KEY_NAME)</string>
// Then access it in code using the ConfigKey protocol from FuturedHelpers:
// enum AppConfigKey: String, ConfigKey {
// case apiKey = "API_KEY"
// }
// let value = try AppConfigKey.apiKey.value
14 changes: 14 additions & 0 deletions Templates/SwiftUI App.xctemplate/Local.xcconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// Local configuration (not tracked by git)
// Add your local overrides and secrets here

// API_KEY =
// OTHER_SWIFT_FLAGS = $(inherited) -D LOG_ENABLED

// NOTE: Each key added here must also be added to Info.plist as:
// <key>KEY_NAME</key>
// <string>$(KEY_NAME)</string>
// Then access it in code using the ConfigKey protocol from FuturedHelpers:
// enum AppConfigKey: String, ConfigKey {
// case apiKey = "API_KEY"
// }
// let value = try AppConfigKey.apiKey.value
3 changes: 3 additions & 0 deletions Templates/SwiftUI App.xctemplate/Release.xcconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// Release configuration

// Do not add any key-value pairs here, they should be injected via CI
53 changes: 53 additions & 0 deletions Templates/SwiftUI App.xctemplate/TemplateInfo.plist
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,15 @@
<dict>
<key>Configurations</key>
<dict>
<key>Debug</key>
<dict>
<key>baseConfigurationReferenceRelativePath</key>
<string>Configuration/Debug.xcconfig</string>
</dict>
<key>Beta</key>
<dict>
<key>baseConfigurationReferenceRelativePath</key>
<string>Configuration/Beta.xcconfig</string>
<key>ENABLE_NS_ASSERTIONS</key>
<string>NO</string>
<key>MTL_ENABLE_DEBUG_INFO</key>
Expand All @@ -74,6 +81,11 @@
<key>VALIDATE_PRODUCT</key>
<string>YES</string>
</dict>
<key>Release</key>
<dict>
<key>baseConfigurationReferenceRelativePath</key>
<string>Configuration/Release.xcconfig</string>
</dict>
</dict>
</dict>
<key>Targets</key>
Expand Down Expand Up @@ -122,6 +134,10 @@
<key>Nodes</key>
<array>
<string>Resources/Assets.xcassets</string>
<string>Configuration/Debug.xcconfig</string>
<string>Configuration/Beta.xcconfig</string>
<string>Configuration/Release.xcconfig</string>
<string>Configuration/Local.xcconfig</string>
<string>___PACKAGENAME:identifier___App.swift</string>
<string>AppCoordinator.swift</string>
<string>AppDelegate.swift</string>
Expand Down Expand Up @@ -159,6 +175,43 @@
<integer>100</integer>
</dict>

<key>Configuration/Debug.xcconfig</key>
<dict>
<key>Path</key>
<string>Debug.xcconfig</string>
<key>Group</key>
<string>Configuration</string>
<key>SortOrder</key>
<integer>98</integer>
</dict>
<key>Configuration/Beta.xcconfig</key>
<dict>
<key>Path</key>
<string>Beta.xcconfig</string>
<key>Group</key>
<string>Configuration</string>
<key>SortOrder</key>
<integer>98</integer>
</dict>
<key>Configuration/Release.xcconfig</key>
<dict>
<key>Path</key>
<string>Release.xcconfig</string>
<key>Group</key>
<string>Configuration</string>
<key>SortOrder</key>
<integer>98</integer>
</dict>
<key>Configuration/Local.xcconfig</key>
<dict>
<key>Path</key>
<string>Local.xcconfig</string>
<key>Group</key>
<string>Configuration</string>
<key>SortOrder</key>
<integer>98</integer>
</dict>

<key>___PACKAGENAME:identifier___App.swift</key>
<dict>
<key>Path</key>
Expand Down