Remove old plugins make native configurable inside kscience block.

This commit is contained in:
Alexander Nozik 2022-09-04 20:46:25 +03:00
parent b4f494e58a
commit 5dacf9c9d6
No known key found for this signature in database
GPG Key ID: F7FCF2DD25C71357
10 changed files with 157 additions and 252 deletions

View File

@ -51,12 +51,6 @@ tasks.test {
gradlePlugin { gradlePlugin {
plugins { 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") { create("project") {
id = "space.kscience.gradle.project" id = "space.kscience.gradle.project"
description = "The root plugin for multi-module project infrastructure" description = "The root plugin for multi-module project infrastructure"
@ -80,18 +74,6 @@ gradlePlugin {
description = "Pre-configured JS project" description = "Pre-configured JS project"
implementationClass = "space.kscience.gradle.KScienceJSPlugin" 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 //publishing version catalog
@Suppress("UnstableApiUsage")
catalog.versionCatalog { catalog.versionCatalog {
from(files("gradle/libs.versions.toml")) from(files("gradle/libs.versions.toml"))
} }

View File

@ -1,5 +1,5 @@
[versions] [versions]
tools = "0.12.1-kotlin-1.7.20-Beta" tools = "0.13.0-kotlin-1.7.20-Beta"
kotlin = "1.7.20-Beta" kotlin = "1.7.20-Beta"
atomicfu = "0.18.2" atomicfu = "0.18.2"
binary-compatibility-validator = "0.10.1" binary-compatibility-validator = "0.10.1"

View File

@ -1,8 +0,0 @@
package space.kscience.gradle
import org.gradle.api.Plugin
import org.gradle.api.Project
public open class KScienceCommonPlugin : Plugin<Project> {
override fun apply(project: Project): Unit = project.configureKScience()
}

View File

@ -2,12 +2,12 @@ package space.kscience.gradle
import org.gradle.api.Project import org.gradle.api.Project
import org.gradle.api.plugins.ApplicationPlugin import org.gradle.api.plugins.ApplicationPlugin
import org.gradle.kotlin.dsl.apply import org.gradle.kotlin.dsl.*
import org.gradle.kotlin.dsl.findByType
import org.gradle.kotlin.dsl.withType
import org.jetbrains.kotlin.gradle.dsl.KotlinJsProjectExtension 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.KotlinMultiplatformExtension
import org.jetbrains.kotlin.gradle.dsl.KotlinProjectExtension 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.plugin.mpp.KotlinNativeTarget
import org.jetbrains.kotlin.gradle.targets.js.dsl.KotlinJsTargetDsl import org.jetbrains.kotlin.gradle.targets.js.dsl.KotlinJsTargetDsl
import org.jetbrains.kotlin.gradle.targets.jvm.KotlinJvmTarget 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<KotlinJvmProjectExtension> {
sourceSets.getByName(sourceSet ?: "main") {
dependencies(dependencyBlock)
}
}
}
project.pluginManager.withPlugin("org.jetbrains.kotlin.js") {
project.configure<KotlinJsProjectExtension> {
sourceSets.getByName(sourceSet ?: "main") {
dependencies(dependencyBlock)
}
}
}
project.pluginManager.withPlugin("org.jetbrains.kotlin.multiplatform") {
project.configure<KotlinMultiplatformExtension> {
sourceSets.getByName(sourceSet ?: "commonMain") {
dependencies(dependencyBlock)
}
}
}
}
/** /**
* Mark this module as an application module. JVM application should be enabled separately * Mark this module as an application module. JVM application should be enabled separately
*/ */
@ -212,38 +239,136 @@ public class KScienceNativeConfiguration {
KScienceNativeTarget.iosArm64, KScienceNativeTarget.iosArm64,
).associateBy { it.preset }.toMutableMap() ).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() targets = target.associateBy { it.preset }.toMutableMap()
} }
/**
* Add a native target
*/
public fun target(target: KScienceNativeTarget) { public fun target(target: KScienceNativeTarget) {
targets[target.preset] = target 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) { public open class KScienceMppExtension(project: Project) : KScienceExtension(project) {
internal var jvmConfiguration: ((KotlinJvmTarget) -> Unit)? = { }
/** /**
* Custom configuration for JVM target. If null - disable JVM target * Custom configuration for JVM target. If null - disable JVM target
*/ */
public fun jvm(block: KotlinJvmTarget.() -> Unit) { public fun jvm(block: KotlinJvmTarget.() -> Unit) {
jvmConfiguration = block project.pluginManager.withPlugin("org.jetbrains.kotlin.multiplatform") {
project.configure<KotlinMultiplatformExtension> {
jvm(block)
}
}
} }
internal var jsConfiguration: ((KotlinJsTargetDsl) -> Unit)? = { } /**
* Remove Jvm target
*/
public fun noJvm() {
project.pluginManager.withPlugin("org.jetbrains.kotlin.multiplatform") {
project.configure<KotlinMultiplatformExtension> {
targets.removeIf { it is KotlinJvmTarget }
}
}
}
/** /**
* Custom configuration for JS target. If null - disable JS target * Custom configuration for JS target. If null - disable JS target
*/ */
public fun js(block: KotlinJsTargetDsl.() -> Unit) { public fun js(block: KotlinJsTargetDsl.() -> Unit) {
jsConfiguration = block project.pluginManager.withPlugin("org.jetbrains.kotlin.multiplatform") {
project.configure<KotlinMultiplatformExtension> {
js(block)
}
}
} }
internal var nativeConfiguration: KScienceNativeConfiguration? = null public fun noJs() {
project.pluginManager.withPlugin("org.jetbrains.kotlin.multiplatform") {
project.configure<KotlinMultiplatformExtension> {
targets.removeIf { it is KotlinJsTargetDsl }
}
}
}
public fun native(block: KScienceNativeConfiguration.() -> Unit = {}) { public fun native(block: KScienceNativeConfiguration.() -> Unit = {}): Unit = with(project) {
nativeConfiguration = KScienceNativeConfiguration().apply(block) val nativeConfiguration = KScienceNativeConfiguration().apply(block)
pluginManager.withPlugin("space.kscience.gradle.mpp") {
configure<KotlinMultiplatformExtension> {
sourceSets {
val nativeTargets: List<KotlinNativeTarget> =
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)
}
}
}
}
}
}
} }
} }

View File

@ -2,7 +2,6 @@ package space.kscience.gradle
import org.gradle.api.Plugin import org.gradle.api.Plugin
import org.gradle.api.Project import org.gradle.api.Project
import org.gradle.kotlin.dsl.apply
public open class KScienceJSPlugin : Plugin<Project> { public open class KScienceJSPlugin : Plugin<Project> {
override fun apply(project: Project): Unit = project.run { override fun apply(project: Project): Unit = project.run {
@ -11,7 +10,6 @@ public open class KScienceJSPlugin : Plugin<Project> {
} else { } else {
logger.info("Kotlin JS plugin is already present") logger.info("Kotlin JS plugin is already present")
} }
project.configureKScience()
apply<KScienceCommonPlugin>()
} }
} }

View File

@ -2,15 +2,14 @@ package space.kscience.gradle
import org.gradle.api.Plugin import org.gradle.api.Plugin
import org.gradle.api.Project import org.gradle.api.Project
import org.gradle.kotlin.dsl.apply
public open class KScienceJVMPlugin : Plugin<Project> { public open class KScienceJVMPlugin : Plugin<Project> {
override fun apply(project: Project): Unit = project.run { 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") plugins.apply("org.jetbrains.kotlin.jvm")
else } else {
logger.info("Kotlin JVM plugin is already present") logger.info("Kotlin JVM plugin is already present")
}
apply<KScienceCommonPlugin>() project.configureKScience()
} }
} }

View File

@ -2,7 +2,6 @@ package space.kscience.gradle
import org.gradle.api.Plugin import org.gradle.api.Plugin
import org.gradle.api.Project import org.gradle.api.Project
import org.gradle.kotlin.dsl.apply
import org.gradle.kotlin.dsl.hasPlugin import org.gradle.kotlin.dsl.hasPlugin
import org.jetbrains.kotlin.gradle.plugin.mpp.KotlinMultiplatformPlugin import org.jetbrains.kotlin.gradle.plugin.mpp.KotlinMultiplatformPlugin
@ -14,7 +13,6 @@ public open class KScienceMPPlugin : Plugin<Project> {
} else { } else {
logger.info("Kotlin MPP plugin is already present") logger.info("Kotlin MPP plugin is already present")
} }
project.configureKScience()
apply<KScienceCommonPlugin>()
} }
} }

View File

@ -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<Project> {
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<KScienceCommonPlugin>()
}
configure<KotlinMultiplatformExtension> {
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)
}
}
}
}
}
}
}

View File

@ -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<Project> {
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<KScienceCommonPlugin>()
}
configure<KotlinMultiplatformExtension> {
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)
}
}
}
}

View File

@ -9,7 +9,6 @@ import org.jetbrains.dokka.gradle.DokkaPlugin
import org.jetbrains.kotlin.gradle.dsl.KotlinJsProjectExtension import org.jetbrains.kotlin.gradle.dsl.KotlinJsProjectExtension
import org.jetbrains.kotlin.gradle.dsl.KotlinJvmProjectExtension import org.jetbrains.kotlin.gradle.dsl.KotlinJvmProjectExtension
import org.jetbrains.kotlin.gradle.dsl.KotlinMultiplatformExtension import org.jetbrains.kotlin.gradle.dsl.KotlinMultiplatformExtension
import org.jetbrains.kotlin.gradle.plugin.mpp.KotlinNativeTargetWithHostTests
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
import space.kscience.gradle.internal.applyRepos import space.kscience.gradle.internal.applyRepos
import space.kscience.gradle.internal.applySettings import space.kscience.gradle.internal.applySettings
@ -44,7 +43,7 @@ public fun Project.configureKScience(
//Configuration for K-JVM plugin //Configuration for K-JVM plugin
pluginManager.withPlugin("org.jetbrains.kotlin.jvm") { pluginManager.withPlugin("org.jetbrains.kotlin.jvm") {
registerKScienceExtension(::KScienceExtension) val extension = registerKScienceExtension(::KScienceExtension)
//logger.info("Applying KScience configuration for JVM project") //logger.info("Applying KScience configuration for JVM project")
configure<KotlinJvmProjectExtension> { configure<KotlinJvmProjectExtension> {
@ -78,7 +77,7 @@ public fun Project.configureKScience(
} }
pluginManager.withPlugin("org.jetbrains.kotlin.js") { pluginManager.withPlugin("org.jetbrains.kotlin.js") {
registerKScienceExtension(::KScienceExtension) val extension = registerKScienceExtension(::KScienceExtension)
//logger.info("Applying KScience configuration for JS project") //logger.info("Applying KScience configuration for JS project")
configure<KotlinJsProjectExtension> { configure<KotlinJsProjectExtension> {
@ -118,31 +117,25 @@ public fun Project.configureKScience(
} }
pluginManager.withPlugin("org.jetbrains.kotlin.multiplatform") { pluginManager.withPlugin("org.jetbrains.kotlin.multiplatform") {
val configuration = registerKScienceExtension(::KScienceMppExtension) val extension = registerKScienceExtension(::KScienceMppExtension)
configure<KotlinMultiplatformExtension> { configure<KotlinMultiplatformExtension> {
configuration.jvmConfiguration?.let { jvmConfiguration -> jvm {
jvm { compilations.all {
compilations.all { kotlinOptions {
kotlinOptions { jvmTarget = KScienceVersions.JVM_TARGET.toString()
jvmTarget = KScienceVersions.JVM_TARGET.toString() freeCompilerArgs = freeCompilerArgs + defaultKotlinJvmArgs
freeCompilerArgs = freeCompilerArgs + defaultKotlinJvmArgs
}
} }
jvmConfiguration(this)
} }
} }
configuration.jsConfiguration?.let { jsConfiguration -> js(IR) {
js(IR) { browser {
browser { commonWebpackConfig {
commonWebpackConfig { cssSupport {
cssSupport { enabled = true
enabled = true
}
} }
} }
jsConfiguration(this)
} }
} }
@ -158,79 +151,17 @@ public fun Project.configureKScience(
implementation(kotlin("test-annotations-common")) implementation(kotlin("test-annotations-common"))
} }
} }
val jvmMain by getting
val jvmTest by getting { val jvmTest by getting {
dependencies { dependencies {
implementation(kotlin("test-junit5")) implementation(kotlin("test-junit5"))
implementation("org.junit.jupiter:junit-jupiter:${KScienceVersions.junit}") implementation("org.junit.jupiter:junit-jupiter:${KScienceVersions.junit}")
} }
} }
val jsMain by getting
val jsTest by getting { val jsTest by getting {
dependencies { dependencies {
implementation(kotlin("test-js")) implementation(kotlin("test-js"))
} }
} }
configuration.nativeConfiguration?.let { nativeConfiguration ->
val nativeTargets: List<KotlinNativeTargetWithHostTests> =
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 { sourceSets.all {