From 2beb1d93b26a97883eab625035ec82bef530dd7e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Javier=20Segovia=20C=C3=B3rdoba?= Date: Mon, 26 Aug 2024 02:37:49 +0200 Subject: [PATCH] Add report Kopy uses a different Kotlin version than the project --- CHANGELOG.md | 1 + .../kopy/gradle/plugin/KopyGradlePlugin.kt | 25 +++++++++++++++++-- 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index fa3b37c..3d8ac23 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ ### Added - `KopyFunctions` to set the functions that will be generated +- report Kopy uses a different Kotlin version than the project ### Changed diff --git a/kopy-gradle-plugin/main/kotlin/com/javiersc/kotlin/kopy/gradle/plugin/KopyGradlePlugin.kt b/kopy-gradle-plugin/main/kotlin/com/javiersc/kotlin/kopy/gradle/plugin/KopyGradlePlugin.kt index 21867e1..993ccb3 100644 --- a/kopy-gradle-plugin/main/kotlin/com/javiersc/kotlin/kopy/gradle/plugin/KopyGradlePlugin.kt +++ b/kopy-gradle-plugin/main/kotlin/com/javiersc/kotlin/kopy/gradle/plugin/KopyGradlePlugin.kt @@ -1,8 +1,10 @@ package com.javiersc.kotlin.kopy.gradle.plugin +import com.javiersc.gradle.logging.extensions.warnColored import com.javiersc.kotlin.kopy.args.KopyFunctions import com.javiersc.kotlin.kopy.args.KopyVisibility import com.javiersc.kotlin.kopy.compiler.KopyCompilerProjectData +import com.javiersc.kotlin.stdlib.remove import javax.inject.Inject import org.gradle.api.Project import org.gradle.api.provider.Provider @@ -16,6 +18,7 @@ import org.jetbrains.kotlin.gradle.plugin.KotlinCompilation import org.jetbrains.kotlin.gradle.plugin.KotlinCompilerPluginSupportPlugin import org.jetbrains.kotlin.gradle.plugin.SubpluginArtifact import org.jetbrains.kotlin.gradle.plugin.SubpluginOption +import org.jetbrains.kotlin.gradle.plugin.getKotlinPluginVersion public class KopyGradlePlugin @Inject @@ -23,9 +26,16 @@ constructor( private val providers: ProviderFactory, ) : KotlinCompilerPluginSupportPlugin { + private val Project.kopy: KopyExtension + get() = the() + + override fun apply(target: Project) { target.createExtension() - target.withKotlin { suppressKopyOptInt() } + target.withKotlin { + suppressKopyOptInt() + reportKotlinVersion() + } target.withKotlinAndroid { dependencies { "api"(kopyRuntime) } } target.withKotlinJvm { dependencies { "api"(kopyRuntime) } } target.withKotlinMultiplatform { dependencies { "commonMainApi"(kopyRuntime) } } @@ -34,7 +44,7 @@ constructor( override fun applyToCompilation( kotlinCompilation: KotlinCompilation<*> ): Provider> { - val kopy: KopyExtension = kotlinCompilation.project.the() + val kopy: KopyExtension = kotlinCompilation.project.kopy return providers.provider { listOf( SubpluginOption( @@ -65,6 +75,17 @@ constructor( extensions.create(KopyExtension.NAME) } + private fun Project.reportKotlinVersion() { + val kopyVersion: String = + KopyCompilerProjectData.Version.replaceBeforeLast("+", "").remove("+") + val kotlinVersion: String = getKotlinPluginVersion() + if (kopyVersion == kotlinVersion) return + logger.warnColored { + "Kopy uses a different Kotlin version, $kopyVersion, than the Kotlin Gradle plugin " + + "applied to this project, $kotlinVersion. It may not work as expected." + } + } + private fun Project.withKotlinAndroid(action: Project.() -> Unit) { pluginManager.withPlugin("org.jetbrains.kotlin.android") { action() } }