Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add compiler plugin dependency only for projects with KGP >= 1.6.20 #226

Merged
merged 2 commits into from May 16, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -34,7 +34,7 @@ open class AtomicFUGradlePlugin : Plugin<Project> {
val pluginVersion = rootProject.buildscript.configurations.findByName("classpath")
?.allDependencies?.find { it.name == "atomicfu-gradle-plugin" }?.version
extensions.add(EXTENSION_NAME, AtomicFUPluginExtension(pluginVersion))
if (rootProject.getBooleanProperty(ENABLE_IR_TRANSFORMATION)) {
if (rootProject.getBooleanProperty(ENABLE_IR_TRANSFORMATION) && isCompilerPluginAvailable()) {
plugins.apply(AtomicfuKotlinGradleSubplugin::class.java)
}
configureDependencies()
Expand Down Expand Up @@ -86,6 +86,15 @@ private fun Project.configureTasks() {
}
}

private fun Project.isCompilerPluginAvailable(): Boolean {
// kotlinx-atomicfu compiler plugin is available for KGP >= 1.6.20
val (majorVersion, minorVersion, patch) = getKotlinPluginVersion()
.split('.')
.take(3)
.map { it.toInt() }
return majorVersion == 1 && (minorVersion == 6 && patch >= 20 || minorVersion > 6)
}

private fun Project.getBooleanProperty(name: String) =
rootProject.findProperty(name)?.toString()?.toBooleanStrict() ?: false

Expand All @@ -101,15 +110,15 @@ private fun Project.needsJsIrTransformation(target: KotlinTarget): Boolean =
private fun KotlinTarget.isJsIrTarget() = (this is KotlinJsTarget && this.irTarget != null) || this is KotlinJsIrTarget

private fun Project.addCompilerPluginDependency() {
val kotlinVersion = rootProject.buildscript.configurations.findByName("classpath")
?.allDependencies?.find { it.name == "kotlin-gradle-plugin" }?.version
withKotlinTargets { target ->
if (needsJsIrTransformation(target)) {
target.compilations.forEach { kotlinCompilation ->
kotlinCompilation.dependencies {
// add atomicfu compiler plugin dependency
// to provide the `kotlinx-atomicfu-runtime` library used during compiler plugin transformation
compileOnly("org.jetbrains.kotlin:atomicfu:$kotlinVersion")
if (isCompilerPluginAvailable()) {
withKotlinTargets { target ->
if (needsJsIrTransformation(target)) {
target.compilations.forEach { kotlinCompilation ->
kotlinCompilation.dependencies {
// add atomicfu compiler plugin dependency
// to provide the `kotlinx-atomicfu-runtime` library used during compiler plugin transformation
compileOnly("org.jetbrains.kotlin:atomicfu:${getKotlinPluginVersion()}")
}
}
}
}
Expand Down