Skip to content

Commit 80a9f57

Browse files
committed
Update Kotlin compile options and enhance integration test compatibility: change JVM target configuration in build.gradle.kts to use the new compilerOptions syntax, and modify PluginIntegrationTest.kt to support AGP version compatibility with updated Kotlin version handling and additional command-line arguments for Gradle execution.
1 parent e3f5aa6 commit 80a9f57

File tree

2 files changed

+80
-19
lines changed

2 files changed

+80
-19
lines changed

plugin/build.gradle.kts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import org.gradle.api.tasks.SourceSetContainer
22
import org.gradle.api.tasks.testing.Test
3+
import org.jetbrains.kotlin.gradle.dsl.JvmTarget
4+
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
35

46
plugins {
57
kotlin("jvm") version "2.0.21"
@@ -45,8 +47,10 @@ dependencies {
4547
testImplementation(gradleTestKit())
4648
}
4749

48-
tasks.withType<org.jetbrains.kotlin.gradle.tasks.KotlinCompile>().configureEach {
49-
kotlinOptions.jvmTarget = "17"
50+
tasks.withType<KotlinCompile>().configureEach {
51+
compilerOptions {
52+
jvmTarget.set(JvmTarget.JVM_17)
53+
}
5054
}
5155

5256
jacoco {

plugin/src/test/kotlin/dev/vyp/stringcare/plugin/integration/PluginIntegrationTest.kt

Lines changed: 74 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ class PluginIntegrationTest {
4242
): org.gradle.testkit.runner.BuildResult {
4343
return GradleRunner.create()
4444
.withProjectDir(projectDir)
45-
.withArguments(*args, "--stacktrace")
45+
.withArguments(*args, "--stacktrace", "--no-scan")
4646
.forwardOutput()
4747
.build()
4848
}
@@ -55,7 +55,7 @@ class PluginIntegrationTest {
5555
val runner =
5656
GradleRunner.create()
5757
.withProjectDir(projectDir)
58-
.withArguments(*args, "--stacktrace")
58+
.withArguments(*args, "--stacktrace", "--no-scan")
5959
.forwardOutput()
6060
.withGradleVersion(gradleVersion)
6161
return runner.build()
@@ -77,30 +77,87 @@ class PluginIntegrationTest {
7777
return temp
7878
}
7979

80-
private fun setAgpVersion(
80+
private fun setCompatibilityVersions(
8181
copiedRepo: File,
82-
version: String,
82+
agpVersion: String,
83+
kotlinVersion: String? = null,
8384
) {
8485
val versionsFile = copiedRepo.resolve("gradle/libs.versions.toml")
85-
val updated = versionsFile.readText().replace(Regex("""^agp = ".*"$""", RegexOption.MULTILINE), """agp = "$version"""")
86-
versionsFile.writeText(updated)
86+
val withAgp =
87+
versionsFile
88+
.readText()
89+
.replace(Regex("""^agp = ".*"$""", RegexOption.MULTILINE), """agp = "$agpVersion"""")
90+
val withKotlin =
91+
if (kotlinVersion != null) {
92+
withAgp.replace(Regex("""^kotlin = ".*"$""", RegexOption.MULTILINE), """kotlin = "$kotlinVersion"""")
93+
} else {
94+
withAgp
95+
}
96+
versionsFile.writeText(withKotlin)
97+
98+
if (kotlinVersion != null) {
99+
val pluginBuild = copiedRepo.resolve("plugin/build.gradle.kts")
100+
pluginBuild.writeText(
101+
pluginBuild
102+
.readText()
103+
.replace("""kotlin("jvm") version "2.0.21"""", """kotlin("jvm") version "$kotlinVersion"""")
104+
.replace("""kotlin("plugin.serialization") version "2.0.21"""", """kotlin("plugin.serialization") version "$kotlinVersion""""),
105+
)
106+
}
87107
}
88108

89109
private fun assertAppConfiguresWithAgp(version: String) {
90110
val copiedRepo = copyRepoForAgpCompatibilityTest()
91-
setAgpVersion(copiedRepo, version)
92-
val gradleVersion =
111+
try {
112+
val gradleVersion: String?
93113
when (version) {
94-
"9.0.0" -> "9.1.0"
95-
else -> null
96-
}
97-
val result =
98-
if (gradleVersion == null) {
99-
runBuild(copiedRepo, ":app:help")
100-
} else {
101-
runBuildWithGradleVersion(copiedRepo, gradleVersion, ":app:help")
114+
"9.0.0" -> {
115+
gradleVersion = "9.1.0"
116+
setCompatibilityVersions(copiedRepo, agpVersion = version, kotlinVersion = "2.2.0")
117+
adaptProjectForAgp9(copiedRepo)
118+
}
119+
else -> {
120+
gradleVersion = null
121+
setCompatibilityVersions(copiedRepo, agpVersion = version)
122+
}
102123
}
103-
assertTrue(result.output.contains("BUILD SUCCESSFUL"))
124+
val result =
125+
if (gradleVersion == null) {
126+
runBuild(copiedRepo, ":app:help")
127+
} else {
128+
runBuildWithGradleVersion(copiedRepo, gradleVersion, ":app:help")
129+
}
130+
assertTrue(result.output.contains("BUILD SUCCESSFUL"))
131+
} finally {
132+
copiedRepo.deleteRecursively()
133+
}
134+
}
135+
136+
private fun adaptProjectForAgp9(copiedRepo: File) {
137+
listOf("app/build.gradle.kts", "library/build.gradle.kts").forEach { relative ->
138+
val file = copiedRepo.resolve(relative)
139+
if (!file.exists()) return@forEach
140+
var text = file.readText()
141+
text = text.replace(Regex("""^\s*alias\(libs\.plugins\.kotlin\.android\)\s*$""", RegexOption.MULTILINE), "")
142+
text = text.replace(Regex("""^\s*id\("org\.jetbrains\.kotlin\.android"\)\s*$""", RegexOption.MULTILINE), "")
143+
text = text.replace(Regex("""^\s*kotlin\("android"\)\s*$""", RegexOption.MULTILINE), "")
144+
text = text.replace(Regex("""^\s*targetSdk\s*=.*$""", RegexOption.MULTILINE), "")
145+
text = text.replace(Regex("""(?s)\n\s*kotlinOptions\s*\{.*?\}\n"""), "\n")
146+
text =
147+
text
148+
.replace(
149+
"""
150+
kotlinOptions {
151+
jvmTarget = "17"
152+
}
153+
""".trimIndent(),
154+
"",
155+
).replace(
156+
"kotlinOptions {\r\n jvmTarget = \"17\"\r\n }\r\n",
157+
"",
158+
)
159+
file.writeText(text)
160+
}
104161
}
105162

106163
@Test

0 commit comments

Comments
 (0)