gradle-tools/src/main/kotlin/ru/mipt/npm/gradle/KScienceNodePlugin.kt

61 lines
2.3 KiB
Kotlin
Raw Normal View History

2020-09-30 20:43:02 +03:00
package ru.mipt.npm.gradle
2021-02-23 16:53:15 +03:00
import org.gradle.api.Action
import org.gradle.api.NamedDomainObjectContainer
2020-09-30 20:43:02 +03:00
import org.gradle.api.Plugin
import org.gradle.api.Project
2021-02-23 16:53:15 +03:00
import org.gradle.kotlin.dsl.apply
import org.gradle.kotlin.dsl.configure
import org.gradle.kotlin.dsl.findPlugin
2020-09-30 20:43:02 +03:00
import org.jetbrains.kotlin.gradle.dsl.KotlinMultiplatformExtension
2021-02-23 16:53:15 +03:00
import org.jetbrains.kotlin.gradle.plugin.KotlinSourceSet
private fun KotlinMultiplatformExtension.sourceSets(configure: Action<NamedDomainObjectContainer<KotlinSourceSet>>): Unit =
(this as org.gradle.api.plugins.ExtensionAware).extensions.configure("sourceSets", configure)
2020-09-30 20:43:02 +03:00
/**
* Create a separate target for node
*/
class KScienceNodePlugin : Plugin<Project> {
override fun apply(target: Project) = target.run {
//Apply multiplatform plugin is not applied, apply it
2020-11-25 14:26:08 +03:00
if (plugins.findPlugin("org.jetbrains.kotlin.multiplatform") == null) {
2020-11-25 12:16:09 +03:00
logger.info("Kotlin multiplatform plugin is not resolved. Adding it automatically")
2020-11-25 14:26:08 +03:00
pluginManager.apply("org.jetbrains.kotlin.multiplatform")
2020-11-25 12:16:09 +03:00
}
if (plugins.findPlugin(KScienceCommonPlugin::class) == null) {
logger.info("KScience plugin is not resolved. Adding it automatically")
pluginManager.apply(KScienceCommonPlugin::class)
2020-09-30 20:43:02 +03:00
}
configure<KotlinMultiplatformExtension> {
js(name = "node", compiler = IR) {
nodejs()
}
2021-02-23 16:53:15 +03:00
2020-09-30 20:43:02 +03:00
sourceSets {
2021-02-23 16:53:15 +03:00
val commonMain = findByName("commonMain")!!
val commonTest = findByName("commonTest")!!
2021-02-03 20:13:22 +03:00
2021-02-23 16:53:15 +03:00
val jsCommonMain = create("jsCommonMain").apply {
2020-09-30 20:43:02 +03:00
dependsOn(commonMain)
2021-02-03 20:13:22 +03:00
}
2021-02-23 16:53:15 +03:00
val jsCommonTest = create("jsCommonTest").apply {
2021-02-03 20:13:22 +03:00
dependsOn(commonTest)
}
2021-02-23 16:53:15 +03:00
findByName("jsMain")?.dependsOn(jsCommonMain)
findByName("jsTest")?.dependsOn(jsCommonTest)
2021-02-03 20:13:22 +03:00
2021-02-23 16:53:15 +03:00
findByName("nodeMain")?.apply {
2021-02-03 20:13:22 +03:00
dependsOn(jsCommonMain)
2021-02-23 16:53:15 +03:00
dependencies {
2020-09-30 20:43:02 +03:00
api("org.jetbrains.kotlinx:kotlinx-nodejs:${KScienceVersions.kotlinxNodeVersion}")
}
}
2021-02-23 16:53:15 +03:00
findByName("nodeTest")?.dependsOn(jsCommonMain)
2020-09-30 20:43:02 +03:00
}
}
}
}