Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Don't Worry, Be Lazy — SwiftGen, Sourcery & gyro

AliSoftware
September 23, 2017

Don't Worry, Be Lazy — SwiftGen, Sourcery & gyro

Slides of my lightning talk at FrenchKit'17 about Code Generation in Swift with SwiftGen, Sourcery and Gyro

AliSoftware

September 23, 2017
Tweet

More Decks by AliSoftware

Other Decks in Programming

Transcript

  1. Don't worry,
    Be Lazy
    And let your Mac write your code for you
    FrenchKit 2017

    O. Halligon

    View full-size slide

  2. @aligatr
    UIFont(name: "Avenir-It ", size: 42)
    2
    let format = NSLocalizedString("home.greeting ", …)
    String(format: format, 1 , "FrenchKit")
    Thread 1: EXC_BAD_ACCESS (code=1, address=0x1)

    "home.greetings" = "… %@ … %d …"

    View full-size slide

  3. @aligatr
    label.text =
    L10n.homeGreetings("FrenchKit", 1, "Paris")
    label.font =
    FontFamily.Avenir.blackOblique.font(size: 18)
    4

    View full-size slide

  4. @aligatr
    Strong Typing !

    View full-size slide

  5. @aligatr
    Code Generate All the Things!
    7
    $ swiftgen

    View full-size slide

  6. No more manual maintenance!
    No more stringWithFormat!
    No more typos!
    Auto-completion!
    Type-safety!

    View full-size slide

  7. @aligatr
    extension Phone: Equatable {
    static func ==(lhs: Phone, rhs: Phone) -> Bool
    guard lhs.model == rhs.model else { return false }
    guard lhs.name == rhs.name else { return false }
    guard lhs.priceUSD == rhs.priceUSD else { return false }
    // Oh joy, 30 more to go
    ]
    }
    10
    struct Phone {
    let model: PhoneModel
    let name: String
    let priceUSD: Double
    // and 30 other properties
    }
    But…

    View full-size slide

  8. @aligatr
    extension PhoneModel {
    static let allValues: [
    .iPhone, .iPhone3G, .iPhone3GS,
    .iPhone4, .iPhone4s,
    .iPhone5, .iPhone5c, .iPhone5s,
    .iPhoneSE, .iPhone6, .iPhone6Plus,
    .iPhone6s, .iPhone6sPlus,
    .iPhone7, .iPhone7Plus
    ]
    }
    12
    enum PhoneModel {
    case iPhone, iPhone3G, iPhone3GS
    case iPhone4, iPhone4s
    case iPhone5, iPhone5c, iPhone5s
    case iPhoneSE
    case iPhone6, iPhone6Plus
    case iPhone6s, iPhone6sPlus
    case iPhone7, iPhone7Plus
    }

    View full-size slide

  9. @aligatr
    extension PhoneModel {
    static let allValues: [
    .iPhone, .iPhone3G, .iPhone3GS,
    .iPhone4, .iPhone4s,
    .iPhone5, .iPhone5c, .iPhone5s,
    .iPhoneSE, .iPhone6, .iPhone6Plus,
    .iPhone6s, .iPhone6sPlus,
    .iPhone7, .iPhone7Plus
    ]
    }
    13
    enum PhoneModel {
    case iPhone, iPhone3G, iPhone3GS
    case iPhone4, iPhone4s
    case iPhone5, iPhone5c, iPhone5s
    case iPhoneSE
    case iPhone6, iPhone6Plus
    case iPhone6s, iPhone6sPlus
    case iPhone7, iPhone7Plus
    case iPhone8, iPhone8Plus, iPhoneX
    }

    View full-size slide

  10. @aligatr
    15
    AutoCases.stencil
    AutoEquatable.stencil
    AutoHashable.stencil
    AutoLenses.stencil
    AutoTypeErased.stencil
    AutoProtocolInterface.stencil
    AutoClonable.stencil

    Your Code Boring Code
    Generated
    for you
    $ sourcery

    View full-size slide

  11. @aligatr
    sources:
    - CodeGenDemo/Sources
    templates:
    - CodeGenDemo/CodeGen/Templates
    output: CodeGenDemo/CodeGen/Generated
    .sourcery.yml
    +
    =

    View full-size slide

  12. @aligatr
    extension PhoneModel {
    let allValues: [
    .iPhone,
    .iPhone3G,
    … /* All the cases autogenerated */
    … /* no more maintenance */
    ]
    }
    18
    {% for enum in types.implementing.AutoCases|enum %}
    extension {{ enum.name }} {
    static let allValues: [{{ enum.name }}] = [
    {% for case in enum.cases %}
    .{{ case.name }},
    {% endfor %}
    ]
    }
    {% endfor %}
    $ sourcery
    enum PhoneModel: AutoCases { … }

    View full-size slide

  13. @aligatr
    20
    $ gyro
    GSON ObjectMapper Decodable
    • • •
    • • •

    View full-size slide

  14. @aligatr
    Model Generation
    Type Erasure
    Hashable
    Equatable
    Lenses
    Decorator
    Dependency Injection AutoCompletion
    Localizable String Constants
    Typed String Formats
    Fonts Constants
    Images Constants
    Storyboards Constants
    Colors Constants
    Type Safety
    Automatic Maintenance
    Protocol API
    Mocks
    More time for NetFlix
    Typos detected at compile time
    All Cases for Enums
    Reduce Hair Pulling
    Customizable Code
    JSON Parsing code
    Time for Wine & Chill
    More time to read my blog

    View full-size slide

  15. @aligatr
    Hello, I’m Olivier
    github.com/SwiftGen/SwiftGen
    github.com/krzysztofzablocki/Sourcery
    github.com/AliSoftware
    alisoftware.github.io
    @aligatr
    23
    github.com/NijiDigital/gyro
    Classroom: Mastering Code Generation

    with SwiftGen & Sourcery templates

    View full-size slide