From dbe7a607b46e98be25301ec3346f7a2b4dad1c1c Mon Sep 17 00:00:00 2001 From: Jens Klingenberg Date: Mon, 27 May 2024 18:49:22 +0200 Subject: [PATCH] Add min version check (#564) --- .../ktorfit/gradle/KtorfitCompilerSubPlugin.kt | 8 ++++++++ .../ktorfit/gradle/KtorfitGradlePlugin.kt | 12 ++++++++++++ 2 files changed, 20 insertions(+) diff --git a/ktorfit-gradle-plugin/src/main/java/de/jensklingenberg/ktorfit/gradle/KtorfitCompilerSubPlugin.kt b/ktorfit-gradle-plugin/src/main/java/de/jensklingenberg/ktorfit/gradle/KtorfitCompilerSubPlugin.kt index b80f13554..26603eccf 100644 --- a/ktorfit-gradle-plugin/src/main/java/de/jensklingenberg/ktorfit/gradle/KtorfitCompilerSubPlugin.kt +++ b/ktorfit-gradle-plugin/src/main/java/de/jensklingenberg/ktorfit/gradle/KtorfitCompilerSubPlugin.kt @@ -4,6 +4,7 @@ import de.jensklingenberg.ktorfit.gradle.KtorfitGradlePlugin.Companion.ARTIFACT_ import de.jensklingenberg.ktorfit.gradle.KtorfitGradlePlugin.Companion.COMPILER_PLUGIN_ID import de.jensklingenberg.ktorfit.gradle.KtorfitGradlePlugin.Companion.GROUP_NAME import de.jensklingenberg.ktorfit.gradle.KtorfitGradlePlugin.Companion.KTORFIT_VERSION +import de.jensklingenberg.ktorfit.gradle.KtorfitGradlePlugin.Companion.MIN_KOTLIN_VERSION import de.jensklingenberg.ktorfit.gradle.KtorfitGradlePlugin.Companion.SNAPSHOT import org.gradle.api.Project import org.gradle.api.provider.Provider @@ -42,10 +43,17 @@ internal class KtorfitCompilerSubPlugin : KotlinCompilerPluginSupportPlugin { @OptIn(ExperimentalKotlinGradlePluginApi::class) override fun getPluginArtifact(): SubpluginArtifact { + checkKotlinVersion(myproject.kotlinExtension.compilerVersion.get()) return SubpluginArtifact( groupId = GROUP_NAME, artifactId = ARTIFACT_NAME, version = "${KTORFIT_VERSION}-${myproject.kotlinExtension.compilerVersion.get()}${SNAPSHOT}" ) } + + private fun checkKotlinVersion(compilerVersion: String) { + if(compilerVersion.split(".")[0] < MIN_KOTLIN_VERSION.split(".")[0]){ + error("Ktorfit: Kotlin version $compilerVersion is not supported. You need at least version $MIN_KOTLIN_VERSION") + } + } } diff --git a/ktorfit-gradle-plugin/src/main/java/de/jensklingenberg/ktorfit/gradle/KtorfitGradlePlugin.kt b/ktorfit-gradle-plugin/src/main/java/de/jensklingenberg/ktorfit/gradle/KtorfitGradlePlugin.kt index e67fc2432..06204abcf 100644 --- a/ktorfit-gradle-plugin/src/main/java/de/jensklingenberg/ktorfit/gradle/KtorfitGradlePlugin.kt +++ b/ktorfit-gradle-plugin/src/main/java/de/jensklingenberg/ktorfit/gradle/KtorfitGradlePlugin.kt @@ -7,6 +7,7 @@ import org.gradle.kotlin.dsl.findByType import org.jetbrains.kotlin.gradle.dsl.KotlinMultiplatformExtension import org.jetbrains.kotlin.gradle.dsl.KotlinProjectExtension import org.jetbrains.kotlin.gradle.dsl.KotlinSingleTargetExtension +import org.jetbrains.kotlin.gradle.dsl.kotlinExtension import org.jetbrains.kotlin.gradle.tasks.KotlinCompilationTask import java.util.Locale.US @@ -18,6 +19,8 @@ class KtorfitGradlePlugin : Plugin { const val COMPILER_PLUGIN_ID = "ktorfitPlugin" const val KTORFIT_VERSION = "2.0.0" // remember to bump this version before any release! const val SNAPSHOT = "-SNAPSHOT" + const val MIN_KSP_VERSION = "1.0.21" + const val MIN_KOTLIN_VERSION = "2.0.0" } override fun apply(project: Project) { @@ -36,6 +39,8 @@ class KtorfitGradlePlugin : Plugin { val kspVersion = kspPlugin.javaClass.protectionDomain.codeSource.location.toURI().toString() .substringAfterLast("-").substringBefore(".jar") + checkKSPVersion(kspVersion) + val kspExtension = extensions.findByName("ksp") ?: error("KSP config not found") val argMethod = kspExtension.javaClass.getMethod("arg", String::class.java, String::class.java) @@ -94,6 +99,13 @@ class KtorfitGradlePlugin : Plugin { } + private fun checkKSPVersion(kspVersion: String) { + val kspVersionParts = kspVersion.split(".") + if (kspVersionParts[2].toInt() < MIN_KSP_VERSION.split(".")[2].toInt()) { + error("Ktorfit: KSP version $kspVersion is not supported. You need at least version $MIN_KSP_VERSION") + } + } + } private val Project.kotlinExtension: KotlinProjectExtension?