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
  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 …"
  3. @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…
  4. @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 }
  5. @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 }
  6. @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 { … }
  7. @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
  8. @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