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

devsap 20180728: コード生成のススメ

devsap 20180728: コード生成のススメ

Takeshi Ihara

July 28, 2018
Tweet

More Decks by Takeshi Ihara

Other Decks in Programming

Transcript

  1. ίʔυੜ੒ͷεεϝ
    iPhone Dev Sapporoษڧձ Jul, 2018

    View Slide

  2. Takeshi Ihara
    • Twitter: @nonchalant0303
    • GitHub: Nonchalant
    iPhone Dev Sapporoษڧձ Jul, 2018

    View Slide

  3. ίʔυੜ੒
    • SwiftͰ͸Runtime Reflection͕͋·ΓڠྗͰ͸ͳ͍
    • ϓϩάϥϜͷ࣮ߦաఔͰϓϩάϥϜࣗ਎ͷߏ଄ΛಡΈऔͬ
    ͨΓॻ͖׵͑ͨΓ͢Δٕज़
    • ͦͷ୅ΘΓSwiftͰ͸ίʔυੜ੒Ͱղܾ͢Δ͜ͱ͕૿͖͑ͯͨ
    • ex. ϘΠϥʔϓϨʔτͷࣗಈੜ੒
    iPhone Dev Sapporoษڧձ Jul, 2018

    View Slide

  4. DIKit
    https://github.com/ishkawa/DIKit
    DIKit provides interfaces to express dependency graph.
    A code generator named dikitgen finds implementations of
    the interfaces, and generate codes which satisfies
    dependency graph.
    iPhone Dev Sapporoษڧձ Jul, 2018

    View Slide

  5. Cuckoo
    https://github.com/Brightify/Cuckoo
    Cuckoo has two parts. One is the runtime and the other one is
    an OS X command-line tool simply called CuckooGenerator.
    Unfortunately Swift does not have a proper reflection, so we
    decided to use a compile-time generator to go through files
    you specify and generate supporting structs/classes that will
    be used by the runtime in your test target.
    iPhone Dev Sapporoษڧձ Jul, 2018

    View Slide

  6. DIKit
    https://github.com/ishkawa/DIKit
    iPhone Dev Sapporoษڧձ Jul, 2018

    View Slide

  7. Cuckoo
    https://github.com/Brightify/Cuckoo
    iPhone Dev Sapporoษڧձ Jul, 2018

    View Slide

  8. Sourcery
    https://github.com/krzysztofzablocki/Sourcery
    Sourcery is a code generator for Swift language, built on top
    of Apple's own SourceKit. It extends the language abstractions
    to allow you to generate boilerplate code automatically.
    iPhone Dev Sapporoษڧձ Jul, 2018

    View Slide

  9. Sourcery
    protocol Animal {
    func bark()
    }
    struct Dog: Animal {} // ίϯύΠϧΤϥʔ
    struct Apple {}
    iPhone Dev Sapporoษڧձ Jul, 2018

    View Slide

  10. Stencil
    ϑΝΠϧςϯϓϨʔτݴޠ
    {% for struct in types.structs where struct.implementing.Animal %}
    extension {{ struct.name }} {
    func bark() {
    print("{{ struct.name }}")
    }
    }
    {% endfor %}
    ↓ Sourcery
    extension Cat {
    func bark() {
    print("Cat")
    }
    }
    iPhone Dev Sapporoษڧձ Jul, 2018

    View Slide

  11. SourceKitten
    https://github.com/jpsim/SourceKitten
    • An adorable little framework and command line tool for
    interacting with SourceKit
    • DIKit, Cuckoo, SourceryͰ࠾༻͞Ε͍ͯΔ
    iPhone Dev Sapporoษڧձ Jul, 2018

    View Slide

  12. SourceKitten
    ࢖ͬͯΈΔ
    // Source.swift
    struct Dog {}
    // SourceKitten
    import SourceKittenFramework
    let file = File(path: "./Source.swift")!
    let dic = try! Structure(file: file).dictionary
    print(dic)
    iPhone Dev Sapporoษڧձ Jul, 2018

    View Slide

  13. SourceKitten
    [String: SourceKitRepresentable]
    struct Dog {}
    [
    "key.diagnostic_stage": "source.diagnostic.stage.swift.parse",
    "key.substructure": [
    [
    "key.bodylength": 0,
    "key.nameoffset": 7,
    "key.accessibility": "source.lang.swift.accessibility.internal",
    "key.length": 13,
    "key.name": "Dog",
    "key.kind": "source.lang.swift.decl.struct",
    "key.namelength": 3,
    "key.offset": 0,
    "key.bodyoffset": 12
    ]
    ],
    "key.offset": 0,
    "key.length": 13
    ]
    iPhone Dev Sapporoษڧձ Jul, 2018

    View Slide

  14. FactoryProvider
    https://github.com/Nonchalant/FactoryProvider
    • SourceKitten + StencilΛ࢖ͬͯϑΝΫτϦʔΛࣗಈੜ੒͢Δ
    ϥΠϒϥϦΛ࡞ͬͯΈͨʂ
    • ৄࡉ͸Ͳ͔͜ʹࢿྉ͋͛·͢ʂ (iOSDCམͪͯ͠·ͬͨ...)
    iPhone Dev Sapporoษڧձ Jul, 2018

    View Slide

  15. Demo
    iPhone Dev Sapporoษڧձ Jul, 2018

    View Slide

  16. ·ͱΊ
    • ίʔυੜ੒ָ͍͠ʂ͕΍Γ͗͢ΔͱଐਓԽ͢ΔͷͰγϯϓϧ
    ʹѻ͏ͷ͕େࣄͳؾ͕ͨ͠
    • iOSDC or DevSapͰ·ͨձ͍·͠ΐ͏ʂ
    iPhone Dev Sapporoษڧձ Jul, 2018

    View Slide