Skip to content

Commit

Permalink
[gradle] Log information about min Kotlin and Gradle supported versio…
Browse files Browse the repository at this point in the history
…ns for KMP resources
  • Loading branch information
terrakok committed Mar 8, 2024
1 parent 720575e commit 70609cd
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 82 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,11 @@ import org.gradle.api.file.FileSystemOperations
import org.gradle.api.provider.Property
import org.gradle.api.provider.Provider
import org.gradle.api.tasks.*
import org.gradle.util.GradleVersion
import org.jetbrains.compose.ComposePlugin
import org.jetbrains.compose.desktop.application.internal.ComposeProperties
import org.jetbrains.compose.experimental.uikit.internal.utils.isIosTarget
import org.jetbrains.compose.internal.KOTLIN_JVM_PLUGIN_ID
import org.jetbrains.compose.internal.KOTLIN_MPP_PLUGIN_ID
import org.jetbrains.compose.internal.utils.*
import org.jetbrains.compose.internal.utils.registerTask
import org.jetbrains.compose.internal.utils.uppercaseFirstChar
import org.jetbrains.compose.resources.ios.getSyncResourcesTaskName
Expand All @@ -33,6 +32,7 @@ import javax.inject.Inject
private const val COMPOSE_RESOURCES_DIR = "composeResources"
private const val RES_GEN_DIR = "generated/compose/resourceGenerator"
private const val KMP_RES_EXT = "multiplatformResourcesPublication"
private const val MIN_GRADLE_VERSION_FOR_KMP_RESOURCES = "7.6"
private val androidPluginIds = listOf(
"com.android.application",
"com.android.library"
Expand All @@ -50,9 +50,28 @@ internal fun Project.configureComposeResources() {
val kotlinExtension = project.extensions.getByType(KotlinMultiplatformExtension::class.java)

val hasKmpResources = extraProperties.has(KMP_RES_EXT)
if (hasKmpResources) {
val currentGradleVersion = GradleVersion.current()
val minGradleVersion = GradleVersion.version(MIN_GRADLE_VERSION_FOR_KMP_RESOURCES)
if (hasKmpResources && currentGradleVersion >= minGradleVersion) {
configureKmpResources(kotlinExtension, extraProperties.get(KMP_RES_EXT)!!, projectId)
} else {
if (!hasKmpResources) {
logger.info(
"""
Compose resources publication requires Kotlin Gradle Plugin >= 2.0
Current Kotlin Gradle Plugin is ${KotlinVersion.CURRENT}
""".trimIndent()
)
}
if (currentGradleVersion < minGradleVersion) {
logger.info(
"""
Compose resources publication requires Gradle >= $MIN_GRADLE_VERSION_FOR_KMP_RESOURCES
Current Gradle is ${currentGradleVersion.version}
""".trimIndent()
)
}

//current KGP doesn't have KPM resources
configureComposeResources(kotlinExtension, KotlinSourceSet.COMMON_MAIN_SOURCE_SET_NAME, projectId)

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.jetbrains.compose.test.tests.integration

import org.gradle.util.GradleVersion
import org.jetbrains.compose.internal.utils.*
import org.jetbrains.compose.test.utils.*
import org.junit.jupiter.api.Test
Expand Down Expand Up @@ -134,94 +135,102 @@ class ResourcesTest : GradlePluginTestBase() {
}

@Test
fun testMultiModuleResources(): Unit = with(
testProject(
"misc/kmpResourcePublication",
defaultTestEnvironment.copy(
kotlinVersion = "2.0.0-titan-105",
composeCompilerPlugin = "2.0.0-Beta4"
)
fun testMultiModuleResources() {
val environment = defaultTestEnvironment.copy(
kotlinVersion = "2.0.0-titan-105",
composeCompilerPlugin = "2.0.0-Beta4"
)
) {
gradle(":cmplib:publishAllPublicationsToMavenRepository").checks {
val resDir = file("cmplib/src/commonMain/composeResources")
val resourcesFiles = resDir.walkTopDown()
.filter { !it.isDirectory && !it.isHidden }
.map { it.relativeTo(resDir).invariantSeparatorsPath }
val subdir = "me.sample.library.cmplib"

fun libpath(target: String, ext: String) =
"my-mvn/me/sample/library/cmplib-$target/1.0/cmplib-$target-1.0$ext"

val aar = file(libpath("android", ".aar"))
val innerClassesJar = aar.parentFile.resolve("aar-inner-classes.jar")
assertTrue(aar.exists(), "File not found: " + aar.path)
ZipFile(aar).use { zip ->
resourcesFiles
.filter { it.startsWith("font") }
.forEach { fontRes ->
assertNotNull(
zip.getEntry("assets/composeResources/$subdir/$fontRes"),
"Resource not found: '$fontRes' in aar '${aar.path}'"
)
}

innerClassesJar.writeBytes(
zip.getInputStream(zip.getEntry("classes.jar")).readBytes()
)
}
ZipFile(innerClassesJar).use { zip ->
resourcesFiles
.filterNot { it.startsWith("font") }
.forEach { res ->
assertNotNull(
zip.getEntry("composeResources/$subdir/$res"),
"Resource not found: '$res' in aar/classes.jar '${aar.path}'"
)
}
with(
testProject("misc/kmpResourcePublication", environment)
) {
if (environment.parsedGradleVersion < GradleVersion.version("7.6")) {
val output = gradle(":tasks").output
output.contains("Compose resources publication requires Gradle >= 7.6")
output.contains("Current Kotlin Gradle Plugin is ${environment.gradleVersion}")
return@with
}

val jar = file(libpath("jvm", ".jar"))
checkResourcesZip(jar, resourcesFiles, subdir)

val iosx64ResZip = file(libpath("iosx64", "-kotlin_resources.kotlin_resources.zip"))
checkResourcesZip(iosx64ResZip, resourcesFiles, subdir)
val iosarm64ResZip = file(libpath("iosarm64", "-kotlin_resources.kotlin_resources.zip"))
checkResourcesZip(iosarm64ResZip, resourcesFiles, subdir)
val iossimulatorarm64ResZip = file(libpath("iossimulatorarm64", "-kotlin_resources.kotlin_resources.zip"))
checkResourcesZip(iossimulatorarm64ResZip, resourcesFiles, subdir)
val jsResZip = file(libpath("js", "-kotlin_resources.kotlin_resources.zip"))
checkResourcesZip(jsResZip, resourcesFiles, subdir)
val wasmjsResZip = file(libpath("wasm-js", "-kotlin_resources.kotlin_resources.zip"))
checkResourcesZip(wasmjsResZip, resourcesFiles, subdir)
}
gradle(":cmplib:publishAllPublicationsToMavenRepository").checks {
val resDir = file("cmplib/src/commonMain/composeResources")
val resourcesFiles = resDir.walkTopDown()
.filter { !it.isDirectory && !it.isHidden }
.map { it.relativeTo(resDir).invariantSeparatorsPath }
val subdir = "me.sample.library.cmplib"

fun libpath(target: String, ext: String) =
"my-mvn/me/sample/library/cmplib-$target/1.0/cmplib-$target-1.0$ext"

val aar = file(libpath("android", ".aar"))
val innerClassesJar = aar.parentFile.resolve("aar-inner-classes.jar")
assertTrue(aar.exists(), "File not found: " + aar.path)
ZipFile(aar).use { zip ->
resourcesFiles
.filter { it.startsWith("font") }
.forEach { fontRes ->
assertNotNull(
zip.getEntry("assets/composeResources/$subdir/$fontRes"),
"Resource not found: '$fontRes' in aar '${aar.path}'"
)
}

innerClassesJar.writeBytes(
zip.getInputStream(zip.getEntry("classes.jar")).readBytes()
)
}
ZipFile(innerClassesJar).use { zip ->
resourcesFiles
.filterNot { it.startsWith("font") }
.forEach { res ->
assertNotNull(
zip.getEntry("composeResources/$subdir/$res"),
"Resource not found: '$res' in aar/classes.jar '${aar.path}'"
)
}
}

file("settings.gradle.kts").modify { content ->
content.replace("//include(\":appModule\")", "include(\":appModule\")")
}
val jar = file(libpath("jvm", ".jar"))
checkResourcesZip(jar, resourcesFiles, subdir)

val iosx64ResZip = file(libpath("iosx64", "-kotlin_resources.kotlin_resources.zip"))
checkResourcesZip(iosx64ResZip, resourcesFiles, subdir)
val iosarm64ResZip = file(libpath("iosarm64", "-kotlin_resources.kotlin_resources.zip"))
checkResourcesZip(iosarm64ResZip, resourcesFiles, subdir)
val iossimulatorarm64ResZip =
file(libpath("iossimulatorarm64", "-kotlin_resources.kotlin_resources.zip"))
checkResourcesZip(iossimulatorarm64ResZip, resourcesFiles, subdir)
val jsResZip = file(libpath("js", "-kotlin_resources.kotlin_resources.zip"))
checkResourcesZip(jsResZip, resourcesFiles, subdir)
val wasmjsResZip = file(libpath("wasm-js", "-kotlin_resources.kotlin_resources.zip"))
checkResourcesZip(wasmjsResZip, resourcesFiles, subdir)
}

gradle(":appModule:jvmTest", "-i")
gradle(":appModule:pixel5Check")
file("settings.gradle.kts").modify { content ->
content.replace("//include(\":appModule\")", "include(\":appModule\")")
}

gradle(":appModule:jvmTest", "-i")
gradle(":appModule:pixel5Check")

if (currentOS == OS.MacOS) {
val iosTask = if (currentArch == Arch.X64) {
":appModule:iosX64Test"
} else {
":appModule:iosSimulatorArm64Test"
if (currentOS == OS.MacOS) {
val iosTask = if (currentArch == Arch.X64) {
":appModule:iosX64Test"
} else {
":appModule:iosSimulatorArm64Test"
}
gradle(iosTask)
}
gradle(iosTask)
}

file("featureModule/src/commonMain/kotlin/me/sample/app/Feature.kt").modify { content ->
content.replace(
"Text(txt + stringResource(Res.string.str_1), modifier)",
"Text(stringResource(Res.string.str_1), modifier)"
)
}
file("featureModule/src/commonMain/kotlin/me/sample/app/Feature.kt").modify { content ->
content.replace(
"Text(txt + stringResource(Res.string.str_1), modifier)",
"Text(stringResource(Res.string.str_1), modifier)"
)
}

gradleFailure(":appModule:jvmTest").checks {
check.logContains("java.lang.AssertionError: Failed to assert the following: (Text + EditableText = [test text: Feature text str_1])")
check.logContains("Text = '[Feature text str_1]'")
gradleFailure(":appModule:jvmTest").checks {
check.logContains("java.lang.AssertionError: Failed to assert the following: (Text + EditableText = [test text: Feature text str_1])")
check.logContains("Text = '[Feature text str_1]'")
}
}
}

Expand Down
1 change: 1 addition & 0 deletions gradle-plugins/gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ compose.tests.compiler.compatible.kotlin.version=1.9.22
compose.tests.js.compiler.compatible.kotlin.version=1.9.22
# __SUPPORTED_GRADLE_VERSIONS__
# Don't forget to edit versions in .github/workflows/gradle-plugin.yml as well
# and Publish.Subtasks.buildTypes.gradle.GradlePluginTestKt#gradleVersions in the TC config
# minimal and current gradle version
#
# !!! we can't update to the gradle upper than 8.3 because of on issue:
Expand Down

0 comments on commit 70609cd

Please sign in to comment.