From 81453866507d11249ef8ff1624a393330903bf7e Mon Sep 17 00:00:00 2001 From: Alexander Nozik Date: Tue, 17 Dec 2019 15:31:33 +0300 Subject: [PATCH] Remove project extension and replace library loading by extensions helpers. --- build.gradle.kts | 2 +- src/main/kotlin/Scientifik.kt | 4 +- .../scientifik/ScientifikAtomicPlugin.kt | 68 --------------- .../kotlin/scientifik/ScientifikExtension.kt | 82 ------------------- .../kotlin/scientifik/ScientifikJSPlugin.kt | 11 ++- .../kotlin/scientifik/ScientifikJVMPlugin.kt | 2 - .../kotlin/scientifik/ScientifikMPPlugin.kt | 2 - src/main/kotlin/scientifik/extensions.kt | 61 ++++++++++++++ 8 files changed, 71 insertions(+), 161 deletions(-) delete mode 100644 src/main/kotlin/scientifik/ScientifikAtomicPlugin.kt delete mode 100644 src/main/kotlin/scientifik/ScientifikExtension.kt create mode 100644 src/main/kotlin/scientifik/extensions.kt diff --git a/build.gradle.kts b/build.gradle.kts index 4f8ba16..7c63207 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -7,7 +7,7 @@ plugins { } group = "scientifik" -version = "0.2.7" +version = "0.3.0" repositories { gradlePluginPortal() diff --git a/src/main/kotlin/Scientifik.kt b/src/main/kotlin/Scientifik.kt index 7718b86..8f3c3e1 100644 --- a/src/main/kotlin/Scientifik.kt +++ b/src/main/kotlin/Scientifik.kt @@ -3,8 +3,8 @@ */ object Scientifik { const val ioVersion = "0.1.15" - const val coroutinesVersion = "1.3.2" - const val atomicfuVersion = "0.12.11" + const val coroutinesVersion = "1.3.3" + const val atomicfuVersion = "0.14.1" const val serializationVersion = "0.14.0" const val JVM_VERSION = "11" } diff --git a/src/main/kotlin/scientifik/ScientifikAtomicPlugin.kt b/src/main/kotlin/scientifik/ScientifikAtomicPlugin.kt deleted file mode 100644 index 36a72f3..0000000 --- a/src/main/kotlin/scientifik/ScientifikAtomicPlugin.kt +++ /dev/null @@ -1,68 +0,0 @@ -package scientifik - -import Scientifik -import org.gradle.api.Plugin -import org.gradle.api.Project -import org.gradle.kotlin.dsl.* -import org.jetbrains.kotlin.gradle.dsl.KotlinJsProjectExtension -import org.jetbrains.kotlin.gradle.dsl.KotlinJvmProjectExtension -import org.jetbrains.kotlin.gradle.dsl.KotlinMultiplatformExtension - -class ScientifikAtomicPlugin : Plugin { - override fun apply(target: Project) { - with(target) { - plugins.apply("kotlinx-atomicfu") - - pluginManager.withPlugin("org.jetbrains.kotlin.multiplatform") { - project.configure { - sourceSets.invoke { - val commonMain by getting { - dependencies { - implementation("org.jetbrains.kotlinx:atomicfu-common:${Scientifik.atomicfuVersion}") - } - } - - val jvmMain by getting { - dependencies { - implementation("org.jetbrains.kotlinx:atomicfu:${Scientifik.atomicfuVersion}") - } - } - val jsMain by getting { - dependencies { - implementation("org.jetbrains.kotlinx:atomicfu-common-js:${Scientifik.atomicfuVersion}") - } - } - val jsTest by getting { - dependencies { - implementation(kotlin("test-js")) - } - } - } - } - //TODO add native clause - } - - pluginManager.withPlugin("org.jetbrains.kotlin.jvm") { - configure { - sourceSets["main"].apply { - dependencies { - implementation("org.jetbrains.kotlinx:atomicfu:${Scientifik.atomicfuVersion}") - } - } - } - } - - pluginManager.withPlugin("org.jetbrains.kotlin.js") { - configure { - sourceSets["main"].apply { - dependencies { - implementation("org.jetbrains.kotlinx:atomicfu-js:${Scientifik.atomicfuVersion}") - } - } - } - } - } - } -} - - diff --git a/src/main/kotlin/scientifik/ScientifikExtension.kt b/src/main/kotlin/scientifik/ScientifikExtension.kt deleted file mode 100644 index 022b7e6..0000000 --- a/src/main/kotlin/scientifik/ScientifikExtension.kt +++ /dev/null @@ -1,82 +0,0 @@ -package scientifik - -import Scientifik -import org.gradle.api.Project -import org.gradle.kotlin.dsl.apply -import org.gradle.kotlin.dsl.create -import org.gradle.kotlin.dsl.findByType -import org.gradle.kotlin.dsl.get -import org.jetbrains.kotlin.gradle.dsl.KotlinJsProjectExtension -import org.jetbrains.kotlin.gradle.dsl.KotlinJvmProjectExtension -import org.jetbrains.kotlin.gradle.dsl.KotlinMultiplatformExtension - -private fun Project.applyMPPDependency(vararg pairs: Pair) { - val map = mapOf(*pairs) - extensions.findByType()?.apply { - pairs.forEach{(target,depString)-> - sourceSets[target].apply { - dependencies { - api(depString) - } - } - - } - } - extensions.findByType()?.apply { - sourceSets["main"].apply { - dependencies { - api(map["jvmMain"] ?: error("jvmMain dependency not found")) - } - } - } - - extensions.findByType()?.apply { - sourceSets["main"].apply { - dependencies { - api(map["jsMain"] ?: error("jsMain dependency not found")) - } - } - } -} - -open class ScientifikExtension { - fun Project.withDokka() { - apply(plugin = "org.jetbrains.dokka") - subprojects { - scientifik.apply { - withDokka() - } - } - } - - fun Project.withSerialization() { - apply(plugin = "kotlinx-serialization") - applyMPPDependency( - "commonMain" to "org.jetbrains.kotlinx:kotlinx-serialization-runtime-common:${Scientifik.serializationVersion}", - "jvmMain" to "org.jetbrains.kotlinx:kotlinx-serialization-runtime:${Scientifik.serializationVersion}", - "jsMain" to "org.jetbrains.kotlinx:kotlinx-serialization-runtime-js:${Scientifik.serializationVersion}" - ) - //recursively apply to all subprojecs - subprojects { - scientifik.apply { - withSerialization() - } - } - } - -// fun Project.withIO() { -// applyMPPDependency( -// "commonMain" to "org.jetbrains.kotlinx:kotlinx-io:${Scientifik.ioVersion}", -// "jvmMain" to "org.jetbrains.kotlinx:kotlinx-io-jvm:${Scientifik.ioVersion}", -// "jsMain" to "org.jetbrains.kotlinx:kotlinx-io-js:${Scientifik.ioVersion}" -// ) -// subprojects { -// scientifik.apply { -// withIO() -// } -// } -// } -} - -internal val Project.scientifik: ScientifikExtension - get() = extensions.findByType() ?: extensions.create("scientifik") \ No newline at end of file diff --git a/src/main/kotlin/scientifik/ScientifikJSPlugin.kt b/src/main/kotlin/scientifik/ScientifikJSPlugin.kt index 83c33d5..9475f4d 100644 --- a/src/main/kotlin/scientifik/ScientifikJSPlugin.kt +++ b/src/main/kotlin/scientifik/ScientifikJSPlugin.kt @@ -10,13 +10,9 @@ import java.io.File open class ScientifikJSPlugin : Plugin { override fun apply(project: Project) { - val extension = project.scientifik with(project) { plugins.apply("org.jetbrains.kotlin.js") - plugins.apply("kotlinx-serialization") - plugins.apply("kotlinx-atomicfu") - repositories.applyRepos() @@ -31,6 +27,13 @@ open class ScientifikJSPlugin : Plugin { api(kotlin("stdlib-js")) } } + + sourceSets["test"].apply { + languageSettings.applySettings() + dependencies { + implementation(kotlin("test-js")) + } + } } tasks.apply { diff --git a/src/main/kotlin/scientifik/ScientifikJVMPlugin.kt b/src/main/kotlin/scientifik/ScientifikJVMPlugin.kt index debf50d..cc6b2b8 100644 --- a/src/main/kotlin/scientifik/ScientifikJVMPlugin.kt +++ b/src/main/kotlin/scientifik/ScientifikJVMPlugin.kt @@ -16,8 +16,6 @@ import org.jetbrains.kotlin.gradle.tasks.KotlinCompile open class ScientifikJVMPlugin : Plugin { override fun apply(project: Project) { - val extension = project.scientifik - with(project) { plugins.apply("org.jetbrains.kotlin.jvm") diff --git a/src/main/kotlin/scientifik/ScientifikMPPlugin.kt b/src/main/kotlin/scientifik/ScientifikMPPlugin.kt index 31cc0d5..6e5680c 100644 --- a/src/main/kotlin/scientifik/ScientifikMPPlugin.kt +++ b/src/main/kotlin/scientifik/ScientifikMPPlugin.kt @@ -16,8 +16,6 @@ import java.io.File open class ScientifikMPPlugin : Plugin { override fun apply(project: Project) { - val extension = project.scientifik - project.run { plugins.apply("org.jetbrains.kotlin.multiplatform") diff --git a/src/main/kotlin/scientifik/extensions.kt b/src/main/kotlin/scientifik/extensions.kt new file mode 100644 index 0000000..2b3aa04 --- /dev/null +++ b/src/main/kotlin/scientifik/extensions.kt @@ -0,0 +1,61 @@ +package scientifik + +import Scientifik +import kotlinx.atomicfu.plugin.gradle.sourceSets +import org.gradle.api.Project +import org.gradle.kotlin.dsl.findByType +import org.gradle.kotlin.dsl.invoke +import org.jetbrains.kotlin.gradle.dsl.KotlinMultiplatformExtension +import org.jetbrains.kotlin.gradle.plugin.KotlinSourceSet + +internal fun Project.useDependency(vararg pairs: Pair) { + pluginManager.withPlugin("org.jetbrains.kotlin.multiplatform") { + extensions.findByType()?.apply { + sourceSets { + pairs.forEach { (name, dep)-> + findByName(name)?.apply { + dependencies { + implementation(dep) + } + } + } + } + } + } + pairs.find { it.first == "jvmMain" }?.let { dep -> + pluginManager.withPlugin("org.jetbrains.kotlin.jvm") { + sourceSets.findByName("main")?.apply { + this as KotlinSourceSet + dependencies { + implementation(dep) + } + } + } + } + + pairs.find { it.first == "jsMain" }?.let { dep -> + pluginManager.withPlugin("org.jetbrains.kotlin.js") { + sourceSets.findByName("main")?.apply { + this as KotlinSourceSet + dependencies { + implementation(dep) + } + } + } + } +} + + +fun Project.useSerialization(version: String = Scientifik.serializationVersion) = useDependency( + "commonMain" to "org.jetbrains.kotlinx:kotlinx-serialization-runtime-common:$version", + "jvmMain" to "org.jetbrains.kotlinx:kotlinx-serialization-runtime:$version", + "jsMain" to "org.jetbrains.kotlinx:kotlinx-serialization-runtime-js:$version", + "nativeMain" to "org.jetbrains.kotlinx:kotlinx-serialization-runtime-native:$version" +) + +fun Project.useCoroutines(version: String = Scientifik.coroutinesVersion) = useDependency( + "commonMain" to "org.jetbrains.kotlinx:kotlinx-coroutines-core-common:$version", + "jvmMain" to "org.jetbrains.kotlinx:kotlinx-coroutines-core:$version", + "jsMain" to "org.jetbrains.kotlinx:kotlinx-coroutines-core-js:$version", + "nativeMain" to "org.jetbrains.kotlinx:kotlinx-coroutines-core-native:$version" +) \ No newline at end of file