• Stars
    star
    157
  • Rank 238,399 (Top 5 %)
  • Language
    Swift
  • License
    MIT License
  • Created over 8 years ago
  • Updated about 4 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Configuration file code generator for use in Xcode projects

Configen - Kin + Carta Create

configen

A command line tool to auto-generate configuration file code, for use in Xcode projects. To read about the motivation behind this project and the scenarios in which it might be used see our blog post.

The configen tool is used to auto-generate configuration code from a property list. It is intended to create the kind of configuration needed for external URLs or API keys used by your app. Currently supports both Swift and Objective-C code generation.

Installation

To add the configen tool to your project you must first aquire the configen excecutable binary. The simplest way to do this is to download the executable binary from the latest release.

Alternatively you can download or clone this repository. Once you have done so, open and build the configen.xcodeproj project in Xcode, right click on the configen product and select โ€˜Show in Finderโ€™.

Once you have the executable file, make a copy and add it to the root directory of your project. Now you are ready to go! Next you need to create the relevant files and set-up your project accordingly. This is outlined below.

Usage

Step 1: The mapping file

Before running the configen tool, you need to create a mapping file (.map), in which you define the configuration variables you support. For example:

entryPointURL : URL
enableFileSharing : Bool
retryCount : Int
adUnitPrefix : String
analyticsKey : String
arrayOfHashes: [String]
environment : Environment

The configen mapping file uses a custom set of types which map to Swift types. Therefore for Objective-C code generation, you must still use Swift equivalent types in the mapping file.

Step 2: A plist for each environment

Then you need to create a property list (plist) file, in which you provide values for each of the keys defined in your mapping file, above. You need to create a property list file for each required environment. For example, you may have a test and a production environment.

Using the above example, the plist source code for a production environment may look as follows:

<plist version="1.0">
<dict>
<key>entryPointURL</key>
<string>http://example.com/production</string>
<key>enableFileSharing</key>
<true/>
<key>retryCount</key>
<integer>4</integer>
<key>adUnitPrefix</key>
<string>production_ad_unit</string>
<key>analyticsKey</key>
<string>haf6d9fha8v56abs</string>
<key>environment</key>
<string>.Production</string>
</dict>
</plist>

Before proceeding to the next step, ensure that both the mapping file and plist files are placed inside your project directory. To keep things simple it might be best to place all these files in the same place, for example, in a Config sub-folder. You will need to reference the path to these files in step 3.

Step 3: An external build step for each environment

Finally, you need to create a build target for each of your environments. This can be done be selecting File -> New -> Target and selecting 'External Build System' from the 'Cross-Platform' tab.

In the settings of each build target point the 'Build Tool' to the location of the configen script that you copied to your directory earlier and invoke the arguments as follows. Note that the output directory must be created separately.

configen --plist-path <plist> --hints-path <mapping-file> --class-name <output-class-name> --output-directory <output-directory>

  -p, --plist-path:
      Path to the input plist file
  -h, --hints-path:
      Path to the input hints file
  -n, --class-name:
      The output config class name
  -o, --output-directory:
      The output config class directory
  -c, --objective-c:
      Whether to generate Objective-C files instead of Swift

# e.g.

configen --plist-path EnvironmentConfig/EnvironmentConfig_Prod.plist --hints-path EnvironmentConfig.map --class-name EnvironmentConfig --output-directory EnvironmentConfig

configen generates Swift code by default. You can generate Objective-C code by providing the -c or --objective-c switches

The best way to support multiple environments is to define a separate scheme for each one. Then add the relevant target as an external build step for each scheme ensuring that 'Parallelize Build' is disabled.

Please refer to the example project included in the repository for further guidance.

Standard types supported

  • Int: Expects integer type in plist
  • String: Expects string type in plist
  • Bool: Expects Boolean type in plist
  • Double: Expects floating point type in plist
  • URL: Expects a string in the plist, which can be converted to a URL (validated at compile time)
  • Array : Expects an array of values in the plist

Custom types

Any other type is supported, by providing a string in the plist which compiles successfully when converted to code. For example:

enum Environment {
  case Development
  case UAT
  case Production
}

Providing the mapping type environment : Environment in the mapping file, and the string .Production in the plist, the property in your configuration class will be as follows:

  static let environment: Environment = .Production

This is powerful, because it allows you to work with optionals, which are not supported by the standard types. For example:

Mapping file:

retryCount : Int?

You have to make the type in your plist a string, and input either a number -- e.g. 1 -- or the word nil, so the output property becomes, for example:

  let retryCount: Int? = nil

Arrays

Configen also supports reading arrays from the plist. For example, you can create an array of certificate hashes for pinning purposes and configen will automatically map them to an array in the generated configuration file. The arrays can be of any depth (for example: [String], [[URL]], [[[Any]]], etc).

The downside of using an array in the plist is that the order of the array and whether there are any array elements at all is not guaranteed, so keep that in mind when using this functionality.

Example:

Plist

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
	<key>arrayOfHashes</key>
	<array>
		<string>9BV3692736V893B47V893BY4V94B8V6123984BV6983V6B093</string>
		<string>BVQ09PY89V86BY98VY9876BV9786B98687B6976BOP967BP96</string>
		<string>PB869869P6B76P9B7869P8B69P697P69769769P7B697PB89B</string>
	</array>
</dict>
</plist>

Mapping (.map)

arrayOfHashes: [String]

Config file (generated)

static let arrayOfHashes: [String] = [
  "9BV3692736V893B47V893BY4V94B8V6123984BV6983V6B093",
  "BVQ09PY89V86BY98VY9876BV9786B98687B6976BOP967BP96",
  "PB869869P6B76P9B7869P8B69P697P69769769P7B697PB89B"
]

More Repositories

1

android-proxy-toggle

Small application to help android developers to quickly enable and disable proxy settings
Kotlin
260
star
2

android-permission-manager

Kotlin
109
star
3

TABTestKit

Library designed to make writing and maintaining automated tests for iOS applications. This includes automation of bio-metrics and controlling of mock servers
Swift
54
star
4

accessibility-guidelines

Plain language summary of the Web Content Accessibility Guidelines
HTML
47
star
5

MasterFastfile

Master Fastfile used for internal builds
Ruby
34
star
6

TABResourceLoader

Framework for loading resources from a network service
Swift
29
star
7

TABSwiftLayout

Provides a flexible, yet minimal API for dealing with AutoLayout programatically
Swift
14
star
8

kc-android

KC Android App
Java
12
star
9

where-to-learn-about-accessibility

Here are some of the best resources to start learning about accessibility and inclusive design.
10
star
10

engineering-challenge

Coding challenge for engineers interested in joining our passionate team
Swift
7
star
11

TABScrollingContentView

A scroll view whose content size is determined based on the auto layout constraints of its subviews.
Swift
6
star
12

engineering-reading-list

A collection of podcasts, publications, tutorials, blogs and industry leaders recommended by the Kin + Carta Engineering CoP (Community of Practice).
5
star
13

TABCommunicate

Lightweight strongly typed wrapper around Multipeer Connectivity
Swift
5
star
14

Android-Code-Guidelines

3
star
15

ARKit-Glasses

Swift
2
star
16

github-actions-demo

Github Actions Demo for Android COP
Kotlin
1
star
17

iOS-Style-Guide

Our coding style guide for Objective C and Swift
1
star
18

kap-shopping-app

kap shopping app
TypeScript
1
star
19

ios-challenge

[OLD] Coding challenge for iOS engineers looking to join our passionate team
1
star
20

patios

Manage large Open API specifications and automatically generate TypeScript definitions from them.
TypeScript
1
star
21

Tracy

An example of how to create a simple Contact Tracing app for iOS and Android using Bluetooth LE
Kotlin
1
star
22

chef-zypper

Ruby
1
star
23

ModularSlothCreator

Modular Sloth Creator project for DocC and Danger Blog Posts on https://medium.com/kinandcartacreated/
Swift
1
star