From 7d92a265f63b38ea4c968821e74cc0ab1e4f30a5 Mon Sep 17 00:00:00 2001 From: Alexey Tsvetkov <654232+AlexeyTsvetkov@users.noreply.github.com> Date: Thu, 21 Apr 2022 11:25:31 +0300 Subject: [PATCH] Allow including kotlinx.serialization.* into Gradle plugin (#2020) --- gradle-plugins/compose/build.gradle.kts | 40 +++++++++++++++++-------- 1 file changed, 27 insertions(+), 13 deletions(-) diff --git a/gradle-plugins/compose/build.gradle.kts b/gradle-plugins/compose/build.gradle.kts index 76a4e2b039..28e819a308 100644 --- a/gradle-plugins/compose/build.gradle.kts +++ b/gradle-plugins/compose/build.gradle.kts @@ -112,21 +112,35 @@ val checkJar by tasks.registering { doLast { val file = jar.get().archiveFile.get().asFile ZipFile(file).use { zip -> - val nonJbComposeClasses = zip.entries().asIterator().asSequence().filter { - !it.isDirectory - && it.name.endsWith(".class") - && !it.name.startsWith("org/jetbrains/compose") - }.toList() - if (nonJbComposeClasses.any()) { - error(buildString { - appendLine("Some classes from $file are not from 'org.jetbrains.compose' package:") - for (entry in nonJbComposeClasses) { - appendLine(" * ${entry.name}") - } - }) - } + checkJarContainsExpectedPackages(zip) + } + } +} + +// we want to avoid accidentally including unexpected jars/packages, e.g kotlin-stdlib etc +fun checkJarContainsExpectedPackages(jar: ZipFile) { + val expectedPackages = arrayOf( + "org/jetbrains/compose", + "kotlinx/serialization" + ) + val unexpectedClasses = arrayListOf() + + for (entry in jar.entries()) { + if (entry.isDirectory || !entry.name.endsWith(".class")) continue + + if (expectedPackages.none { prefix -> entry.name.startsWith(prefix) }) { + unexpectedClasses.add(entry.name) } } + + if (unexpectedClasses.any()) { + error(buildString { + appendLine("Some classes from ${jar.name} are not from 'org.jetbrains.compose' package:") + unexpectedClasses.forEach { + appendLine(" * $it") + } + }) + } } tasks.check {