From 1b179e0b0551be9f15753ffd71227bd620074dcf Mon Sep 17 00:00:00 2001 From: Alexander Nozik Date: Fri, 3 Feb 2023 19:28:29 +0300 Subject: [PATCH] Easier dependency handling for kscience --- .../kscience/gradle/KScienceExtension.kt | 50 +++++++++---------- 1 file changed, 25 insertions(+), 25 deletions(-) diff --git a/src/main/kotlin/space/kscience/gradle/KScienceExtension.kt b/src/main/kotlin/space/kscience/gradle/KScienceExtension.kt index 61afe26..2b2e4fa 100644 --- a/src/main/kotlin/space/kscience/gradle/KScienceExtension.kt +++ b/src/main/kotlin/space/kscience/gradle/KScienceExtension.kt @@ -128,28 +128,18 @@ public open class KScienceExtension(public val project: Project) { } } - public fun jvmDependencies(dependencyBlock: KotlinDependencyHandler.() -> Unit) { + public fun testDependencies(sourceSet: String? = null, dependencyBlock: KotlinDependencyHandler.() -> Unit) { project.pluginManager.withPlugin("org.jetbrains.kotlin.jvm") { project.configure { - sourceSets.getByName("main") { + sourceSets.getByName(sourceSet ?: "test") { dependencies(dependencyBlock) } } } - project.pluginManager.withPlugin("org.jetbrains.kotlin.multiplatform") { - project.configure { - sourceSets.getByName("jvmMain") { - dependencies(dependencyBlock) - } - } - } - } - - public fun jsDependencies(dependencyBlock: KotlinDependencyHandler.() -> Unit) { project.pluginManager.withPlugin("org.jetbrains.kotlin.js") { project.configure { - sourceSets.getByName("main") { + sourceSets.getByName(sourceSet ?: "test") { dependencies(dependencyBlock) } } @@ -157,13 +147,22 @@ public open class KScienceExtension(public val project: Project) { project.pluginManager.withPlugin("org.jetbrains.kotlin.multiplatform") { project.configure { - sourceSets.getByName("jsMain") { + sourceSets.getByName(sourceSet ?: "commonTest") { dependencies(dependencyBlock) } } } } + + public class DefaultSourceSet(public val key: String) + + public fun dependencies( + defaultSourceSet: DefaultSourceSet, + dependencyBlock: KotlinDependencyHandler.() -> Unit, + ): Unit = dependencies(defaultSourceSet.key, dependencyBlock) + + /** * Mark this module as an application module. JVM application should be enabled separately */ @@ -196,15 +195,21 @@ public open class KScienceExtension(public val project: Project) { /** * Add context receivers to this project and all subprojects */ - public fun withContextReceivers() { - project.allprojects { - tasks.withType { - kotlinOptions { - freeCompilerArgs = freeCompilerArgs + "-Xcontext-receivers" - } + public fun useContextReceivers() { + project.tasks.withType { + kotlinOptions { + freeCompilerArgs = freeCompilerArgs + "-Xcontext-receivers" } } } + + public val jvmMain: DefaultSourceSet get() = DefaultSourceSet("jvmMain") + public val jvmTest: DefaultSourceSet get() = DefaultSourceSet("jvmTest") + public val jsMain: DefaultSourceSet get() = DefaultSourceSet("jsMain") + public val jsTest: DefaultSourceSet get() = DefaultSourceSet("jsTest") + public val nativeMain: DefaultSourceSet get() = DefaultSourceSet("nativeMain") + public val nativeTest: DefaultSourceSet get() = DefaultSourceSet("nativeTest") + } public enum class KotlinNativePreset { @@ -287,11 +292,6 @@ public open class KScienceMppExtension(project: Project) : KScienceExtension(pro implementation("org.junit.jupiter:junit-jupiter:${KScienceVersions.junit}") } } - getByName("jvmTest") { - dependencies { - implementation(kotlin("test-js")) - } - } } jvmToolchain { languageVersion.set(KScienceVersions.JVM_TARGET)