Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support the alternative usage of kapt plugin #481

Merged
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ data class UnusedPluginFinding(
override val buildFile: File,
override val findingName: String,
val pluginId: String,
val alternatePluginId: String = "",
val kotlinPluginFunction: String = ""
) : Finding, Problem, Fixable, Deletable {

Expand All @@ -53,8 +54,10 @@ data class UnusedPluginFinding(
val row = lines
.indexOfFirst { line ->
line.contains("id(\"$pluginId\")") ||
line.contains("id(\"$alternatePluginId\")") ||
line.contains(kotlinPluginFunction) ||
line.contains("plugin = \"$pluginId\")")
line.contains("plugin = \"$pluginId\")") ||
line.contains("plugin = \"$alternatePluginId\")")
}

if (row < 0) return@lazyDeferred null
Expand All @@ -71,6 +74,9 @@ data class UnusedPluginFinding(
"id(\"$pluginId\")",
"id \"$pluginId\"",
"id '$pluginId'",
"id(\"$alternatePluginId\")",
"id \"$alternatePluginId\"",
"id '$alternatePluginId'",
kotlinPluginFunction
).firstNotNullOfOrNull { id ->
dependentProject.buildFileParser.pluginsBlock()?.getById(id)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import modulecheck.utils.LazySet
import modulecheck.utils.any

const val KAPT_PLUGIN_ID = "org.jetbrains.kotlin.kapt"
const val KAPT_ALTERNATE_PLUGIN_ID = "kotlin-kapt"
private const val KAPT_PLUGIN_FUN = "kotlin(\"kapt\")"

class UnusedKaptRule(
Expand Down Expand Up @@ -91,6 +92,7 @@ class UnusedKaptRule(
buildFile = project.buildFile,
findingName = "unusedKaptPlugin",
pluginId = KAPT_PLUGIN_ID,
alternatePluginId = KAPT_ALTERNATE_PLUGIN_ID,
kotlinPluginFunction = KAPT_PLUGIN_FUN
)
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,60 @@ class UnusedKaptProcessorTest : RunnerTest() {
)
}

@Test
fun `unused from kapt configuration with alternate plugin id without autoCorrect should fail`() {

val app = kotlinProject(":app") {
hasKapt = true

addExternalDependency(ConfigurationName.kapt, dagger)

buildFile {
"""
plugins {
id("kotlin-jvm")
id("kotlin-kapt")
}

dependencies {
kapt("$dagger")
}
"""
}
}

run(
autoCorrect = false
).isSuccess shouldBe false

app.buildFile shouldHaveText """
plugins {
id("kotlin-jvm")
id("kotlin-kapt")
}

dependencies {
kapt("$dagger")
}
"""

logger.parsedReport() shouldBe listOf(
":app" to listOf(
unusedKaptProcessor(
fixed = false,
configuration = "kapt",
dependency = "com.google.dagger:dagger-compiler",
position = "7, 3"
),
unusedKaptPlugin(
fixed = false,
dependency = "org.jetbrains.kotlin.kapt",
position = "3, 3"
)
)
)
}

@Test
fun `unused from non-kapt configuration without autoCorrect should pass without changes`() {

Expand Down Expand Up @@ -273,4 +327,56 @@ class UnusedKaptProcessorTest : RunnerTest() {
)
)
}

@Test
fun `unused with main kapt with alternate plugin id with autoCorrect and no other processors should remove processor and plugin`() {

val app = kotlinProject(":app") {
hasKapt = true

addExternalDependency(ConfigurationName.kapt, dagger)

buildFile {
"""
plugins {
id("kotlin-jvm")
id("kotlin-kapt")
}

dependencies {
kapt("$dagger")
}
"""
}
}

run().isSuccess shouldBe true

app.buildFile shouldHaveText """
plugins {
id("kotlin-jvm")
// id("kotlin-kapt") // ModuleCheck finding [unusedKaptPlugin]
}

dependencies {
// kapt("com.google.dagger:dagger-compiler:2.40.5") // ModuleCheck finding [unusedKaptProcessor]
}
"""

logger.parsedReport() shouldBe listOf(
":app" to listOf(
unusedKaptProcessor(
fixed = true,
configuration = "kapt",
dependency = "com.google.dagger:dagger-compiler",
position = "7, 3"
),
unusedKaptPlugin(
fixed = true,
dependency = "org.jetbrains.kotlin.kapt",
position = "3, 3"
)
)
)
}
}