개발/최적화
Ktlint + detekt
이도일
2023. 7. 25. 11:29
Ktlint란?
- 코틀린 표준 코드 스타일을 따르는 린터
Detekt란?
- code smell, 잠재적 성능 저하와 버그를 잡아주는 코드 정적 분석 도구
- editorConfig 파일 수정 → 코드 스타일 작성시에 강제하도록
- git-hooks 파일 완성
- action yml 파일 완성(브라우저)
- master에서 develop따오기
- develop에 이번 branch 합치기
- .git/hooks/precommit 으로 추가 (*확장자 없어야됨)
#!/bin/bash
# Stash the current index to be used in the event of failure
git stash -q --keep-index
echo "Running Ktlint before git commit is committed"
./gradlew ktlintFormat
RESULT=$?
git stash pop -q
# return 1 exit code if running checks fails
[ $RESULT -ne 0 ] && exit 1
######## KTLINT-GRADLE HOOK START ########
CHANGED_FILES="$(git --no-pager diff --name-status --no-color --cached | awk '$1 != "D" && $2 ~ /\.kts|\.kt/ { print $2}')"
if [ -z "$CHANGED_FILES" ]; then
echo "No Kotlin staged files."
exit 0
fi;
echo "Running ktlint over these files:"
echo "$CHANGED_FILES"
./gradlew --quiet ktlintFormat -PinternalKtlintGitFilter="$CHANGED_FILES"
echo "Completed ktlint run."
echo "$CHANGED_FILES" | while read -r file; do
if [ -f $file ]; then
git add $file
fi
done
echo "Completed ktlint hook."
######## KTLINT-GRADLE HOOK END ########
- 프로젝트 단위gradle에 추가
task installGitHook(type: Copy) {
def lintingConfigScript = new File(rootProject.rootDir, '.git/hooks/pre-commit')
if (!lintingConfigScript.exists()) {
from new File(rootProject.rootDir, '.githooks/pre-commit')
into { new File(rootProject.rootDir, '.git/hooks') }
fileMode 0777
}
}
tasks.getByPath('app:preBuild').dependsOn installGitHook
- dependency추가
repositories {
...
maven {
url "https://plugins.gradle.org/m2/"
}
}
dependencies {
...
classpath("org.jlleitschuh.gradle:ktlint-gradle:9.3.0")
}
allProjects {
...
apply plugin: "org.jlleitschuh.gradle.ktlint"
}
- editor config도 추가 (루트단위에서 추가 가능 (project보기로 추가 가능)
root = true
[*]
ktlint_no-wildcard-imports = disabled
charset = utf-8
end_of_line = lf
indent_size = 4
indent_style = space
trim_trailing_whitespace = true
insert_final_newline = true
tab_width = 4
[*.{kt,kts}]
disabled_rules=import-ordering

- 로컬 커밋시 알아서 잘 막음

- desktop으로도 잘 막음
Uploaded by N2T