Open keloud opened 4 months ago
Git コミット前の処理にはpre-commitを利用する予定 https://pre-commit.com/
以下のページを参考に導入する
以下に変更する
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
plugins {
id("org.springframework.boot") version "3.2.3"
id("io.spring.dependency-management") version "1.1.4"
id("org.jlleitschuh.gradle.ktlint") version "12.1.0" // 追加
kotlin("jvm") version "1.9.22"
kotlin("plugin.spring") version "1.9.22"
kotlin("plugin.jpa") version "1.9.22"
}
java {
sourceCompatibility = JavaVersion.VERSION_21
targetCompatibility = JavaVersion.VERSION_21
}
allprojects {
val axonVersion = "4.9.3"
group = "inaba"
version = "0.0.1-SNAPSHOT"
apply(plugin = "kotlin")
apply(plugin = "org.jlleitschuh.gradle.ktlint") // 追加
repositories {
mavenCentral()
}
tasks.withType<KotlinCompile> {
kotlinOptions {
freeCompilerArgs += "-Xjsr305=strict"
jvmTarget = "21"
}
}
tasks.withType<Test> {
useJUnitPlatform()
}
dependencies {
implementation(platform("org.axonframework:axon-bom:${axonVersion}"))
implementation("io.github.oshai:kotlin-logging-jvm:5.1.0")
implementation("com.michael-bull.kotlin-result:kotlin-result:2.0.0")
}
// Ktlintの設定値
configure<org.jlleitschuh.gradle.ktlint.KtlintExtension> {
// testディレクトリをコーディング規約チェックから除外する
filter {
exclude { element ->
element.file.path.contains("test")
}
}
} // 追加
}
Git pre-commitを利用した問題点
Git側で動作するためIntellij IDEAのJavaの設定などが利用されず、OS側の設定が利用されるため個々のメンバの開発環境によって差異が生じやすい
そのためIntellij IDEA内で完結するように設定を組む方法を考える
回避策 editorconfigを利用する方法
開発メンバ側 Intellij IDEAがeditorconfigを自動的に読みとりコードスタイルに適用してくれるため、メンバの手元のフォーマッタをIntellij IDEA純正のまま.editorconfigを利用して共通の設定を組み、GradleのTest Taskの実行をフックしktlintのCheckを実行する Intellij IDEA上で実行されるため、Intellij IDEAのJavaの設定を利用できる
GitHub側 Pull Requestまたはfuturesブランチへマージ毎にGitHub ActionsでktlintのCheckを実行する
editorconfig例
プロジェクトディレクトリ/.editorconfig
root = true
[*]
charset = utf-8
insert_final_newline = true
trim_trailing_whitespace = true
[*.{kt,kts}]
indent_size = 4
ij_kotlin_packages_to_use_import_on_demand = unset
ij_kotlin_name_count_to_use_star_import = 999
ij_kotlin_name_count_to_use_star_import_for_members = 999
ktlint_code_style=intellij_idea
[*.{yml,yaml}]
indent_size = 2
pre-commitでktlintを実行するために、環境変数にJAVA_HOMEを追加する必要がある
重要度
高い
概要
Linterを用いたコーディングルールの定義
目的
内容
ktlintなどを利用してGit コミット前にLinterを強制的に実行し、規約違反があればコミットできないようにする
IDEのコーディング支援よりもより強制力のある状態にし、凡ミスを減らす