BuildConfig.swift
BuildConfig.swift copied to clipboard
Android-like auto-generate configuration files for macOS/iOS
BuildConfig.swift
BuildConfig.swift is a tool to generate configuration files by merging yamls or jsons.
By splitting the file for each type of setting, it is possible to prevent conflicts of configuration files.
Also, by splitting the file for environment configurations, it will be easier to overwrite configurations for each environment.
Example
Base JSON file
{
"API": {
"domain": "http://localhost",
"path": {
"login": {
"method": "POST",
"path": "/login"
},
"getList": {
"method": "GET",
"path": "/list"
}
}
}
}
Call above configuration
Vanilla
let file = Bundle.main.path(forResource: "Base", ofType: "json")!
let data = try! Data(contentsOf: URL(fileURLWithPath: filePath))
let config = try! JSONSerialization.jsonObject(with: data, options: []) as! [String: Any]
let api = config["API"] as! [String: Any]
let domain = api.domain as! String // "http://localhost"
let loginPath = (api.path as! [String: Any])["login"] as! [String: Any]
let path = loginPath.path // "/login"
let method = loginPath.method // "POST"
Using BuildConfig.swift
let config = BuildConfig.default
let domain = config.API.domain // "http://localhost"
let path = config.API.path.login.path // "/login"
let method = config.API.path.login.method // "POST"
Installation
Common
- Create directory for splitted configuration files, e.g.
$PROJECT/Resources/Config. - If you use different settings for each environment, create
.envinto above directory. - You don't have to add above directory into project.
CocoaPods
- Add the following line to your test target in your Podfile:
pod 'BuildConfig.swift'
- Add the following
Run scriptbuild phase to your test target'sBuild Phases:
if [ "${CONFIGURATION}" = 'Release' ]; then
ENVIRONMENT='production'
else
ENVIRONMENT='staging'
fi
"${PODS_ROOT}/BuildConfig.swift/buildconfigswift" -e $ENVIRONMENT "$SRCROOT/$PROJECT/Resources/Config"
You can replace "$SRCROOT/$PROJECT/Resources/Config" to the relative path from project to the directory you created.
Also, you can add -o option with output path to specify where BuildConfig.plist and BuildConfig.generated.swift will be created.
-
Add
$(TEMP_DIR)/buildconfigswift-lastrunintoInput Filesin aboveRun scriptbuild phase. -
Add
$(SRCROOT)/BuildConfig.plistand$(SRCROOT)/BuildConfig.generated.swiftintoOutput Filesin aboveRun scriptbuild phase.- If you set a path to output generated files by
-ooption, you have to changeOutput Filesto those paths.
- If you set a path to output generated files by
-
Drag the new
Run Scriptphase above theCompile Sourcesphase and belowCheck Pods Manifest.lock
If you are using R.swift, drag the newRun Scriptabove theRun Scriptphase for R.swift and you can load withR.file.configPlist. -
Build your project, in Finder you will now see a
BuildConfig.plistandBuildConfig.generated.swiftin$SRCROOTor a path you set with-ooption in aboveRun scriptbuild phase. -
Drag them into your project.
Tip: Add the BuildConfig.plist pattern and the *.generated.swift pattern to your .gitignore file to prevent unnecessary conflicts.
Manually
TODO: Future support.
What is BuildConfig.swift doing?
- Detect all yml/json files in
$SRCROOT/$PROJECT/Resources/Config, exclude.env. - If the
-eoption is set and a file with the same name as that option exists in$SRCROOT/$PROJECT/Resources/Config/.env, only that file is read.
For example,-e stagingoption means to read$SRCROOT/$PROJECT/Resources/Config/.env/staging.{yml/yaml/json}. - Parse above files as
Swift.Dictionary. - Deep merge the above dictionaries.
- Output merged dictionary as a plist file.
Libraries
License
Available under the MIT License.