Skip to content

Commit

Permalink
SONARKT-372 Improve the integration with Gradle Tooling API in the Gr…
Browse files Browse the repository at this point in the history
…adle rules (#400)
  • Loading branch information
leveretka committed Dec 13, 2023
1 parent 2a75938 commit 7f85757
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 35 deletions.
1 change: 0 additions & 1 deletion settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ dependencyResolutionManagement {
library("sonar-regex-parsing", "org.sonarsource.analyzer-commons", "sonar-regex-parsing").versionRef(analyzerCommons)
library("sonar-xml-parsing", "org.sonarsource.analyzer-commons", "sonar-xml-parsing").versionRef(analyzerCommons)
library("staxmate", "com.fasterxml.staxmate", "staxmate").versionRef(staxmate)
library("gradle-tooling-api", "org.gradle", "gradle-tooling-api").versionRef(gradleToolingApi)
}

create("utilLibs") {
Expand Down
1 change: 0 additions & 1 deletion sonar-kotlin-gradle/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ dependencies {
implementation(libs.staxmate)
implementation(libs.gson)
implementation(libs.sonar.analyzer.commons.recognizers)
implementation(libs.gradle.tooling.api)

implementation(project(":sonar-kotlin-api"))
implementation(project(":sonar-kotlin-surefire"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@
*/
package org.sonarsource.kotlin.gradle

import org.gradle.tooling.GradleConnector
import org.gradle.tooling.model.kotlin.dsl.KotlinDslScriptsModel
import org.jetbrains.kotlin.resolve.BindingContext
import org.slf4j.LoggerFactory
import org.sonar.api.batch.fs.FileSystem
Expand Down Expand Up @@ -69,23 +67,18 @@ class KotlinGradleSensor(
override fun getFilesToAnalyse(sensorContext: SensorContext): Iterable<InputFile> {
val fileSystem: FileSystem = sensorContext.fileSystem()

val rootDirFile = File(sensorContext.config()[GRADLE_PROJECT_ROOT_PROPERTY].get())
val mainFilePredicate = fileSystem.predicates().and(
fileSystem.predicates().or(
fileSystem.predicates().hasFilename("build.gradle.kts"),
fileSystem.predicates().hasFilename("settings.gradle.kts")
)
)

checkForMissingGradleSettings(rootDirFile, sensorContext)

val projectConnection = GradleConnector.newConnector()
.forProjectDirectory(rootDirFile)
.connect()

projectConnection.newBuild()
.forTasks("prepareKotlinBuildScriptModel")
.run()

val models = projectConnection.getModel(KotlinDslScriptsModel::class.java).scriptModels
return models.keys.mapNotNull { file ->
val predicate = fileSystem.predicates().hasAbsolutePath(file.absolutePath)
fileSystem.inputFile(predicate)
sensorContext.config()[GRADLE_PROJECT_ROOT_PROPERTY].ifPresent {
checkForMissingGradleSettings(File(it), sensorContext)
}

return fileSystem.inputFiles(mainFilePredicate)
}

private fun checkForMissingGradleSettings(rootDirFile: File, sensorContext: SensorContext) {
Expand Down
2 changes: 1 addition & 1 deletion sonar-kotlin-plugin/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ tasks.shadowJar {
exclude("org/jetbrains/kotlin/org/jline/**")
exclude("org/jetbrains/kotlin/net/jpountz/**")
doLast {
enforceJarSizeAndCheckContent(shadowJar.get().archiveFile.get().asFile, 39_500_000L, 41_000_000L)
enforceJarSizeAndCheckContent(shadowJar.get().archiveFile.get().asFile, 37_500_000L, 38_000_000L)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,7 @@ class KotlinPlugin : Plugin {
KotlinProfileDefinition::class.java,
)

context.bootConfiguration[GRADLE_PROJECT_ROOT_PROPERTY].ifPresent {
context.addExtension(KotlinGradleSensor::class.java)
}
context.addExtension(KotlinGradleSensor::class.java)

if (context.runtime.product != SonarProduct.SONARLINT) {
context.addExtensions(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,24 +36,15 @@ import java.util.Optional
@ExperimentalTime
internal class KotlinPluginTest {
@Test
fun testWithoutGradle() {
testSonarQube(17)
}

@Test
fun testWithGradle() {
testSonarQube(18, mapOf(GRADLE_PROJECT_ROOT_PROPERTY to "../"))
fun testSonarQube() {
testSonarQube(18)
}

@Test
fun testSonarLint() {
testSonarLint(4)
testSonarLint(5)
}

@Test
fun testSonarLintWithGradle() {
testSonarLint(5, mapOf(GRADLE_PROJECT_ROOT_PROPERTY to "../"))
}

private fun testSonarQube(expectedExtensionsCount: Int, overrideProperties: Map<String, String> = emptyMap()) {
val runtime = SonarRuntimeImpl.forSonarQube(Version.create(7, 9), SonarQubeSide.SCANNER, SonarEdition.COMMUNITY)
Expand Down

0 comments on commit 7f85757

Please sign in to comment.