Skip to content

Commit 3a3f53e

Browse files
committed
Refactor static checks configuration for Kotlin DSL
Moved static check configurations (Checkstyle and PMD) from the `staticChecks.gradle` script into `build.gradle.kts`. This centralizes Gradle setup into one file and leverages Kotlin DSL for better type safety and maintainability.
1 parent a3e965f commit 3a3f53e

File tree

2 files changed

+80
-87
lines changed

2 files changed

+80
-87
lines changed

build.gradle.kts

Lines changed: 80 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,15 @@
33
* See COPYING.txt for license details.
44
*/
55

6+
import groovy.json.JsonSlurper
67
import org.jetbrains.changelog.Changelog
78
import org.jetbrains.changelog.markdownToHTML
89
import org.jetbrains.intellij.platform.gradle.TestFrameworkType
910

1011
plugins {
1112
id("java")
13+
id("checkstyle")
14+
id("pmd")
1215
alias(libs.plugins.kotlin)
1316
alias(libs.plugins.intelliJPlatform)
1417
alias(libs.plugins.changelog)
@@ -109,10 +112,6 @@ intellijPlatform {
109112
}
110113
}
111114

112-
apply {
113-
from("${project.rootDir}/gradle-tasks/staticChecks.gradle")
114-
}
115-
116115
changelog {
117116
groups.empty()
118117
repositoryUrl = providers.gradleProperty("pluginRepositoryUrl")
@@ -141,6 +140,20 @@ tasks {
141140
exclude("com/magento/idea/magento2plugin/actions/**") //https://github.com/magento/magento2-phpstorm-plugin/issues/2474
142141
useJUnitPlatform()
143142
}
143+
144+
checkstyle {
145+
toolVersion = "8.31"
146+
isIgnoreFailures = false
147+
maxWarnings = 0
148+
configFile = rootProject.file("${rootDir}/gradle-tasks/checkstyle/checkstyle.xml")
149+
}
150+
151+
pmd {
152+
toolVersion = "6.21.0"
153+
isConsoleOutput = true
154+
ruleSetFiles = files("${rootDir}/gradle-tasks/pmd/ruleset.xml")
155+
ruleSets = listOf()
156+
}
144157
}
145158

146159
intellijPlatformTesting {
@@ -163,3 +176,66 @@ intellijPlatformTesting {
163176
}
164177
}
165178
}
179+
180+
// Configure Checkstyle tasks
181+
tasks.withType(Checkstyle::class).configureEach {
182+
// Specify all files that should be checked
183+
classpath = files()
184+
setSource("${project.rootDir}")
185+
}
186+
187+
// Execute Checkstyle on all files
188+
tasks.register<Checkstyle>("checkstyle") {
189+
// Task-specific configuration can go here if necessary
190+
}
191+
192+
// Execute Checkstyle on all modified files
193+
tasks.register<Checkstyle>("checkstyleCI") {
194+
val changedFiles = getChangedFiles()
195+
include(changedFiles)
196+
}
197+
198+
// Configure PMD tasks
199+
tasks.withType(Pmd::class).configureEach {
200+
// Specify all files that should be checked
201+
classpath = files()
202+
setSource("${project.rootDir}")
203+
}
204+
205+
// Execute PMD on all files
206+
tasks.register<Pmd>("pmd") {
207+
// Task-specific configuration can go here if necessary
208+
}
209+
210+
// Execute PMD on all modified files
211+
tasks.register<Pmd>("pmdCI") {
212+
val changedFiles = getChangedFiles()
213+
include(changedFiles)
214+
}
215+
216+
/**
217+
* Get all files that are changed but not deleted nor renamed.
218+
* Compares to master or the specified target branch.
219+
*
220+
* @return list of all changed files
221+
*/
222+
fun getChangedFiles(): List<String> {
223+
val modifiedFilesJson = System.getenv("MODIFIED_FILES")
224+
val files = mutableListOf<String>()
225+
226+
if (modifiedFilesJson == null) {
227+
return files
228+
}
229+
230+
println("Modified Files: $modifiedFilesJson")
231+
232+
// Parse the JSON string into a list of files
233+
val modifiedFiles = JsonSlurper().parseText(modifiedFilesJson) as List<*>
234+
235+
modifiedFiles.forEach {
236+
files.add(it.toString())
237+
}
238+
239+
// Return the list of touched files
240+
return files
241+
}

gradle-tasks/staticChecks.gradle

Lines changed: 0 additions & 83 deletions
This file was deleted.

0 commit comments

Comments
 (0)