Skip to content

Configen

Neil Horton edited this page Mar 23, 2016 · 2 revisions

The configen script requires the following arguments:

  • The path to the property list to take settings from
  • The path to the mapping file, as described above
  • The name of the class to create
  • The path where the implementation file should be created

It's a very simple, immature Swift script that just takes a list of arguments in order :)

1. Create a wrapper script

So it's best to define a wrapper script, named something like build-config.sh, which contains the following, for example:

env=$1
./configen ExampleApp/AppEnvironment-$env.plist AppEnvironment.map AppEnvironment ExampleApp

This script takes the name of the environment (e.g. "STAGING") as its first argument and executes the configen script. It specifies the name of the class and the paths from which to load properties and write the Swift output file. The input argument is used as a suffix to the property list filename.

The build-config.sh script should be run as part of the build. We define a different Scheme per environment, but we only have two configurations, the default "Debug" and "Release". All of our QA builds and our App Store builds use the "Release" configuration, so we can guarantee what we test is what we release, in terms of Build Settings.

2. Create an External Build System target

For each environemnt, we create an "External Build System" target (File > New > Target... > Other > External Build System). Name it according to the environment, e.g. "STAGING" and be sure to associate it to the correct project.

In the Project Navigator, select your new External Build System target. And navigate to the "Info" tab in the Standard Editor. Set the Build Tool to ./build-config.sh and set the Arguments to the name of the environment to build, e.g. "STAGING" (i.e. the suffix of the property list file to load).

3. Create separate Property List files

For each environment, create a property list, and name it according to the input that you defined to the configen script, e.g. "AppEnvironment-TEST.plist" and "AppEnvironment-STAGING.plist". Place them at the path you specified in the build-config.sh script.

4. Setup the Scheme

Create a scheme for each environment. Edit the scheme and go to the Build settings in the panel on the left. In the panel on the right, click the + button to add a new target to the build. From the list of targets, select the External Build System target that you defined in step 2. For example, if you editing the "TEST" scheme, add the "TEST" target.

After adding the target, drag it up to the top of the list of targets, so that it builds before the actual project target. Ensure all the checkboxes are ticked.

5. Build and test

Build your scheme and you should see that the environment class file is created. The first time you create this file, you'll need to add it to the project. Those that have used mogenerator will be familiar with this. It's a one time task. And thereafter, whenever you build your project, this file will be updated for the scheme you're building.

6. Accessing from code

It's so, so simple to access these settings from your app's codebase. They're all static properties of whatever class you specified to create. For example, I could access my analytics key as follows:

let tracker = AnalyticsTrackerWithID(AppEnvironment.analyticsKey)

And you don't need to write any parsing yourself, you just update the properties file and it's right there as a static property. Gone are the days of re-writing code to load and read from a flat file or property list!

Home

Clone this wiki locally