[Gradle, JS] Extract JS presets container, no js in codegen since now

This commit is contained in:
Ilya Goncharov
2020-02-19 12:30:04 +03:00
parent 956b57a55d
commit 488538889b
5 changed files with 84 additions and 147 deletions
@@ -7,7 +7,6 @@ package org.jetbrains.kotlin.generators.gradle.dsl
import groovy.lang.Closure import groovy.lang.Closure
import org.gradle.util.ConfigureUtil import org.gradle.util.ConfigureUtil
import org.jetbrains.kotlin.gradle.plugin.KotlinJsCompilerTypeHolder
import org.jetbrains.kotlin.gradle.plugin.KotlinTargetsContainerWithPresets import org.jetbrains.kotlin.gradle.plugin.KotlinTargetsContainerWithPresets
import java.io.File import java.io.File
@@ -15,29 +14,25 @@ fun main() {
generateKotlinTargetContainerWithPresetFunctionsInterface() generateKotlinTargetContainerWithPresetFunctionsInterface()
} }
private val parentInterfaces = listOf( private val parentInterface = KotlinTargetsContainerWithPresets::class
KotlinTargetsContainerWithPresets::class,
KotlinJsCompilerTypeHolder::class
)
private val presetsProperty = KotlinTargetsContainerWithPresets::presets.name private val presetsProperty = KotlinTargetsContainerWithPresets::presets.name
private fun generateKotlinTargetContainerWithPresetFunctionsInterface() { private fun generateKotlinTargetContainerWithPresetFunctionsInterface() {
// Generate KotlinMultiplatformExtension subclass with member functions for the presets: // Generate KotlinMultiplatformExtension subclass with member functions for the presets:
val functions = allPresetEntries.map { val functions = allPresetEntries.map {
it.generatePresetFunctions(presetsProperty, "configureOrCreate") generatePresetFunctions(it, presetsProperty, "configureOrCreate")
} }
val parentInterfaceNames = val parentInterfaceName =
parentInterfaces.map { typeName(it.java.canonicalName) } typeName(parentInterface.java.canonicalName)
val className = val className =
typeName("org.jetbrains.kotlin.gradle.dsl.KotlinTargetContainerWithPresetFunctions") typeName("org.jetbrains.kotlin.gradle.dsl.KotlinTargetContainerWithPresetFunctions")
val imports = allPresetEntries val imports = allPresetEntries
.flatMap { it.typeNames() } .flatMap { it.typeNames() }
.plus(parentInterfaceNames) .plus(parentInterfaceName)
.plus(typeName(ConfigureUtil::class.java.canonicalName)) .plus(typeName(ConfigureUtil::class.java.canonicalName))
.plus(typeName(Closure::class.java.canonicalName)) .plus(typeName(Closure::class.java.canonicalName))
.filter { it.packageName() != className.packageName() } .filter { it.packageName() != className.packageName() }
@@ -51,7 +46,7 @@ private fun generateKotlinTargetContainerWithPresetFunctionsInterface() {
"package ${className.packageName()}", "package ${className.packageName()}",
imports, imports,
generatedCodeWarning, generatedCodeWarning,
"interface ${className.renderShort()} : ${parentInterfaceNames.joinToString { it.renderShort() }} {", "interface ${className.renderShort()} : ${parentInterfaceName.renderShort()} {",
functions.joinToString("\n\n") { it.indented(4) }, functions.joinToString("\n\n") { it.indented(4) },
"}" "}"
).joinToString("\n\n") ).joinToString("\n\n")
@@ -60,3 +55,27 @@ private fun generateKotlinTargetContainerWithPresetFunctionsInterface() {
val targetFile = File("$outputSourceRoot/${className.fqName.replace(".", "/")}.kt") val targetFile = File("$outputSourceRoot/${className.fqName.replace(".", "/")}.kt")
targetFile.writeText(code) targetFile.writeText(code)
} }
private fun generatePresetFunctions(
presetEntry: KotlinPresetEntry,
getPresetsExpression: String,
configureOrCreateFunctionName: String
): String {
val presetName = presetEntry.presetName
return """
fun $presetName(
name: String = "$presetName",
configure: ${presetEntry.targetType.renderShort()}.() -> Unit = { }
): ${presetEntry.targetType.renderShort()} =
$configureOrCreateFunctionName(
name,
$getPresetsExpression.getByName("$presetName") as ${presetEntry.presetType.renderShort()},
configure
)
fun $presetName() = $presetName("$presetName") { }
fun $presetName(name: String) = $presetName(name) { }
fun $presetName(name: String, configure: Closure<*>) = $presetName(name) { ConfigureUtil.configure(configure, this) }
fun $presetName(configure: Closure<*>) = $presetName { ConfigureUtil.configure(configure, this) }
""".trimIndent()
}
@@ -7,100 +7,17 @@ package org.jetbrains.kotlin.generators.gradle.dsl
import org.jetbrains.kotlin.generators.gradle.dsl.NativeFQNames.Presets import org.jetbrains.kotlin.generators.gradle.dsl.NativeFQNames.Presets
import org.jetbrains.kotlin.generators.gradle.dsl.NativeFQNames.Targets import org.jetbrains.kotlin.generators.gradle.dsl.NativeFQNames.Targets
import org.jetbrains.kotlin.gradle.plugin.JsCompilerType
import org.jetbrains.kotlin.konan.target.HostManager import org.jetbrains.kotlin.konan.target.HostManager
import org.jetbrains.kotlin.konan.target.KonanTarget import org.jetbrains.kotlin.konan.target.KonanTarget
import org.jetbrains.kotlin.konan.target.presetName import org.jetbrains.kotlin.konan.target.presetName
internal open class KotlinPresetEntry( internal class KotlinPresetEntry(
val presetName: String, val presetName: String,
val presetType: TypeName, val presetType: TypeName,
val targetType: TypeName val targetType: TypeName
) { )
open fun typeNames(): Set<TypeName> = setOf(presetType, targetType)
open fun generatePresetFunctions( internal fun KotlinPresetEntry.typeNames(): Set<TypeName> = setOf(presetType, targetType)
getPresetsExpression: String,
configureOrCreateFunctionName: String
): String {
val presetName = presetName
return """
fun $presetName(
name: String = "$presetName",
configure: ${targetType.renderShort()}.() -> Unit = { }
): ${targetType.renderShort()} =
$configureOrCreateFunctionName(
name,
$getPresetsExpression.getByName("$presetName") as ${presetType.renderShort()},
configure
)
fun $presetName() = $presetName("$presetName") { }
fun $presetName(name: String) = $presetName(name) { }
fun $presetName(name: String, configure: Closure<*>) = $presetName(name) { ConfigureUtil.configure(configure, this) }
fun $presetName(configure: Closure<*>) = $presetName { ConfigureUtil.configure(configure, this) }
""".trimIndent()
}
}
internal class KotlinJsPresetEntry(
presetName: String,
presetType: TypeName,
targetType: TypeName
) : KotlinPresetEntry(
presetName,
presetType,
targetType
) {
override fun typeNames(): Set<TypeName> {
return setOf(
typeName(JsCompilerType::class.qualifiedName!!),
typeName("org.jetbrains.kotlin.gradle.utils.lowerCamelCaseName"),
presetType,
targetType
)
}
override fun generatePresetFunctions(
getPresetsExpression: String,
configureOrCreateFunctionName: String
): String {
val presetName = presetName
return """
fun $presetName(
name: String = "$presetName",
compiler: JsCompilerType = defaultJsCompilerType,
configure: ${targetType.renderShort()}.() -> Unit = { }
): ${targetType.renderShort()} =
$configureOrCreateFunctionName(
lowerCamelCaseName(name, if (compiler == JsCompilerType.both) JsCompilerType.legacy.name else null),
$getPresetsExpression.getByName(
lowerCamelCaseName(
"$presetName",
if (compiler == JsCompilerType.legacy) null else compiler.name
)
) as ${presetType.renderShort()},
configure
)
fun $presetName(
name: String = "$presetName",
configure: ${targetType.renderShort()}.() -> Unit = { }
) = $presetName(name = name, compiler = defaultJsCompilerType, configure = configure)
fun $presetName(
compiler: JsCompilerType,
configure: ${targetType.renderShort()}.() -> Unit = { }
) = $presetName(name = "$presetName", compiler = compiler, configure = configure)
fun $presetName() = $presetName(name = "$presetName") { }
fun $presetName(name: String) = $presetName(name = name) { }
fun $presetName(name: String, configure: Closure<*>) = $presetName(name = name) { ConfigureUtil.configure(configure, this) }
fun $presetName(compiler: JsCompilerType, configure: Closure<*>) = $presetName(compiler = compiler) { ConfigureUtil.configure(configure, this) }
fun $presetName(configure: Closure<*>) = $presetName { ConfigureUtil.configure(configure, this) }
""".trimIndent()
}
}
internal const val MPP_PACKAGE = "org.jetbrains.kotlin.gradle.plugin.mpp" internal const val MPP_PACKAGE = "org.jetbrains.kotlin.gradle.plugin.mpp"
@@ -124,13 +41,6 @@ internal val jvmPresetEntry = KotlinPresetEntry(
typeName("org.jetbrains.kotlin.gradle.targets.jvm.KotlinJvmTarget") typeName("org.jetbrains.kotlin.gradle.targets.jvm.KotlinJvmTarget")
) )
internal val jsPresetEntry = KotlinJsPresetEntry(
"js",
// need for commonization KotlinJsTargetPreset and KotlinJsIrTargetPreset
typeName("org.jetbrains.kotlin.gradle.plugin.KotlinTargetPreset", "org.jetbrains.kotlin.gradle.targets.js.dsl.KotlinJsTargetDsl"),
typeName("org.jetbrains.kotlin.gradle.targets.js.dsl.KotlinJsTargetDsl")
)
internal val androidPresetEntry = KotlinPresetEntry( internal val androidPresetEntry = KotlinPresetEntry(
"android", "android",
typeName("$MPP_PACKAGE.KotlinAndroidTargetPreset"), typeName("$MPP_PACKAGE.KotlinAndroidTargetPreset"),
@@ -160,6 +70,5 @@ internal val nativePresetEntries = HostManager().targets
internal val allPresetEntries = listOf( internal val allPresetEntries = listOf(
jvmPresetEntry, jvmPresetEntry,
jsPresetEntry,
androidPresetEntry androidPresetEntry
) + nativePresetEntries ) + nativePresetEntries
@@ -18,6 +18,7 @@ import org.jetbrains.kotlin.gradle.plugin.mpp.*
open class KotlinMultiplatformExtension : open class KotlinMultiplatformExtension :
KotlinProjectExtension(), KotlinProjectExtension(),
KotlinTargetContainerWithPresetFunctions, KotlinTargetContainerWithPresetFunctions,
KotlinTargetContainerWithJsPresetFunctions,
KotlinTargetContainerWithNativeShortcuts { KotlinTargetContainerWithNativeShortcuts {
override lateinit var presets: NamedDomainObjectCollection<KotlinTargetPreset<*>> override lateinit var presets: NamedDomainObjectCollection<KotlinTargetPreset<*>>
internal set internal set
@@ -83,9 +84,9 @@ internal fun <T : KotlinTarget> KotlinTargetsContainerWithPresets.configureOrCre
else -> { else -> {
throw InvalidUserCodeException( throw InvalidUserCodeException(
"The target '$targetName' already exists, but it was not created with the '${targetPreset.name}' preset. " + "The target '$targetName' already exists, but it was not created with the '${targetPreset.name}' preset. " +
"To configure it, access it by name in `kotlin.targets`" + "To configure it, access it by name in `kotlin.targets`" +
" or use the preset function '${existingTarget.preset?.name}'." " or use the preset function '${existingTarget.preset?.name}'."
.takeIf { existingTarget.preset != null } ?: "." .takeIf { existingTarget.preset != null } ?: "."
) )
} }
} }
@@ -0,0 +1,45 @@
package org.jetbrains.kotlin.gradle.dsl
import groovy.lang.Closure
import org.gradle.util.ConfigureUtil
import org.jetbrains.kotlin.gradle.plugin.JsCompilerType
import org.jetbrains.kotlin.gradle.plugin.KotlinJsCompilerTypeHolder
import org.jetbrains.kotlin.gradle.plugin.KotlinTargetPreset
import org.jetbrains.kotlin.gradle.targets.js.dsl.KotlinJsTargetDsl
import org.jetbrains.kotlin.gradle.utils.lowerCamelCaseName
interface KotlinTargetContainerWithJsPresetFunctions :
KotlinTargetContainerWithPresetFunctions,
KotlinJsCompilerTypeHolder {
fun js(
name: String = "js",
compiler: JsCompilerType = defaultJsCompilerType,
configure: KotlinJsTargetDsl.() -> Unit = { }
): KotlinJsTargetDsl =
configureOrCreate(
lowerCamelCaseName(name, if (compiler == JsCompilerType.both) JsCompilerType.legacy.name else null),
presets.getByName(
lowerCamelCaseName(
"js",
if (compiler == JsCompilerType.legacy) null else compiler.name
)
) as KotlinTargetPreset<KotlinJsTargetDsl>,
configure
)
fun js(
name: String = "js",
configure: KotlinJsTargetDsl.() -> Unit = { }
) = js(name = name, compiler = defaultJsCompilerType, configure = configure)
fun js(
compiler: JsCompilerType,
configure: KotlinJsTargetDsl.() -> Unit = { }
) = js(name = "js", compiler = compiler, configure = configure)
fun js() = js(name = "js") { }
fun js(name: String) = js(name = name) { }
fun js(name: String, configure: Closure<*>) = js(name = name) { ConfigureUtil.configure(configure, this) }
fun js(compiler: JsCompilerType, configure: Closure<*>) = js(compiler = compiler) { ConfigureUtil.configure(configure, this) }
fun js(configure: Closure<*>) = js { ConfigureUtil.configure(configure, this) }
}
@@ -2,18 +2,13 @@ package org.jetbrains.kotlin.gradle.dsl
import groovy.lang.Closure import groovy.lang.Closure
import org.gradle.util.ConfigureUtil import org.gradle.util.ConfigureUtil
import org.jetbrains.kotlin.gradle.plugin.JsCompilerType
import org.jetbrains.kotlin.gradle.plugin.KotlinJsCompilerTypeHolder
import org.jetbrains.kotlin.gradle.plugin.KotlinTargetPreset
import org.jetbrains.kotlin.gradle.plugin.KotlinTargetsContainerWithPresets import org.jetbrains.kotlin.gradle.plugin.KotlinTargetsContainerWithPresets
import org.jetbrains.kotlin.gradle.plugin.mpp.* import org.jetbrains.kotlin.gradle.plugin.mpp.*
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
import org.jetbrains.kotlin.gradle.utils.lowerCamelCaseName
// DO NOT EDIT MANUALLY! Generated by org.jetbrains.kotlin.generators.gradle.dsl.MppPresetFunctionsCodegenKt // DO NOT EDIT MANUALLY! Generated by org.jetbrains.kotlin.generators.gradle.dsl.MppPresetFunctionsCodegenKt
interface KotlinTargetContainerWithPresetFunctions : KotlinTargetsContainerWithPresets, KotlinJsCompilerTypeHolder { interface KotlinTargetContainerWithPresetFunctions : KotlinTargetsContainerWithPresets {
fun jvm( fun jvm(
name: String = "jvm", name: String = "jvm",
@@ -30,38 +25,6 @@ interface KotlinTargetContainerWithPresetFunctions : KotlinTargetsContainerWithP
fun jvm(name: String, configure: Closure<*>) = jvm(name) { ConfigureUtil.configure(configure, this) } fun jvm(name: String, configure: Closure<*>) = jvm(name) { ConfigureUtil.configure(configure, this) }
fun jvm(configure: Closure<*>) = jvm { ConfigureUtil.configure(configure, this) } fun jvm(configure: Closure<*>) = jvm { ConfigureUtil.configure(configure, this) }
fun js(
name: String = "js",
compiler: JsCompilerType = defaultJsCompilerType,
configure: KotlinJsTargetDsl.() -> Unit = { }
): KotlinJsTargetDsl =
configureOrCreate(
lowerCamelCaseName(name, if (compiler == JsCompilerType.both) JsCompilerType.legacy.name else null),
presets.getByName(
lowerCamelCaseName(
"js",
if (compiler == JsCompilerType.legacy) null else compiler.name
)
) as KotlinTargetPreset<KotlinJsTargetDsl>,
configure
)
fun js(
name: String = "js",
configure: KotlinJsTargetDsl.() -> Unit = { }
) = js(name = name, compiler = defaultJsCompilerType, configure = configure)
fun js(
compiler: JsCompilerType,
configure: KotlinJsTargetDsl.() -> Unit = { }
) = js(name = "js", compiler = compiler, configure = configure)
fun js() = js(name = "js") { }
fun js(name: String) = js(name = name) { }
fun js(name: String, configure: Closure<*>) = js(name = name) { ConfigureUtil.configure(configure, this) }
fun js(compiler: JsCompilerType, configure: Closure<*>) = js(compiler = compiler) { ConfigureUtil.configure(configure, this) }
fun js(configure: Closure<*>) = js { ConfigureUtil.configure(configure, this) }
fun android( fun android(
name: String = "android", name: String = "android",
configure: KotlinAndroidTarget.() -> Unit = { } configure: KotlinAndroidTarget.() -> Unit = { }