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

静的解析ツール detekt で任意の条件で警告させる

静的解析ツール detekt で任意の条件で警告させる

Kotlin Fest 2019 の LT 発表資料です。

Shunsuke Maeda

August 24, 2019
Tweet

More Decks by Shunsuke Maeda

Other Decks in Technology

Transcript

  1. ੩తղੳπʔϧ detekt Ͱ೚ҙͷ৚݅Ͱܯࠂͤ͞Δɹ
    Kotlin Fest 2019
    2019.08.24 [SAT]
    Shunsuke Maeda (@duck8823)

    View Slide

  2. ࣗݾ঺հ
    ✓ Shunsuke Maeda
    ✓ @duck8823
    ✓ ॴଐ
    ✓ גࣜձࣾΤεɾΤϜɾΤε
    ✓ EarthCampusגࣜձࣾ
    ✓ ීஈ͸
    ✓ Kotlin Ͱ։ൃʢαʔόʔαΠυʣ
    2/21

    View Slide

  3. ੩తղੳ
    ϓϩάϥϜΛ࣮ߦͤͣʹιʔείʔυΛղੳ
    ✓ϓϩδΣΫτʹ͓͚ΔίʔσΟϯάϧʔϧ
    ✓ Πϯσϯτ
    ✓ ϝιου / Ϋϥεͷ௕͞
    ✓ ෳࡶ౓
    =>
    ✓ίʔυͷ඼࣭Λอͭ
    ✓ίʔυϨϏϡʔͰϩδοΫʹ஫ྗͰ͖Δ
    3/21

    View Slide

  4. detekt
    arturbosch/detekt
    ✓Kotlin੡ͷ੩తղੳπʔϧ
    ✓ϓϥΨϒϧͳઃܭ
    ✓࠷ۙ(2019-08-13) ver. 1.0.0 ͕ϦϦʔε͞Εͨ
    4/21

    View Slide

  5. ੩తղੳ ͱ CI
    CI (Continuous Integration) ͳͲͰৗʹಈ͔͢͜ͱ͕େࣄ
    ϧʔϧҧ൓͕͋ͬͨ৔߹ʹܯࠂ(= CI ͷδϣϒࣦഊ)
    ಈతʹ CIͷδϣϒ ΛϋϯυϦϯά͍ͨ͠
    ྫ.
    - ίʔυͷن໛ʹΑͬͯܯࠂ͢Δᮢ஋Λม͍͑ͨ
    - PRͷ৔߹͸ࠩ෼͕͋ͬͨϑΝΠϧͷΈର৅ʹ͍ͨ͠
    5/21

    View Slide

  6. ੩తղੳπʔϧ detekt Ͱ೚ҙͷ৚݅Ͱܯࠂͤ͞Δɹ
    6/21

    View Slide

  7. CI ͱ ऴྃίʔυ
    CI Ͱδϣϒ͕ࣦഊ͢Δ৚݅ = ίϚϯυͷऴྃίʔυ͕ 0 Ͱͳ͍
    =>
    ऴྃίʔυΛϋϯυϦϯάͰ͖Ε͹೚ҙͷ৚݅Ͱδϣϒࣦഊ
    7/21

    View Slide

  8. detekt ͱ ऴྃίʔυ
    detekt ίϚϯυͷ৔߹
    fun main(args: Array) {
    try {
    buildRunner(args).execute()
    } catch (e: BuildFailure) {
    e.printStackTrace()
    exitProcess(2)
    } catch (e: Exception) {
    e.printStackTrace()
    exitProcess(1)
    }
    exitProcess(0)
    }
    8/21

    View Slide

  9. BuildFailure ͸Ͳ͔͜Β throw ͞Ε͍ͯΔʁ
    9/21

    View Slide

  10. ઃఆϑΝΠϧʹώϯτ͕͋Δ
    detekt --generate-config # ઃఆϑΝΠϧͷੜ੒ίϚϯυ
    ੜ੒͞ΕͨઃఆϑΝΠϧ
    console-reports:
    active: true
    exclude:
    # - 'ProjectStatisticsReport'
    # - 'ComplexityReport'
    # - 'NotificationReport'
    # - 'FindingsReport'
    # - 'BuildFailureReport'
    10/21

    View Slide

  11. BuildFailureReport
    11/21

    View Slide

  12. BuildFailureReport
    ✓ຊମʹ૊Έࠐ·Ε͍ͯΔ Custom Reports ϓϥάΠϯͷҰछ
    ✓exclude ͢Δͱϧʔϧҧ൓͕͋ͬͯ΋ऴྃίʔυ͸ 0 ʹͳΔ ( 1.0.0 )
    console-reports:
    active: true
    exclude:
    # - 'ProjectStatisticsReport'
    # - 'ComplexityReport'
    # - 'NotificationReport'
    # - 'FindingsReport'
    - 'BuildFailureReport'
    12/21

    View Slide

  13. detekt ͷϓϥάΠϯ
    ✓Custom RuleSets: ಠࣗͷϧʔϧͷ௥Ճ
    ✓Custom Processors: ੩తղੳ࣮ߦ࣌ʢલޙʣͷॲཧ
    ✓ interface FileProcessListener
    ✓Custom Reports: ੩తղੳͷ݁Ռͷग़ྗ
    ✓ abstract class ConsoleReport <= BuildFailureReport
    ✓ abstract class OutputReport
    see also https://arturbosch.github.io/detekt/extensions.html
    13/21

    View Slide

  14. BuildFailureReport
    class BuildFailureReport : ConsoleReport() {
    ...
    override fun render(detektion: Detektion): String? {
    ...
    maxIssues.reached(amount) -> {
    val message = "Build failed with $amount..."
    println(message.red())
    throw BuildFailure(message)
    }
    ,,,
    }
    }
    ※ ver. 1.0.0
    14/21

    View Slide

  15. ຊମ૊ΈࠐΈϓϥάΠϯͱಉ͡Α͏ʹ࡞Ε͹͍͍
    15/21

    View Slide

  16. ೚ҙͷ৚݅Ͱ throw ͢Δ Custom Report
    import io.gitlab.arturbosch.detekt.api.ConsoleReport
    import io.gitlab.arturbosch.detekt.api.Detektion
    import io.gitlab.arturbosch.detekt.cli.console.BuildFailure
    class MyCustomReport : ConsoleReport() {
    override val priority: Int = Int.MIN_VALUE
    ...
    override fun render(detektion: Detektion): String? {
    ...
    # ͜͜Ͱ೚ҙͷ৚݅Ͱ throw BuildFailure("...")
    }
    }
    ✓priority: CustomReportΛ࣮ߦ͢Δ༏ઌ౓
    16/21

    View Slide

  17. ؆୯...?
    17/21

    View Slide

  18. BuildFailureReport ʹมߋ...
    arturbosch/detekt#1818ʢ6೔લʣ
    18/21

    View Slide

  19. PR #1818 ޙ ( 1.0.1 ʹೖΔ༧ఆ )
    ✓BuildFailureReport Ͱ͸ throw ͠ͳ͍
    ✓Runner Ͱ throw ͢Δ
    => BuildFailureReport Λ exclude ͯ͠΋ऴྃεςʔλε͸มΘΒͳ͍
    ※ BuildFailureReport Ͱ throw ͍ͯ͠Δͷ͸ઃܭ্ͷόά
    => 1.0.1Ͱमਖ਼
    19/21

    View Slide

  20. build failure ͷແޮԽ
    ઃఆϑΝΠϧͰ
    build:
    maxIssues: -1
    ແޮԽ্ͨ͠ CustomReport ͕ྫ֎Λ౤͛Δ͜ͱͰऴྃίʔυมߋՄ
    1.0.0 Ͱ΋༗ޮ
    20/21

    View Slide

  21. ·ͱΊ
    ੩తղੳπʔϧ detekt Ͱ೚ҙͷ৚݅Ͱܯࠂͤ͞Δ
    ✓<= 1.0.0
    ✓ BuildFailureReport Λແޮ
    ✓ build -> maxIssues: -1
    ✓ CustomReport Ͱ throw BuildFailure
    ✓>= 1.0.1
    ✓ build -> maxIssues: -1
    ✓ CustomReport Ͱ throw BuildFailure
    21/21

    View Slide