Skip to content

Commit

Permalink
Added check if mappie-api is applied
Browse files Browse the repository at this point in the history
  • Loading branch information
stefankoppier committed Jul 31, 2024
1 parent 0e2e789 commit 55984b3
Showing 1 changed file with 39 additions and 20 deletions.
59 changes: 39 additions & 20 deletions gradle-plugin/src/main/kotlin/tech/mappie/MappieGradlePlugin.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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.*
Expand All @@ -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<List<SubpluginOption>> {
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()))
}
}
}
}
Expand All @@ -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.")
}
}

Expand Down

0 comments on commit 55984b3

Please sign in to comment.