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

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

Fb60bbce2fadcc41e950ef8dea3f29e5?s=47 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

Fb60bbce2fadcc41e950ef8dea3f29e5?s=128

AliSoftware

September 23, 2017
Tweet

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 3

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

    4
  5. @aligatr Strong Typing !

  6. @aligatr 6

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

  8. No more manual maintenance! No more stringWithFormat! No more typos!

    Auto-completion! Type-safety!
  9. @aligatr

  10. @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…
  11. @aligatr

  12. @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 }
  13. @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 }
  14. @aligatr

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

    Your Code Boring Code Generated for you $ sourcery
  16. @aligatr sources: - CodeGenDemo/Sources templates: - CodeGenDemo/CodeGen/Templates output: CodeGenDemo/CodeGen/Generated .sourcery.yml

    + =
  17. @aligatr

  18. @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 { … }
  19. @aligatr

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

    • • •
  21. @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
  22. @aligatr

  23. @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