|
| 1 | +package io.github.podcastindex_sdk.gradle |
| 2 | + |
| 3 | +import org.gradle.api.Plugin |
| 4 | +import org.gradle.api.Project |
| 5 | +import org.gradle.api.tasks.compile.JavaCompile |
| 6 | +import org.gradle.kotlin.dsl.getByType |
| 7 | +import org.gradle.kotlin.dsl.withType |
| 8 | +import org.jetbrains.kotlin.gradle.ExperimentalKotlinGradlePluginApi |
| 9 | +import org.jetbrains.kotlin.gradle.dsl.JvmTarget |
| 10 | +import org.jetbrains.kotlin.gradle.dsl.KotlinMultiplatformExtension |
| 11 | +import org.jetbrains.kotlin.gradle.dsl.KotlinVersion |
| 12 | + |
| 13 | +class CompatibilityConventionPlugin : Plugin<Project> { |
| 14 | + |
| 15 | + override fun apply(target: Project) { |
| 16 | + with(target) { |
| 17 | + pluginManager.apply("org.jetbrains.kotlin.multiplatform") |
| 18 | + pluginManager.apply("dev.drewhamilton.poko") |
| 19 | + |
| 20 | + val kotlinExtension = extensions.getByType<KotlinMultiplatformExtension>() |
| 21 | + configureKotlinExtension(kotlinExtension) |
| 22 | + } |
| 23 | + } |
| 24 | + |
| 25 | + private fun Project.configureKotlinExtension( |
| 26 | + kotlinExtension: KotlinMultiplatformExtension |
| 27 | + ) { |
| 28 | + kotlinExtension.apply { |
| 29 | + explicitApi() |
| 30 | + jvmToolchain(libs.findVersion("jvmToolchain").get().toString().toInt()) |
| 31 | + |
| 32 | + jvm() |
| 33 | + listOf( |
| 34 | + iosX64(), |
| 35 | + iosArm64(), |
| 36 | + iosSimulatorArm64(), |
| 37 | + ).forEach { |
| 38 | + it.binaries.framework { |
| 39 | + baseName = when (this@configureKotlinExtension.name) { |
| 40 | + "podcastindex-sdk-base" -> "podcastindex-sdk" |
| 41 | + "podcastindex-ktor2" -> "podcastindex-sdk-ktor2" |
| 42 | + "podcastindex-ktor3" -> "podcastindex-sdk-ktor3" |
| 43 | + else -> error("Unknown project name: ${this@configureKotlinExtension.name}") |
| 44 | + } |
| 45 | + isStatic = true |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + val kotlinTargetVersion = KotlinVersion.fromVersion(libs.findVersion("kotlinTarget").get().toString().toKotlinMinor()) |
| 50 | + |
| 51 | + @OptIn(ExperimentalKotlinGradlePluginApi::class) |
| 52 | + compilerOptions { |
| 53 | + languageVersion.set(kotlinTargetVersion) |
| 54 | + apiVersion.set(kotlinTargetVersion) |
| 55 | + freeCompilerArgs.add(libs.findVersion("jdkTarget").get().toString().let { "-Xjdk-release=${it.toJdkTarget()}" }) |
| 56 | + } |
| 57 | + coreLibrariesVersion = libs.findVersion("kotlinTarget").get().toString() |
| 58 | + } |
| 59 | + |
| 60 | + configureCompilationTasks() |
| 61 | + } |
| 62 | + |
| 63 | + private fun Project.configureCompilationTasks() { |
| 64 | + tasks.withType<org.jetbrains.kotlin.gradle.tasks.KotlinCompile>().configureEach { |
| 65 | + compilerOptions { |
| 66 | + jvmTarget.set(JvmTarget.fromTarget(libs.findVersion("jdkTarget").get().toString().toJdkTarget())) |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + tasks.withType<JavaCompile>().configureEach { |
| 71 | + sourceCompatibility = libs.findVersion("jdkTarget").get().toString().toJdkTarget() |
| 72 | + targetCompatibility = libs.findVersion("jdkTarget").get().toString().toJdkTarget() |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + private fun String.toJdkTarget() = if (toInt() <= 8) "1.$this" else this |
| 77 | + |
| 78 | + private fun String.toKotlinMinor() = split(".").take(2).joinToString(".") |
| 79 | +} |
0 commit comments