방대한 문서보다 동작하는 소프트웨어

개발

[안드로이드] spotless 를 적용하자

꽃게장세트 2022. 1. 10. 01:59

spotless 란?

spotless 는 코드를 흠 잡을데 없이 유지시켜주는 도구다.

 

적용하기

spotless는 ktlint 를 사용하고 있는 프로젝트 수준의 build.gradle.kts 에 아래의 코드를 삽입하자!

plugins {
  id("com.diffplug.gradle.spotless") version "3.27.1"
}

subprojects {
  apply(plugin = "com.diffplug.gradle.spotless")
  val ktlintVer = "0.43.2"
  spotless {
    kotlin {
      target("**/*.kt")
      ktlint(ktlintVer).userData(
        mapOf("max_line_length" to "100", "disabled_rules" to "import-ordering")
      )
      licenseHeaderFile(project.rootProject.file("copyright.kt"))
      trimTrailingWhitespace()
      indentWithSpaces()
      endWithNewline()
    }
    kotlinGradle {
      // same as kotlin, but for .gradle.kts files (defaults to '*.gradle.kts')
      target("**/*.gradle.kts")
      ktlint(ktlintVer)
      licenseHeaderFile(
        project.rootProject.file("copyright.kt"),
        "(plugins |import |include)"
      )
      trimTrailingWhitespace()
      indentWithSpaces()
      endWithNewline()
    }
    format("misc") {
      target("**/*.md", "**/.gitignore")
      trimTrailingWhitespace()
      indentWithSpaces()
      endWithNewline()
    }
  }
}

실행은,

./gradlew spotlessApply

추가로

필자는 .editorconfig 와 함께 사용하고 있다. 물론 겹치는 부분이 있다. 동일하게 맞추면 문제가 없다. 다르게 설정해놓았다면 실행할때 문제가 생길 것이다.

# http://editorconfig.org

root = true

[{*.kt, *.kts}]
charset = utf-8
indent_style = space
indent_size = 2
end_of_line = lf
insert_final_newline = true
trim_trailing_whitespace = true
max_line_length = 100

[*.md]
trim_trailing_whitespace = true

[*.java]
max_line_length = 100

결론

반드시 적용하자.