Git Product home page Git Product logo

swifterate's Introduction

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.

swifterate's People

Contributors

enriquez avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.