Arestronaut / FancyOldSwiftModel

A tool to easily generate swift models

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

FancyOldSwiftModel

Swift

... is a tool to generate swift models either by providing template files (which are written as a standalone swift protocol) or by using the inline sytnax

Installation

For now building from source is the only available way to install the tool:

  1. Build swift package
swift build --configuration release
  1. Copy the executable to usr/local/bin
cp -f .build/release/FancyOldSwiftModel /usr/local/bin/FancyOldSwiftModel

TODO

  • Add HomeBrew support

Generate models from template files

In order to generate model from template files you need to provide a path to the templates and an output path.

FancyOldSwiftModel generateFromTemplate -m #Output path# -t #Template path#

By providing the flag --creates the given paths will be created if not existant.

Example Template

A very simple example to create an Account type could look like this.

protocol AccountTemplate: Equatable, Codable {
    var id: UUID { get }
    var name: String { get set }
}

The result would be:

struct Account: Equatable, Codable {
    let id: UUID
    var name: String
}

Configure the output

Let's say you want a reference type instead of a value type and the type should be open: You simply add a configuration comment above the protocol declaration, like this:

// modelgenconfig: refType, open
protocol AccountTemplate: Equatable, Codable {
    var id: UUID { get }
    var name: String { get set }
}

That would result in:

open class Account: Equatable, Codable {
    public let id: UUID
    open var name: String

    public init(id: UUID, name: String) {
        self.id = id
        self.name = name
    }
}

// MARK: - Equatable
extension Account {
    public static func ==(lhs: Account, rhs: Account) -> Bool {
        lhs.id == rhs.id && lhs.name == rhs.name
    }
}

A full list of configurations can be found here.

TODO

  • Add support for more configurations

Generate a model directly from the command line

Generating a simple model on the fly is pretty straight forward. Simply use the command generate:

FancyOldSwiftModel generate -i "Account" -m "id: UUID, name: String"

That results in:

struct Account {
    var id: UUID
    var name: String
}

The created file will be created in the current directory. Optionally an output path can be supplied with the argument -o #Output path#

TODO

  • Add support for configuration

About

A tool to easily generate swift models

License:MIT License


Languages

Language:Swift 96.7%Language:Makefile 2.2%Language:Ruby 1.1%