Project

swifterate

0.0
No commit activity in last 3 years
No release in over 3 years
Swift code generator. Generate strongly typed enums based on your Info.plist and Asset Catalog. No more magic strings!
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 1.9
~> 10.0

Runtime

~> 1.0
~> 0.19
 Project Readme

Swifterate

Swift code generator. Generate strongly typed enums based on your Info.plist and Asset Catalog. No more magic strings!

Installation

Swifterate is distributed as a Ruby gem.

$ gem install swifterate

Usage

After installing, the swifterate command is available:

$ swifterate help
Commands:
  swifterate ac DIR          # Generate Swift code from an Asset Catalog directory located at DIR
  swifterate help [COMMAND]  # Describe available commands or one specific command
  swifterate plist FILE      # Generate Swift code from the .plist file located at FILE

The generated Swift code is printed to the console. You can redirect the output to a file instead:

$ swifterate plist test/fixtures/Info.plist > InfoPlist.swift

Examples

Swifterate currently supports .plist and .xcassets as inputs to generate Swift code.

.plist to Swift Enum

Generate a swift enum from the following MyApp-Info.plist file:

<?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>CFBundleName</key>
	<string>MyApp</string>
	<key>CFBundleShortVersionString</key>
	<string>1.0.0</string>
	<key>CFBundleVersion</key>
	<string>100</string>
</dict>
</plist>

The following is generated with the command: swifterate plist MyApp-Info.plist

enum InfoPlist: String {

    case CFBundleName = "CFBundleName"
    case CFBundleShortVersionString = "CFBundleShortVersionString"
    case CFBundleVersion = "CFBundleVersion"

    private var infoDictionary: [NSObject : AnyObject] {
        return NSBundle.mainBundle().infoDictionary!
    }

    var dictionary: [NSObject : AnyObject] {
        return infoDictionary[rawValue] as! [NSObject : AnyObject]
    }
    
    var array: [AnyObject] {
        return infoDictionary[rawValue] as! [AnyObject]
    }
    
    var data: NSData {
        return infoDictionary[rawValue] as! NSData
    }
    
    var date: NSDate {
        return infoDictionary[rawValue] as! NSDate
    }
    
    var number: NSNumber {
        return infoDictionary[rawValue] as! NSNumber
    }
    
    var string: String {
        return infoDictionary[rawValue] as! String
    }


    init(_ key: InfoPlist) {
        self = key
    }

}

To use the generated enum:

let appName = InfoPlist(.CFBundleName).string // "MyApp"

Asset Catalog to UIImage Extension

Generate a UIImage extension with an enumerated list of assets from the Asset Catalog:

MyApp.xcassets/
  AppIcon.appiconset/
  LaunchImage.launchimage/
  MyAppLogo.imageset/
  PrimaryButton.imageset/

The following is generated with the command: swifterate ac MyApp.xcassets

extension UIImage {
    
    enum MyAppAsset: String {
        case MyAppLogo = "MyAppLogo"
        case PrimaryButton = "PrimaryButton"
    }
    
    convenience init!(assetIdentifier: MyAppAsset) {
        self.init(named: assetIdentifier.rawValue)
    }
    
}

Notice that the AppIcon and LaunchImage aren't included. They cannot be referenced using UIImage(imageNamed:)

To use the generated extension:

let myAppLogo = UIImage(assetIdentifier: .MyAppLogo)

License

Swifterate is available under the MIT license. See the LICENSE file for more info.