diff --git a/gradle-plugin/src/main/kotlin/tech/mappie/MappieGradlePlugin.kt b/gradle-plugin/src/main/kotlin/tech/mappie/MappieGradlePlugin.kt index cd9f65e9..b3f13307 100644 --- a/gradle-plugin/src/main/kotlin/tech/mappie/MappieGradlePlugin.kt +++ b/gradle-plugin/src/main/kotlin/tech/mappie/MappieGradlePlugin.kt @@ -3,6 +3,7 @@ package tech.mappie import org.gradle.api.Project +import org.gradle.api.artifacts.Dependency import org.gradle.api.provider.Provider import org.jetbrains.kotlin.gradle.plugin.* import java.util.* @@ -11,25 +12,28 @@ class MappieGradlePlugin : KotlinCompilerPluginSupportPlugin { override fun apply(target: Project) { target.extensions.create("mappie", MappieExtension::class.java) - checkCompatibility(target) + target.checkCompatibility() } override fun applyToCompilation(kotlinCompilation: KotlinCompilation<*>): Provider> { - val extension = kotlinCompilation.project.extensions.getByType(MappieExtension::class.java) - kotlinCompilation.project.logger.info("Mappie plugin ${getPluginArtifact().version} applied") - return kotlinCompilation.target.project.provider { - buildList { - extension.warningsAsErrors.orNull?.apply { - add(SubpluginOption("warningsAsErrors", this.toString())) - } - extension.useDefaultArguments.orNull?.apply { - add(SubpluginOption("useDefaultArguments", this.toString())) - } - extension.strictness.enums.orNull?.apply { - add(SubpluginOption("strictness.enums", this.toString() )) - } - extension.strictness.visibility.orNull?.apply { - add(SubpluginOption("strictness.visibility", this.toString())) + with (kotlinCompilation.project) { + logger.info("Mappie plugin ${getPluginArtifact().version} applied") + + val extension = extensions.getByType(MappieExtension::class.java) + return provider { + buildList { + extension.warningsAsErrors.orNull?.apply { + add(SubpluginOption("warningsAsErrors", this.toString())) + } + extension.useDefaultArguments.orNull?.apply { + add(SubpluginOption("useDefaultArguments", this.toString())) + } + extension.strictness.enums.orNull?.apply { + add(SubpluginOption("strictness.enums", this.toString())) + } + extension.strictness.visibility.orNull?.apply { + add(SubpluginOption("strictness.visibility", this.toString())) + } } } } @@ -47,12 +51,27 @@ class MappieGradlePlugin : KotlinCompilerPluginSupportPlugin { ) override fun isApplicable(kotlinCompilation: KotlinCompilation<*>) = - kotlinCompilation.target.project.plugins.hasPlugin(MappieGradlePlugin::class.java) + kotlinCompilation.target.project.run { + hasMappiePlugin() && hasMappieDependency(kotlinCompilation) + } + + private fun Project.hasMappiePlugin() = + plugins.hasPlugin(MappieGradlePlugin::class.java) + + private fun Project.hasMappieDependency(kotlinCompilation: KotlinCompilation<*>): Boolean = + runCatching { getMappieDependency(kotlinCompilation) != null }.getOrElse { + logger.warn("Mappie could not determine runtime dependency configuration name and is not applied.") + false + } + + private fun Project.getMappieDependency(kotlinCompilation: KotlinCompilation<*>): Dependency? = + configurations.getByName(kotlinCompilation.runtimeDependencyConfigurationName ?: "implementation") + .allDependencies.first { it.group == "tech.mappie" && it.name == "mappie-api" } - private fun checkCompatibility(target: Project) { - val version = target.getKotlinPluginVersion() + private fun Project.checkCompatibility() { + val version = getKotlinPluginVersion() if (version !in SUPPORTED_KOTLIN_VERSIONS) { - target.logger.warn("Mappie: unsupported Kotlin version $version. Expected one of ${SUPPORTED_KOTLIN_VERSIONS.joinToString()}. This may lead to compilation failure.") + logger.warn("Mappie unsupported Kotlin version $version. Expected one of ${SUPPORTED_KOTLIN_VERSIONS.joinToString()}. This may lead to compilation failure.") } }