diff --git a/build.gradle.kts b/build.gradle.kts index 00436aa..9c61514 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -51,12 +51,6 @@ tasks.test { gradlePlugin { plugins { - create("common") { - id = "space.kscience.gradle.common" - description = "The generalized kscience plugin that works in conjunction with any kotlin plugin" - implementationClass = "space.kscience.gradle.KScienceCommonPlugin" - } - create("project") { id = "space.kscience.gradle.project" description = "The root plugin for multi-module project infrastructure" @@ -80,18 +74,6 @@ gradlePlugin { description = "Pre-configured JS project" implementationClass = "space.kscience.gradle.KScienceJSPlugin" } - - create("native") { - id = "space.kscience.gradle.native" - description = "Additional native targets to be use alongside mpp" - implementationClass = "space.kscience.gradle.KScienceNativePlugin" - } - - create("node") { - id = "space.kscience.gradle.node" - description = "Additional nodejs target to be use alongside mpp" - implementationClass = "space.kscience.gradle.KScienceNodePlugin" - } } } @@ -108,7 +90,6 @@ tasks.create("version") { //publishing version catalog -@Suppress("UnstableApiUsage") catalog.versionCatalog { from(files("gradle/libs.versions.toml")) } diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index 97221a5..6b47123 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -1,5 +1,5 @@ [versions] -tools = "0.12.1-kotlin-1.7.20-Beta" +tools = "0.13.0-kotlin-1.7.20-Beta" kotlin = "1.7.20-Beta" atomicfu = "0.18.2" binary-compatibility-validator = "0.10.1" diff --git a/src/main/kotlin/space/kscience/gradle/KScienceCommonPlugin.kt b/src/main/kotlin/space/kscience/gradle/KScienceCommonPlugin.kt deleted file mode 100644 index 4e42b22..0000000 --- a/src/main/kotlin/space/kscience/gradle/KScienceCommonPlugin.kt +++ /dev/null @@ -1,8 +0,0 @@ -package space.kscience.gradle - -import org.gradle.api.Plugin -import org.gradle.api.Project - -public open class KScienceCommonPlugin : Plugin { - override fun apply(project: Project): Unit = project.configureKScience() -} diff --git a/src/main/kotlin/space/kscience/gradle/KScienceExtension.kt b/src/main/kotlin/space/kscience/gradle/KScienceExtension.kt index 3afa58b..2c6051d 100644 --- a/src/main/kotlin/space/kscience/gradle/KScienceExtension.kt +++ b/src/main/kotlin/space/kscience/gradle/KScienceExtension.kt @@ -2,12 +2,12 @@ package space.kscience.gradle import org.gradle.api.Project import org.gradle.api.plugins.ApplicationPlugin -import org.gradle.kotlin.dsl.apply -import org.gradle.kotlin.dsl.findByType -import org.gradle.kotlin.dsl.withType +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 import org.jetbrains.kotlin.gradle.dsl.KotlinProjectExtension +import org.jetbrains.kotlin.gradle.plugin.KotlinDependencyHandler import org.jetbrains.kotlin.gradle.plugin.mpp.KotlinNativeTarget import org.jetbrains.kotlin.gradle.targets.js.dsl.KotlinJsTargetDsl import org.jetbrains.kotlin.gradle.targets.jvm.KotlinJvmTarget @@ -138,6 +138,33 @@ public open class KScienceExtension(public val project: Project) { } } + + public fun dependencies(sourceSet: String? = null, dependencyBlock: KotlinDependencyHandler.() -> Unit) { + project.pluginManager.withPlugin("org.jetbrains.kotlin.jvm") { + project.configure { + sourceSets.getByName(sourceSet ?: "main") { + dependencies(dependencyBlock) + } + } + } + + project.pluginManager.withPlugin("org.jetbrains.kotlin.js") { + project.configure { + sourceSets.getByName(sourceSet ?: "main") { + dependencies(dependencyBlock) + } + } + } + + project.pluginManager.withPlugin("org.jetbrains.kotlin.multiplatform") { + project.configure { + sourceSets.getByName(sourceSet ?: "commonMain") { + dependencies(dependencyBlock) + } + } + } + } + /** * Mark this module as an application module. JVM application should be enabled separately */ @@ -212,38 +239,136 @@ public class KScienceNativeConfiguration { KScienceNativeTarget.iosArm64, ).associateBy { it.preset }.toMutableMap() - public fun targets(vararg target: KScienceNativeTarget) { + /** + * Replace all targets + */ + public fun setTargets(vararg target: KScienceNativeTarget) { targets = target.associateBy { it.preset }.toMutableMap() } + /** + * Add a native target + */ public fun target(target: KScienceNativeTarget) { targets[target.preset] = target } + + public fun target( + preset: KotlinNativePreset, + targetName: String = preset.name, + targetConfiguration: KotlinNativeTarget.() -> Unit = { }, + ): Unit = target(KScienceNativeTarget(preset, targetName, targetConfiguration)) } public open class KScienceMppExtension(project: Project) : KScienceExtension(project) { - internal var jvmConfiguration: ((KotlinJvmTarget) -> Unit)? = { } - /** * Custom configuration for JVM target. If null - disable JVM target */ public fun jvm(block: KotlinJvmTarget.() -> Unit) { - jvmConfiguration = block + project.pluginManager.withPlugin("org.jetbrains.kotlin.multiplatform") { + project.configure { + jvm(block) + } + } } - internal var jsConfiguration: ((KotlinJsTargetDsl) -> Unit)? = { } + /** + * Remove Jvm target + */ + public fun noJvm() { + project.pluginManager.withPlugin("org.jetbrains.kotlin.multiplatform") { + project.configure { + targets.removeIf { it is KotlinJvmTarget } + } + } + } /** * Custom configuration for JS target. If null - disable JS target */ public fun js(block: KotlinJsTargetDsl.() -> Unit) { - jsConfiguration = block + project.pluginManager.withPlugin("org.jetbrains.kotlin.multiplatform") { + project.configure { + js(block) + } + } } - internal var nativeConfiguration: KScienceNativeConfiguration? = null + public fun noJs() { + project.pluginManager.withPlugin("org.jetbrains.kotlin.multiplatform") { + project.configure { + targets.removeIf { it is KotlinJsTargetDsl } + } + } + } - public fun native(block: KScienceNativeConfiguration.() -> Unit = {}) { - nativeConfiguration = KScienceNativeConfiguration().apply(block) + public fun native(block: KScienceNativeConfiguration.() -> Unit = {}): Unit = with(project) { + val nativeConfiguration = KScienceNativeConfiguration().apply(block) + pluginManager.withPlugin("space.kscience.gradle.mpp") { + configure { + sourceSets { + val nativeTargets: List = + nativeConfiguration.targets.values.mapNotNull { nativeTarget -> + when (nativeTarget.preset) { + KotlinNativePreset.linuxX64 -> linuxX64( + nativeTarget.targetName, + nativeTarget.targetConfiguration + ) + + KotlinNativePreset.mingwX64 -> mingwX64( + nativeTarget.targetName, + nativeTarget.targetConfiguration + ) + + KotlinNativePreset.macosX64 -> macosX64( + nativeTarget.targetName, + nativeTarget.targetConfiguration + ) + + KotlinNativePreset.iosX64 -> iosX64( + nativeTarget.targetName, + nativeTarget.targetConfiguration + ) + + KotlinNativePreset.iosArm64 -> iosArm64( + nativeTarget.targetName, + nativeTarget.targetConfiguration + ) + +// else -> { +// logger.error("Native preset ${nativeTarget.preset} not recognised.") +// null +// } + } + } + val commonMain by getting + val commonTest by getting + + val nativeMain by creating { + dependsOn(commonMain) + } + + val nativeTest by creating { + //should NOT depend on nativeMain because automatic dependency by plugin + dependsOn(commonTest) + } + + configure(nativeTargets) { + compilations["main"]?.apply { + configure(kotlinSourceSets) { + dependsOn(nativeMain) + } + } + + compilations["test"]?.apply { + configure(kotlinSourceSets) { + dependsOn(nativeTest) + } + } + } + } + } + } } } diff --git a/src/main/kotlin/space/kscience/gradle/KScienceJSPlugin.kt b/src/main/kotlin/space/kscience/gradle/KScienceJSPlugin.kt index a2fd328..ee7960c 100644 --- a/src/main/kotlin/space/kscience/gradle/KScienceJSPlugin.kt +++ b/src/main/kotlin/space/kscience/gradle/KScienceJSPlugin.kt @@ -2,7 +2,6 @@ package space.kscience.gradle import org.gradle.api.Plugin import org.gradle.api.Project -import org.gradle.kotlin.dsl.apply public open class KScienceJSPlugin : Plugin { override fun apply(project: Project): Unit = project.run { @@ -11,7 +10,6 @@ public open class KScienceJSPlugin : Plugin { } else { logger.info("Kotlin JS plugin is already present") } - - apply() + project.configureKScience() } } diff --git a/src/main/kotlin/space/kscience/gradle/KScienceJVMPlugin.kt b/src/main/kotlin/space/kscience/gradle/KScienceJVMPlugin.kt index 55caf90..749d24a 100644 --- a/src/main/kotlin/space/kscience/gradle/KScienceJVMPlugin.kt +++ b/src/main/kotlin/space/kscience/gradle/KScienceJVMPlugin.kt @@ -2,15 +2,14 @@ package space.kscience.gradle import org.gradle.api.Plugin import org.gradle.api.Project -import org.gradle.kotlin.dsl.apply public open class KScienceJVMPlugin : Plugin { override fun apply(project: Project): Unit = project.run { - if (!plugins.hasPlugin("org.jetbrains.kotlin.jvm")) + if (!plugins.hasPlugin("org.jetbrains.kotlin.jvm")) { plugins.apply("org.jetbrains.kotlin.jvm") - else + } else { logger.info("Kotlin JVM plugin is already present") - - apply() + } + project.configureKScience() } } diff --git a/src/main/kotlin/space/kscience/gradle/KScienceMPPlugin.kt b/src/main/kotlin/space/kscience/gradle/KScienceMPPlugin.kt index 4da79c2..65c53c0 100644 --- a/src/main/kotlin/space/kscience/gradle/KScienceMPPlugin.kt +++ b/src/main/kotlin/space/kscience/gradle/KScienceMPPlugin.kt @@ -2,7 +2,6 @@ package space.kscience.gradle import org.gradle.api.Plugin import org.gradle.api.Project -import org.gradle.kotlin.dsl.apply import org.gradle.kotlin.dsl.hasPlugin import org.jetbrains.kotlin.gradle.plugin.mpp.KotlinMultiplatformPlugin @@ -14,7 +13,6 @@ public open class KScienceMPPlugin : Plugin { } else { logger.info("Kotlin MPP plugin is already present") } - - apply() + project.configureKScience() } } diff --git a/src/main/kotlin/space/kscience/gradle/KScienceNativePlugin.kt b/src/main/kotlin/space/kscience/gradle/KScienceNativePlugin.kt deleted file mode 100644 index 5b86fea..0000000 --- a/src/main/kotlin/space/kscience/gradle/KScienceNativePlugin.kt +++ /dev/null @@ -1,62 +0,0 @@ -package space.kscience.gradle - -import org.gradle.api.Plugin -import org.gradle.api.Project -import org.gradle.kotlin.dsl.* -import org.jetbrains.kotlin.gradle.dsl.KotlinMultiplatformExtension - -@Deprecated("Use common configuration block") -public class KScienceNativePlugin : Plugin { - override fun apply(project: Project): Unit = project.run { - logger.warn("Native plugin is deprecated. Use `ksceince.native()`") - - //Apply multiplatform plugin is not applied, apply it - if (!plugins.hasPlugin("org.jetbrains.kotlin.multiplatform")) { - logger.info("Kotlin multiplatform plugin is not resolved. Adding it automatically") - plugins.apply("org.jetbrains.kotlin.multiplatform") - } - - if (!plugins.hasPlugin(KScienceCommonPlugin::class)) { - logger.info("KScience plugin is not resolved. Adding it automatically") - apply() - } - - configure { - val nativeTargets = setOf( - linuxX64(), - mingwX64(), - macosX64(), - iosX64(), - iosArm64() - ) - - sourceSets { - val commonMain = findByName("commonMain")!! - val commonTest = findByName("commonTest")!! - - val nativeMain by creating { - dependsOn(commonMain) - } - - val nativeTest by creating { - //should NOT depend on nativeMain because automatic dependency by plugin - dependsOn(commonTest) - } - - configure(nativeTargets) { - compilations["main"]?.apply { - configure(kotlinSourceSets) { - dependsOn(nativeMain) - } - } - - compilations["test"]?.apply { - configure(kotlinSourceSets) { - dependsOn(nativeTest) - } - } - } - } - } - } -} diff --git a/src/main/kotlin/space/kscience/gradle/KScienceNodePlugin.kt b/src/main/kotlin/space/kscience/gradle/KScienceNodePlugin.kt deleted file mode 100644 index 4eedc80..0000000 --- a/src/main/kotlin/space/kscience/gradle/KScienceNodePlugin.kt +++ /dev/null @@ -1,57 +0,0 @@ -package space.kscience.gradle - -import org.gradle.api.Plugin -import org.gradle.api.Project -import org.gradle.kotlin.dsl.apply -import org.gradle.kotlin.dsl.configure -import org.gradle.kotlin.dsl.findPlugin -import org.gradle.kotlin.dsl.invoke -import org.jetbrains.kotlin.gradle.dsl.KotlinMultiplatformExtension - -/** - * Create a separate target for node - */ -public class KScienceNodePlugin : Plugin { - override fun apply(target: Project): Unit = target.run { - //Apply multiplatform plugin is not applied, apply it - if (plugins.findPlugin("org.jetbrains.kotlin.multiplatform") == null) { - logger.info("Kotlin multiplatform plugin is not resolved. Adding it automatically") - plugins.apply("org.jetbrains.kotlin.multiplatform") - } - - if (plugins.findPlugin(KScienceCommonPlugin::class) == null) { - logger.info("KScience plugin is not resolved. Adding it automatically") - apply() - } - - configure { - js(name = "node", compiler = IR) { - nodejs() - } - - sourceSets { - val commonMain = findByName("commonMain")!! - val commonTest = findByName("commonTest")!! - - val jsCommonMain = create("jsCommonMain").apply { - dependsOn(commonMain) - } - - val jsCommonTest = create("jsCommonTest").apply { - dependsOn(commonTest) - } - - findByName("jsMain")?.dependsOn(jsCommonMain) - findByName("jsTest")?.dependsOn(jsCommonTest) - - findByName("nodeMain")?.apply { - dependsOn(jsCommonMain) - dependencies { - api("org.jetbrains.kotlinx:kotlinx-nodejs:${KScienceVersions.kotlinxNodeVersion}") - } - } - findByName("nodeTest")?.dependsOn(jsCommonMain) - } - } - } -} diff --git a/src/main/kotlin/space/kscience/gradle/commonConfigurations.kt b/src/main/kotlin/space/kscience/gradle/commonConfigurations.kt index b2c26ef..a948ed0 100644 --- a/src/main/kotlin/space/kscience/gradle/commonConfigurations.kt +++ b/src/main/kotlin/space/kscience/gradle/commonConfigurations.kt @@ -9,7 +9,6 @@ import org.jetbrains.dokka.gradle.DokkaPlugin import org.jetbrains.kotlin.gradle.dsl.KotlinJsProjectExtension import org.jetbrains.kotlin.gradle.dsl.KotlinJvmProjectExtension import org.jetbrains.kotlin.gradle.dsl.KotlinMultiplatformExtension -import org.jetbrains.kotlin.gradle.plugin.mpp.KotlinNativeTargetWithHostTests import org.jetbrains.kotlin.gradle.tasks.KotlinCompile import space.kscience.gradle.internal.applyRepos import space.kscience.gradle.internal.applySettings @@ -44,7 +43,7 @@ public fun Project.configureKScience( //Configuration for K-JVM plugin pluginManager.withPlugin("org.jetbrains.kotlin.jvm") { - registerKScienceExtension(::KScienceExtension) + val extension = registerKScienceExtension(::KScienceExtension) //logger.info("Applying KScience configuration for JVM project") configure { @@ -78,7 +77,7 @@ public fun Project.configureKScience( } pluginManager.withPlugin("org.jetbrains.kotlin.js") { - registerKScienceExtension(::KScienceExtension) + val extension = registerKScienceExtension(::KScienceExtension) //logger.info("Applying KScience configuration for JS project") configure { @@ -118,31 +117,25 @@ public fun Project.configureKScience( } pluginManager.withPlugin("org.jetbrains.kotlin.multiplatform") { - val configuration = registerKScienceExtension(::KScienceMppExtension) + val extension = registerKScienceExtension(::KScienceMppExtension) configure { - configuration.jvmConfiguration?.let { jvmConfiguration -> - jvm { - compilations.all { - kotlinOptions { - jvmTarget = KScienceVersions.JVM_TARGET.toString() - freeCompilerArgs = freeCompilerArgs + defaultKotlinJvmArgs - } + jvm { + compilations.all { + kotlinOptions { + jvmTarget = KScienceVersions.JVM_TARGET.toString() + freeCompilerArgs = freeCompilerArgs + defaultKotlinJvmArgs } - jvmConfiguration(this) } } - configuration.jsConfiguration?.let { jsConfiguration -> - js(IR) { - browser { - commonWebpackConfig { - cssSupport { - enabled = true - } + js(IR) { + browser { + commonWebpackConfig { + cssSupport { + enabled = true } } - jsConfiguration(this) } } @@ -158,79 +151,17 @@ public fun Project.configureKScience( implementation(kotlin("test-annotations-common")) } } - val jvmMain by getting val jvmTest by getting { dependencies { implementation(kotlin("test-junit5")) implementation("org.junit.jupiter:junit-jupiter:${KScienceVersions.junit}") } } - val jsMain by getting val jsTest by getting { dependencies { implementation(kotlin("test-js")) } } - - configuration.nativeConfiguration?.let { nativeConfiguration -> - val nativeTargets: List = - nativeConfiguration.targets.values.mapNotNull { nativeTarget -> - when (nativeTarget.preset) { - KotlinNativePreset.linuxX64 -> linuxX64( - nativeTarget.targetName, - nativeTarget.targetConfiguration - ) - - KotlinNativePreset.mingwX64 -> linuxX64( - nativeTarget.targetName, - nativeTarget.targetConfiguration - ) - - KotlinNativePreset.macosX64 -> linuxX64( - nativeTarget.targetName, - nativeTarget.targetConfiguration - ) - - KotlinNativePreset.iosX64 -> linuxX64( - nativeTarget.targetName, - nativeTarget.targetConfiguration - ) - - KotlinNativePreset.iosArm64 -> linuxX64( - nativeTarget.targetName, - nativeTarget.targetConfiguration - ) - - else -> { - logger.error("Native preset ${nativeTarget.preset} not recognised.") - null - } - } - } - - val nativeMain by creating { - dependsOn(commonMain) - } - - val nativeTest by creating { - //should NOT depend on nativeMain because automatic dependency by plugin - dependsOn(commonTest) - } - - configure(nativeTargets) { - compilations["main"]?.apply { - configure(kotlinSourceSets) { - dependsOn(nativeMain) - } - } - - compilations["test"]?.apply { - configure(kotlinSourceSets) { - dependsOn(nativeTest) - } - } - } - } } sourceSets.all {