diff --git a/.github/workflows/build_and_test.yml b/.github/workflows/build_and_test.yml index a40d4e4690a..2601e4e3d7d 100644 --- a/.github/workflows/build_and_test.yml +++ b/.github/workflows/build_and_test.yml @@ -46,7 +46,7 @@ jobs: - name: Build run: | - ./gradlew --no-daemon --stacktrace build -x test + ./gradlew --no-daemon --stacktrace build -x test -x checkstyleMain -x checkstyleTest -x checkstyleJmh -x checkstyleIntegrationTest -x spotlessApply -x spotlessJavaCheck - name: Archive build artifacts uses: actions/upload-artifact@b4b15b8c7c6ac21ea08fcf65892d2ee8f75cf882 #v4.4.3 diff --git a/.gitignore b/.gitignore index 80b59516973..39e1c2131b8 100644 --- a/.gitignore +++ b/.gitignore @@ -53,10 +53,10 @@ blocksminer jacocoHtml/ # CheckStyle Reports -config/checkstyle/reports/ +rskj-core/config/checkstyle/reports/ # PMD -config/pmd/reports/ +rskj-core/config/pmd/reports/ blocksminer1.txt diff --git a/config/checkstyle/checkstyle.xml b/config/checkstyle/checkstyle.xml new file mode 100644 index 00000000000..73498f813c9 --- /dev/null +++ b/config/checkstyle/checkstyle.xml @@ -0,0 +1,365 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/config/checkstyle/suppressions.xml b/config/checkstyle/suppressions.xml new file mode 100644 index 00000000000..f18d7beffe2 --- /dev/null +++ b/config/checkstyle/suppressions.xml @@ -0,0 +1,5 @@ + + + + + diff --git a/gradle/verification-metadata.xml b/gradle/verification-metadata.xml index 510e633ddef..adc09c6f6ab 100644 --- a/gradle/verification-metadata.xml +++ b/gradle/verification-metadata.xml @@ -9,6 +9,14 @@ + + + + + + + + @@ -46,6 +54,78 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + @@ -227,6 +307,11 @@ + + + + + @@ -240,6 +325,121 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + @@ -248,11 +448,92 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + @@ -261,6 +542,22 @@ + + + + + + + + + + + + + + + + @@ -277,6 +574,14 @@ + + + + + + + + @@ -483,6 +788,14 @@ + + + + + + + + @@ -499,6 +812,14 @@ + + + + + + + + @@ -507,6 +828,30 @@ + + + + + + + + + + + + + + + + + + + + + + + + @@ -695,6 +1040,27 @@ + + + + + + + + + + + + + + + + + + + + + @@ -705,6 +1071,16 @@ + + + + + + + + + + @@ -736,6 +1112,16 @@ + + + + + + + + + + @@ -746,6 +1132,11 @@ + + + + + @@ -767,6 +1158,40 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + @@ -804,6 +1229,56 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + @@ -832,6 +1307,19 @@ + + + + + + + + + + + + + @@ -927,6 +1415,22 @@ + + + + + + + + + + + + + + + + @@ -1287,6 +1791,22 @@ + + + + + + + + + + + + + + + + @@ -1303,6 +1823,14 @@ + + + + + + + + @@ -1311,6 +1839,11 @@ + + + + + @@ -1347,6 +1880,11 @@ + + + + + @@ -1405,5 +1943,16 @@ + + + + + + + + + + + diff --git a/rskj-core/build.gradle b/rskj-core/build.gradle index 0078115a127..f7a8fb332ad 100644 --- a/rskj-core/build.gradle +++ b/rskj-core/build.gradle @@ -1,10 +1,157 @@ plugins { id 'application' + id 'checkstyle' + id "com.diffplug.spotless" version "6.13.0" } apply plugin: 'maven-publish' apply plugin: 'jacoco' +import java.nio.file.Paths + +// Define static timestamp +def staticTimestamp = '2024-10-31T10:24:00' +ext.timestamp = staticTimestamp + +def filteredFiles = getFilteredFiles() + +checkstyle { + toolVersion = '8.45' + configFile = file("$rootDir/config/checkstyle/checkstyle.xml") +} + +spotless { + java { + googleJavaFormat() + indentWithTabs(2) + trimTrailingWhitespace() + endWithNewline() + target filteredFiles + } +} + +def getFilteredFiles() { + def timestamp = ext.timestamp + + def commandLog = [ + 'git', 'log', '--since=' + timestamp, '--name-only', '--pretty=format:', '--abbrev-commit' + ] + def commandDiff = [ + 'git', 'diff', '--name-only' + ] + def commandStaged = [ + 'git', 'diff', '--cached', '--name-only' + ] + + def outputLog = new ByteArrayOutputStream() + def outputDiff = new ByteArrayOutputStream() + def outputStaged = new ByteArrayOutputStream() + + exec { + commandLine commandLog + standardOutput = outputLog + errorOutput = outputLog + ignoreExitValue = true + } + + exec { + commandLine commandDiff + standardOutput = outputDiff + errorOutput = outputDiff + ignoreExitValue = true + } + + exec { + commandLine commandStaged + standardOutput = outputStaged + errorOutput = outputStaged + ignoreExitValue = true + } + + def committedFiles = outputLog.toString().trim().split('\n').toList().unique() + def uncommittedFiles = outputDiff.toString().trim().split('\n').toList().unique() + def stagedFiles = outputStaged.toString().trim().split('\n').toList().unique() + def allFiles = committedFiles + uncommittedFiles + stagedFiles + + allFiles = allFiles.unique().findAll { file -> file } + + def projectRoot = project.projectDir.toPath().toAbsolutePath() + + allFiles = allFiles.findAll { file -> + file.endsWith('.java') + }.collect { file -> + def relativePath = Paths.get(file).subpath(1, Paths.get(file).getNameCount()) // Adjust the subpath as necessary + def absolutePath = projectRoot.resolve(relativePath).toFile() + + return absolutePath.exists() ? absolutePath : null + }.findAll { it != null } + + return allFiles +} + + +def configureCheckstyleTask(Checkstyle task, String sourceDir) { + def filteredFilesCheckstyle = getFilteredFilesForCheckstyle(sourceDir) + + if (!filteredFilesCheckstyle.isEmpty()) { + task.source = project.files(filteredFilesCheckstyle) + } else { + println "No files to assign to Checkstyle for ${sourceDir}. Skipping task." + task.enabled = false + } + + task.classpath = project.files( + sourceSets.main.output.classesDirs, + sourceSets.main.output.resourcesDir + ) + task.reports { + xml.required.set(true) + html.required.set(true) + } + task.onlyIf { + !filteredFilesCheckstyle.isEmpty() + } +} + +def getFilteredFilesForCheckstyle(String sourceDir) { + def files = getFilteredFiles() + return files.findAll { file -> + def filePath = file.toPath().normalize() + def isJavaFile = filePath.toString().endsWith('.java') + def containsSourceSegment = filePath.toString().contains(sourceDir) + + isJavaFile && containsSourceSegment + } +} + + +// Configure each Checkstyle task +tasks.withType(Checkstyle).configureEach { Checkstyle task -> + def sourceDirMapping = [ + 'checkstyleMain': 'src/main/java', + 'checkstyleTest': 'src/test/java', + 'checkstyleIntegrationTest': 'src/integrationTest/java', + 'checkstyleJmh': 'src/jmh/java' + ] + + sourceDirMapping.each { taskName, sourceDir -> + if (task.name == taskName) { + configureCheckstyleTask(task, sourceDir) + } + } + + if (task.name in ['checkstyleJmh', 'checkstyleIntegrationTest']) { + task.doFirst { + def relevantSourceDir = task.name == 'checkstyleJmh' ? 'src/jmh/java' : 'src/integrationTest/java' + def checkstyleFiles = getFilteredFilesForCheckstyle(relevantSourceDir) + + if (!checkstyleFiles.isEmpty()) { + task.source = project.files(checkstyleFiles) + } + } + } +} + configurations { jmh } @@ -81,6 +228,7 @@ tasks.withType(Javadoc) { repositories { mavenCentral() + gradlePluginPortal() maven { url "https://deps.rsklabs.io" } @@ -252,8 +400,6 @@ javadoc { options.encoding = "UTF-8" } -def generatedResources = "$buildDir/generated-resources" - publishing { publications { rskj(MavenPublication) { @@ -275,8 +421,9 @@ task generateResources { def buildInfoFile = 'build-info.properties' doLast { - mkdir generatedResources - def generated = new File(generatedResources as String, buildInfoFile) + def generatedDir = new File(buildDir, 'generatedResources') + mkdir(generatedDir) + def generated = new File(generatedDir, buildInfoFile) def commitHash = gitCommitHash() def currentBranch = gitCurrentBranch() generated.text = """ @@ -297,7 +444,7 @@ task javadocJar(type: Jar) { } jar { - dependsOn 'generateResources' + dependsOn generateResources def commitHash = gitCommitHash() def currentBranch = gitCurrentBranch() manifest { @@ -306,7 +453,7 @@ jar { } from sourceSets.main.output.classesDirs from sourceSets.main.output.resourcesDir - from generatedResources + from new File(buildDir, 'generatedResources') // Reference the generated resources directory } task generatePom(dependsOn: jar) { @@ -417,3 +564,10 @@ static def amendPathIfNeeded(details) { details.path = newPath } } + +tasks.named('check').configure { + dependsOn 'checkstyleMain' + dependsOn 'checkstyleTest' + dependsOn 'checkstyleJmh' + dependsOn 'checkstyleIntegrationTest' +} \ No newline at end of file