Remove project extension and replace library loading by extensions helpers.

This commit is contained in:
Alexander Nozik 2019-12-17 15:31:33 +03:00
parent 7dba150ee0
commit 8145386650
8 changed files with 71 additions and 161 deletions

View File

@ -7,7 +7,7 @@ plugins {
} }
group = "scientifik" group = "scientifik"
version = "0.2.7" version = "0.3.0"
repositories { repositories {
gradlePluginPortal() gradlePluginPortal()

View File

@ -3,8 +3,8 @@
*/ */
object Scientifik { object Scientifik {
const val ioVersion = "0.1.15" const val ioVersion = "0.1.15"
const val coroutinesVersion = "1.3.2" const val coroutinesVersion = "1.3.3"
const val atomicfuVersion = "0.12.11" const val atomicfuVersion = "0.14.1"
const val serializationVersion = "0.14.0" const val serializationVersion = "0.14.0"
const val JVM_VERSION = "11" const val JVM_VERSION = "11"
} }

View File

@ -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<Project> {
override fun apply(target: Project) {
with(target) {
plugins.apply("kotlinx-atomicfu")
pluginManager.withPlugin("org.jetbrains.kotlin.multiplatform") {
project.configure<KotlinMultiplatformExtension> {
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<KotlinJvmProjectExtension> {
sourceSets["main"].apply {
dependencies {
implementation("org.jetbrains.kotlinx:atomicfu:${Scientifik.atomicfuVersion}")
}
}
}
}
pluginManager.withPlugin("org.jetbrains.kotlin.js") {
configure<KotlinJsProjectExtension> {
sourceSets["main"].apply {
dependencies {
implementation("org.jetbrains.kotlinx:atomicfu-js:${Scientifik.atomicfuVersion}")
}
}
}
}
}
}
}

View File

@ -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<String, String>) {
val map = mapOf(*pairs)
extensions.findByType<KotlinMultiplatformExtension>()?.apply {
pairs.forEach{(target,depString)->
sourceSets[target].apply {
dependencies {
api(depString)
}
}
}
}
extensions.findByType<KotlinJvmProjectExtension>()?.apply {
sourceSets["main"].apply {
dependencies {
api(map["jvmMain"] ?: error("jvmMain dependency not found"))
}
}
}
extensions.findByType<KotlinJsProjectExtension>()?.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")

View File

@ -10,13 +10,9 @@ import java.io.File
open class ScientifikJSPlugin : Plugin<Project> { open class ScientifikJSPlugin : Plugin<Project> {
override fun apply(project: Project) { override fun apply(project: Project) {
val extension = project.scientifik
with(project) { with(project) {
plugins.apply("org.jetbrains.kotlin.js") plugins.apply("org.jetbrains.kotlin.js")
plugins.apply("kotlinx-serialization")
plugins.apply("kotlinx-atomicfu")
repositories.applyRepos() repositories.applyRepos()
@ -31,6 +27,13 @@ open class ScientifikJSPlugin : Plugin<Project> {
api(kotlin("stdlib-js")) api(kotlin("stdlib-js"))
} }
} }
sourceSets["test"].apply {
languageSettings.applySettings()
dependencies {
implementation(kotlin("test-js"))
}
}
} }
tasks.apply { tasks.apply {

View File

@ -16,8 +16,6 @@ import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
open class ScientifikJVMPlugin : Plugin<Project> { open class ScientifikJVMPlugin : Plugin<Project> {
override fun apply(project: Project) { override fun apply(project: Project) {
val extension = project.scientifik
with(project) { with(project) {
plugins.apply("org.jetbrains.kotlin.jvm") plugins.apply("org.jetbrains.kotlin.jvm")

View File

@ -16,8 +16,6 @@ import java.io.File
open class ScientifikMPPlugin : Plugin<Project> { open class ScientifikMPPlugin : Plugin<Project> {
override fun apply(project: Project) { override fun apply(project: Project) {
val extension = project.scientifik
project.run { project.run {
plugins.apply("org.jetbrains.kotlin.multiplatform") plugins.apply("org.jetbrains.kotlin.multiplatform")

View File

@ -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<String, String>) {
pluginManager.withPlugin("org.jetbrains.kotlin.multiplatform") {
extensions.findByType<KotlinMultiplatformExtension>()?.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"
)