Skip to content

Commit 5527be0

Browse files
authored
Add compiler plugin dependency only for projects with KGP >= 1.6.20 (#226)
* Add compiler plugin dependency only for projects with KGP >= 1.6.20 Fixes #225 * fixup
1 parent 450b056 commit 5527be0

File tree

1 file changed

+19
-10
lines changed

1 file changed

+19
-10
lines changed

atomicfu-gradle-plugin/src/main/kotlin/kotlinx/atomicfu/plugin/gradle/AtomicFUGradlePlugin.kt

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ open class AtomicFUGradlePlugin : Plugin<Project> {
3434
val pluginVersion = rootProject.buildscript.configurations.findByName("classpath")
3535
?.allDependencies?.find { it.name == "atomicfu-gradle-plugin" }?.version
3636
extensions.add(EXTENSION_NAME, AtomicFUPluginExtension(pluginVersion))
37-
if (rootProject.getBooleanProperty(ENABLE_IR_TRANSFORMATION)) {
37+
if (rootProject.getBooleanProperty(ENABLE_IR_TRANSFORMATION) && isCompilerPluginAvailable()) {
3838
plugins.apply(AtomicfuKotlinGradleSubplugin::class.java)
3939
}
4040
configureDependencies()
@@ -86,6 +86,15 @@ private fun Project.configureTasks() {
8686
}
8787
}
8888

89+
private fun Project.isCompilerPluginAvailable(): Boolean {
90+
// kotlinx-atomicfu compiler plugin is available for KGP >= 1.6.20
91+
val (majorVersion, minorVersion, patch) = getKotlinPluginVersion()
92+
.split('.')
93+
.take(3)
94+
.map { it.toInt() }
95+
return majorVersion == 1 && (minorVersion == 6 && patch >= 20 || minorVersion > 6)
96+
}
97+
8998
private fun Project.getBooleanProperty(name: String) =
9099
rootProject.findProperty(name)?.toString()?.toBooleanStrict() ?: false
91100

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

103112
private fun Project.addCompilerPluginDependency() {
104-
val kotlinVersion = rootProject.buildscript.configurations.findByName("classpath")
105-
?.allDependencies?.find { it.name == "kotlin-gradle-plugin" }?.version
106-
withKotlinTargets { target ->
107-
if (needsJsIrTransformation(target)) {
108-
target.compilations.forEach { kotlinCompilation ->
109-
kotlinCompilation.dependencies {
110-
// add atomicfu compiler plugin dependency
111-
// to provide the `kotlinx-atomicfu-runtime` library used during compiler plugin transformation
112-
compileOnly("org.jetbrains.kotlin:atomicfu:$kotlinVersion")
113+
if (isCompilerPluginAvailable()) {
114+
withKotlinTargets { target ->
115+
if (needsJsIrTransformation(target)) {
116+
target.compilations.forEach { kotlinCompilation ->
117+
kotlinCompilation.dependencies {
118+
// add atomicfu compiler plugin dependency
119+
// to provide the `kotlinx-atomicfu-runtime` library used during compiler plugin transformation
120+
compileOnly("org.jetbrains.kotlin:atomicfu:${getKotlinPluginVersion()}")
121+
}
113122
}
114123
}
115124
}

0 commit comments

Comments
 (0)