[Gradle, JS] Add link tasks to binaries, add test executable binary type

This commit is contained in:
Ilya Goncharov
2020-02-12 15:07:53 +03:00
parent 99d73d4c79
commit 1de720cdd0
3 changed files with 118 additions and 23 deletions
@@ -0,0 +1,58 @@
/*
* Copyright 2010-2020 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
package org.jetbrains.kotlin.gradle.targets.js.ir
import org.gradle.api.Project
import org.gradle.api.tasks.TaskProvider
import org.jetbrains.kotlin.gradle.plugin.KotlinTarget
import org.jetbrains.kotlin.gradle.plugin.mpp.KotlinJsCompilation
import org.jetbrains.kotlin.gradle.utils.lowerCamelCaseName
sealed class JsBinary(
internal val name: String,
internal val type: KotlinJsIrType,
internal val compilation: KotlinJsCompilation
) {
val linkTaskName: String = linkTaskName(type)
val linkTask: TaskProvider<KotlinJsIrLink>
get() = target.project.tasks.named(linkTaskName) as TaskProvider<KotlinJsIrLink>
private fun linkTaskName(type: KotlinJsIrType): String =
lowerCamelCaseName(
"compile",
type.name.toLowerCase(),
name,
"Kotlin",
target.targetName
)
val target: KotlinTarget
get() = compilation.target
val project: Project
get() = target.project
}
class Executable(
name: String,
type: KotlinJsIrType,
compilation: KotlinJsCompilation
) : JsBinary(
name,
type,
compilation
)
class TestExecutable(
name: String,
type: KotlinJsIrType,
compilation: KotlinJsCompilation
) : JsBinary(
name,
type,
compilation
)
@@ -5,12 +5,7 @@
package org.jetbrains.kotlin.gradle.targets.js.ir
sealed class JsBinary(
internal val name: String
)
class Executable(
name: String
) : JsBinary(
name
)
enum class JsBinaryType {
EXECUTABLE,
TEST
}
@@ -8,8 +8,11 @@ package org.jetbrains.kotlin.gradle.targets.js.ir
import org.gradle.api.DomainObjectSet
import org.gradle.api.Project
import org.gradle.api.plugins.ExtensionAware
import org.jetbrains.kotlin.gradle.plugin.KotlinCompilation
import org.jetbrains.kotlin.gradle.plugin.mpp.KotlinBinaryContainer
import org.jetbrains.kotlin.gradle.plugin.mpp.KotlinJsCompilation
import org.jetbrains.kotlin.gradle.targets.js.ir.KotlinJsIrType.DEVELOPMENT
import org.jetbrains.kotlin.gradle.targets.js.ir.KotlinJsIrType.PRODUCTION
import org.jetbrains.kotlin.gradle.utils.lowerCamelCaseName
import javax.inject.Inject
@@ -25,29 +28,68 @@ constructor(
private val nameToBinary = mutableMapOf<String, JsBinary>()
fun executable() = createBinaries(project.name, ::Executable)
private val defaultCompilation: KotlinJsCompilation
get() = target.compilations.getByName(KotlinCompilation.MAIN_COMPILATION_NAME)
private val defaultTestCompilation: KotlinJsCompilation
get() = target.compilations.getByName(KotlinCompilation.TEST_COMPILATION_NAME)
fun executable() = createBinaries(
baseName = project.name,
jsBinaryType = JsBinaryType.EXECUTABLE,
create = ::Executable
)
internal fun testExecutable() = createBinaries(
baseName = project.name,
jsBinaryType = JsBinaryType.TEST,
create = ::TestExecutable
)
private fun <T : JsBinary> createBinaries(
baseName: String,
create: (name: String) -> T
buildVariantKinds: Collection<KotlinJsIrType> = listOf(PRODUCTION, DEVELOPMENT),
jsBinaryType: JsBinaryType,
create: (name: String, buildVariantKind: KotlinJsIrType, compilation: KotlinJsCompilation) -> T
) {
val name = generateBinaryName(baseName)
buildVariantKinds.forEach { buildVariantKind ->
val name = generateBinaryName(
baseName,
buildVariantKind,
jsBinaryType
)
require(name !in nameToBinary) {
"Cannot create binary $name: binary with such a name already exists"
}
require(name !in nameToBinary) {
"Cannot create binary $name: binary with such a name already exists"
}
val binary = create(baseName)
add(binary)
nameToBinary[binary.name] = binary
// Allow accessing binaries as properties of the container in Groovy DSL.
if (this is ExtensionAware) {
extensions.add(binary.name, binary)
val compilation = if (jsBinaryType == JsBinaryType.TEST) {
defaultTestCompilation
} else {
defaultCompilation
}
val binary = create(baseName, buildVariantKind, compilation)
add(binary)
nameToBinary[binary.name] = binary
// Allow accessing binaries as properties of the container in Groovy DSL.
if (this is ExtensionAware) {
extensions.add(binary.name, binary)
}
}
}
companion object {
internal fun generateBinaryName(name: String) =
lowerCamelCaseName(name)
internal fun generateBinaryName(
name: String,
buildVariantKind: KotlinJsIrType,
jsBinaryType: JsBinaryType
) =
lowerCamelCaseName(
name,
buildVariantKind.name,
jsBinaryType.name
)
}
}