[Gradle, JS] UpperCased KotlinJsCompilerType

This commit is contained in:
Ilya Goncharov
2020-02-20 17:26:52 +03:00
parent 9acd98071e
commit 068d547375
12 changed files with 56 additions and 62 deletions
@@ -5,21 +5,10 @@
package org.jetbrains.kotlin.gradle.plugin package org.jetbrains.kotlin.gradle.plugin
import org.gradle.api.Named enum class KotlinJsCompilerType {
import java.io.Serializable LEGACY,
IR,
// For Gradle attributes BOTH;
@Suppress("EnumEntryName")
enum class KotlinJsCompilerType : Named, Serializable {
legacy,
ir,
both;
override fun getName(): String =
name.toLowerCase()
override fun toString(): String =
getName()
companion object { companion object {
const val jsCompilerProperty = "kotlin.js.compiler" const val jsCompilerProperty = "kotlin.js.compiler"
@@ -29,11 +18,14 @@ enum class KotlinJsCompilerType : Named, Serializable {
} }
} }
val KotlinJsCompilerType.lowerName
get() = name.toLowerCase()
fun String.removeJsCompilerSuffix(compilerType: KotlinJsCompilerType): String { fun String.removeJsCompilerSuffix(compilerType: KotlinJsCompilerType): String {
val truncatedString = removeSuffix(compilerType.name) val truncatedString = removeSuffix(compilerType.lowerName)
if (this != truncatedString) { if (this != truncatedString) {
return truncatedString return truncatedString
} }
return removeSuffix(compilerType.name.capitalize()) return removeSuffix(compilerType.lowerName.capitalize())
} }
@@ -39,7 +39,7 @@ class Kotlin2JsIrGradlePluginIT : AbstractKotlin2JsGradlePluginIT(true) {
build( build(
"build", "build",
options = defaultBuildOptions().copy(jsCompilerType = KotlinJsCompilerType.ir) options = defaultBuildOptions().copy(jsCompilerType = KotlinJsCompilerType.IR)
) { ) {
assertSuccessful() assertSuccessful()
@@ -63,7 +63,7 @@ class Kotlin2JsIrGradlePluginIT : AbstractKotlin2JsGradlePluginIT(true) {
build( build(
"build", "build",
options = defaultBuildOptions().copy(jsCompilerType = KotlinJsCompilerType.ir) options = defaultBuildOptions().copy(jsCompilerType = KotlinJsCompilerType.IR)
) { ) {
assertSuccessful() assertSuccessful()
@@ -552,7 +552,7 @@ abstract class AbstractKotlin2JsGradlePluginIT(private val irBackend: Boolean) :
gradleSettingsScript().modify(::transformBuildScriptWithPluginsDsl) gradleSettingsScript().modify(::transformBuildScriptWithPluginsDsl)
if (irBackend) { if (irBackend) {
gradleProperties().appendText(jsCompilerType(KotlinJsCompilerType.ir)) gradleProperties().appendText(jsCompilerType(KotlinJsCompilerType.IR))
} }
build("build") { build("build") {
@@ -9,6 +9,7 @@ import org.jetbrains.kotlin.gradle.internals.*
import org.jetbrains.kotlin.gradle.plugin.KotlinJsCompilerType import org.jetbrains.kotlin.gradle.plugin.KotlinJsCompilerType
import org.jetbrains.kotlin.gradle.plugin.KotlinJsCompilerType.* import org.jetbrains.kotlin.gradle.plugin.KotlinJsCompilerType.*
import org.jetbrains.kotlin.gradle.plugin.ProjectLocalConfigurations import org.jetbrains.kotlin.gradle.plugin.ProjectLocalConfigurations
import org.jetbrains.kotlin.gradle.plugin.lowerName
import org.jetbrains.kotlin.gradle.plugin.mpp.KotlinJvmWithJavaTargetPreset import org.jetbrains.kotlin.gradle.plugin.mpp.KotlinJvmWithJavaTargetPreset
import org.jetbrains.kotlin.gradle.plugin.mpp.KotlinMultiplatformPlugin import org.jetbrains.kotlin.gradle.plugin.mpp.KotlinMultiplatformPlugin
import org.jetbrains.kotlin.gradle.plugin.mpp.NativeOutputKind import org.jetbrains.kotlin.gradle.plugin.mpp.NativeOutputKind
@@ -248,42 +249,42 @@ class NewMultiplatformIT : BaseGradleIT() {
fun testLibAndAppJsLegacy() = doTestLibAndAppJsBothCompilers( fun testLibAndAppJsLegacy() = doTestLibAndAppJsBothCompilers(
"sample-lib", "sample-lib",
"sample-app", "sample-app",
legacy LEGACY
) )
@Test @Test
fun testLibAndAppJsIr() = doTestLibAndAppJsBothCompilers( fun testLibAndAppJsIr() = doTestLibAndAppJsBothCompilers(
"sample-lib", "sample-lib",
"sample-app", "sample-app",
ir IR
) )
@Test @Test
fun testLibAndAppJsBoth() = doTestLibAndAppJsBothCompilers( fun testLibAndAppJsBoth() = doTestLibAndAppJsBothCompilers(
"sample-lib", "sample-lib",
"sample-app", "sample-app",
both BOTH
) )
@Test @Test
fun testLibAndAppWithGradleKotlinDslJsLegacy() = doTestLibAndAppJsBothCompilers( fun testLibAndAppWithGradleKotlinDslJsLegacy() = doTestLibAndAppJsBothCompilers(
"sample-lib-gradle-kotlin-dsl", "sample-lib-gradle-kotlin-dsl",
"sample-app-gradle-kotlin-dsl", "sample-app-gradle-kotlin-dsl",
legacy LEGACY
) )
@Test @Test
fun testLibAndAppWithGradleKotlinDslJsIr() = doTestLibAndAppJsBothCompilers( fun testLibAndAppWithGradleKotlinDslJsIr() = doTestLibAndAppJsBothCompilers(
"sample-lib-gradle-kotlin-dsl", "sample-lib-gradle-kotlin-dsl",
"sample-app-gradle-kotlin-dsl", "sample-app-gradle-kotlin-dsl",
ir IR
) )
@Test @Test
fun testLibAndAppWithGradleKotlinDslJsBoth() = doTestLibAndAppJsBothCompilers( fun testLibAndAppWithGradleKotlinDslJsBoth() = doTestLibAndAppJsBothCompilers(
"sample-lib-gradle-kotlin-dsl", "sample-lib-gradle-kotlin-dsl",
"sample-app-gradle-kotlin-dsl", "sample-app-gradle-kotlin-dsl",
both BOTH
) )
private fun doTestLibAndAppJsBothCompilers( private fun doTestLibAndAppJsBothCompilers(
@@ -296,12 +297,12 @@ class NewMultiplatformIT : BaseGradleIT() {
val compileTasksNames = val compileTasksNames =
listOf( listOf(
*(if (jsCompilerType != both) { *(if (jsCompilerType != BOTH) {
arrayOf("NodeJs") arrayOf("NodeJs")
} else { } else {
arrayOf( arrayOf(
"NodeJs${legacy.name.capitalize()}", "NodeJs${LEGACY.lowerName.capitalize()}",
"NodeJs${ir.name.capitalize()}", "NodeJs${IR.lowerName.capitalize()}",
) )
}), }),
"Metadata" "Metadata"
@@ -316,7 +317,7 @@ class NewMultiplatformIT : BaseGradleIT() {
assertTasksExecuted(*compileTasksNames.toTypedArray(), ":metadataJar") assertTasksExecuted(*compileTasksNames.toTypedArray(), ":metadataJar")
val groupDir = projectDir.resolve("repo/com/example") val groupDir = projectDir.resolve("repo/com/example")
val jsExtension = if (jsCompilerType == legacy) "jar" else "klib" val jsExtension = if (jsCompilerType == LEGACY) "jar" else "klib"
val jsJarName = "sample-lib-nodejs/1.0/sample-lib-nodejs-1.0.$jsExtension" val jsJarName = "sample-lib-nodejs/1.0/sample-lib-nodejs-1.0.$jsExtension"
val metadataJarName = "sample-lib-metadata/1.0/sample-lib-metadata-1.0.jar" val metadataJarName = "sample-lib-metadata/1.0/sample-lib-metadata-1.0.jar"
@@ -340,7 +341,7 @@ class NewMultiplatformIT : BaseGradleIT() {
} }
when (jsCompilerType) { when (jsCompilerType) {
legacy -> { LEGACY -> {
val jsJar = ZipFile(groupDir.resolve(jsJarName)) val jsJar = ZipFile(groupDir.resolve(jsJarName))
val compiledJs = jsJar.getInputStream(jsJar.getEntry("sample-lib.js")).reader().readText() val compiledJs = jsJar.getInputStream(jsJar.getEntry("sample-lib.js")).reader().readText()
Assert.assertTrue("function id(" in compiledJs) Assert.assertTrue("function id(" in compiledJs)
@@ -348,7 +349,7 @@ class NewMultiplatformIT : BaseGradleIT() {
Assert.assertTrue("function expectedFun(" in compiledJs) Assert.assertTrue("function expectedFun(" in compiledJs)
Assert.assertTrue("function main(" in compiledJs) Assert.assertTrue("function main(" in compiledJs)
} }
ir -> { IR -> {
groupDir.resolve(jsJarName).exists() groupDir.resolve(jsJarName).exists()
} }
} }
@@ -382,7 +383,7 @@ class NewMultiplatformIT : BaseGradleIT() {
Assert.assertTrue(resolve("com/example/app/AKt.kotlin_metadata").exists()) Assert.assertTrue(resolve("com/example/app/AKt.kotlin_metadata").exists())
} }
if (jsCompilerType == legacy) { if (jsCompilerType == LEGACY) {
projectDir.resolve(targetClassesDir("nodeJs")).resolve("sample-app.js").readText().run { projectDir.resolve(targetClassesDir("nodeJs")).resolve("sample-app.js").readText().run {
Assert.assertTrue(contains("console.info")) Assert.assertTrue(contains("console.info"))
Assert.assertTrue(contains("function nodeJsMain(")) Assert.assertTrue(contains("function nodeJsMain("))
@@ -397,10 +398,10 @@ class NewMultiplatformIT : BaseGradleIT() {
checkAppBuild(jsCompilerType) checkAppBuild(jsCompilerType)
} }
if (jsCompilerType == both) { if (jsCompilerType == BOTH) {
listOf( listOf(
legacy, LEGACY,
ir IR
).forEach { ).forEach {
build( build(
"assemble", "assemble",
@@ -42,7 +42,7 @@ class VariantAwareDependenciesIT : BaseGradleIT() {
assertContains(">> :${innerProject.projectName}:runtime --> sample-lib-nodejs-1.0.jar") assertContains(">> :${innerProject.projectName}:runtime --> sample-lib-nodejs-1.0.jar")
} }
gradleProperties().appendText(jsCompilerType(KotlinJsCompilerType.ir)) gradleProperties().appendText(jsCompilerType(KotlinJsCompilerType.IR))
testResolveAllConfigurations( testResolveAllConfigurations(
subproject = innerProject.projectName, subproject = innerProject.projectName,
@@ -13,7 +13,7 @@ class WorkersIT : BaseGradleIT() {
fun testParallelTasks() { fun testParallelTasks() {
parallelTasksImpl( parallelTasksImpl(
isParallel = true, isParallel = true,
jsCompilerType = KotlinJsCompilerType.legacy jsCompilerType = KotlinJsCompilerType.LEGACY
) )
} }
@@ -21,7 +21,7 @@ class WorkersIT : BaseGradleIT() {
fun testParallelTasksJsIr() { fun testParallelTasksJsIr() {
parallelTasksImpl( parallelTasksImpl(
isParallel = true, isParallel = true,
jsCompilerType = KotlinJsCompilerType.ir jsCompilerType = KotlinJsCompilerType.IR
) )
} }
@@ -29,7 +29,7 @@ class WorkersIT : BaseGradleIT() {
fun testNoParallelTasks() { fun testNoParallelTasks() {
parallelTasksImpl( parallelTasksImpl(
isParallel = false, isParallel = false,
jsCompilerType = KotlinJsCompilerType.legacy jsCompilerType = KotlinJsCompilerType.LEGACY
) )
} }
@@ -37,7 +37,7 @@ class WorkersIT : BaseGradleIT() {
fun testNoParallelTasksJsIr() { fun testNoParallelTasksJsIr() {
parallelTasksImpl( parallelTasksImpl(
isParallel = false, isParallel = false,
jsCompilerType = KotlinJsCompilerType.ir jsCompilerType = KotlinJsCompilerType.IR
) )
} }
@@ -64,7 +64,7 @@ class WorkersIT : BaseGradleIT() {
kotlinClassesDir(sourceSet = "metadata/main") + "common/A.kotlin_metadata", kotlinClassesDir(sourceSet = "metadata/main") + "common/A.kotlin_metadata",
kotlinClassesDir(sourceSet = "jvm/main") + "common/A.class", kotlinClassesDir(sourceSet = "jvm/main") + "common/A.class",
kotlinClassesDir(sourceSet = "js/main") + kotlinClassesDir(sourceSet = "js/main") +
if (jsCompilerType == KotlinJsCompilerType.ir) "default/manifest" else "new-mpp-parallel.js" if (jsCompilerType == KotlinJsCompilerType.IR) "default/manifest" else "new-mpp-parallel.js"
) )
expectedKotlinOutputFiles.forEach { assertFileExists(it) } expectedKotlinOutputFiles.forEach { assertFileExists(it) }
assertSubstringCount("Loaded GradleKotlinCompilerWork", 1) assertSubstringCount("Loaded GradleKotlinCompilerWork", 1)
@@ -120,13 +120,13 @@ open class KotlinJsProjectExtension :
) { ) {
if (_target == null) { if (_target == null) {
val target: KotlinJsTargetDsl = when (compiler) { val target: KotlinJsTargetDsl = when (compiler) {
legacy -> legacyPreset LEGACY -> legacyPreset
.also { it.irPreset = null } .also { it.irPreset = null }
.createTarget("js") .createTarget("js")
ir -> irPreset IR -> irPreset
.also { it.mixedMode = false } .also { it.mixedMode = false }
.createTarget("js") .createTarget("js")
both -> legacyPreset BOTH -> legacyPreset
.also { .also {
irPreset.mixedMode = true irPreset.mixedMode = true
it.irPreset = irPreset it.irPreset = irPreset
@@ -134,7 +134,7 @@ open class KotlinJsProjectExtension :
.createTarget( .createTarget(
lowerCamelCaseName( lowerCamelCaseName(
"js", "js",
legacy.name LEGACY.lowerName
) )
) )
} }
@@ -5,6 +5,7 @@ import org.gradle.util.ConfigureUtil
import org.jetbrains.kotlin.gradle.plugin.KotlinJsCompilerType import org.jetbrains.kotlin.gradle.plugin.KotlinJsCompilerType
import org.jetbrains.kotlin.gradle.plugin.KotlinJsCompilerTypeHolder import org.jetbrains.kotlin.gradle.plugin.KotlinJsCompilerTypeHolder
import org.jetbrains.kotlin.gradle.plugin.KotlinTargetPreset import org.jetbrains.kotlin.gradle.plugin.KotlinTargetPreset
import org.jetbrains.kotlin.gradle.plugin.lowerName
import org.jetbrains.kotlin.gradle.targets.js.dsl.KotlinJsTargetDsl import org.jetbrains.kotlin.gradle.targets.js.dsl.KotlinJsTargetDsl
import org.jetbrains.kotlin.gradle.utils.lowerCamelCaseName import org.jetbrains.kotlin.gradle.utils.lowerCamelCaseName
@@ -17,11 +18,11 @@ interface KotlinTargetContainerWithJsPresetFunctions :
configure: KotlinJsTargetDsl.() -> Unit = { } configure: KotlinJsTargetDsl.() -> Unit = { }
): KotlinJsTargetDsl = ): KotlinJsTargetDsl =
configureOrCreate( configureOrCreate(
lowerCamelCaseName(name, if (compiler == KotlinJsCompilerType.both) KotlinJsCompilerType.legacy.name else null), lowerCamelCaseName(name, if (compiler == KotlinJsCompilerType.BOTH) KotlinJsCompilerType.LEGACY.lowerName else null),
presets.getByName( presets.getByName(
lowerCamelCaseName( lowerCamelCaseName(
"js", "js",
if (compiler == KotlinJsCompilerType.legacy) null else compiler.name if (compiler == KotlinJsCompilerType.LEGACY) null else compiler.lowerName
) )
) as KotlinTargetPreset<KotlinJsTargetDsl>, ) as KotlinTargetPreset<KotlinJsTargetDsl>,
configure configure
@@ -170,10 +170,10 @@ internal class PropertiesProvider private constructor(private val project: Proje
get() = booleanProperty("kotlin.js.experimental.discoverTypes") get() = booleanProperty("kotlin.js.experimental.discoverTypes")
/** /**
* Use Kotlin/JS backend mode * Use Kotlin/JS backend compiler type
*/ */
val jsCompiler: KotlinJsCompilerType val jsCompiler: KotlinJsCompilerType
get() = property(jsCompilerProperty)?.let { KotlinJsCompilerType.byArgument(it) } ?: KotlinJsCompilerType.legacy get() = property(jsCompilerProperty)?.let { KotlinJsCompilerType.byArgument(it) } ?: KotlinJsCompilerType.LEGACY
private fun propertyWithDeprecatedVariant(propName: String, deprecatedPropName: String): String? { private fun propertyWithDeprecatedVariant(propName: String, deprecatedPropName: String): String? {
val deprecatedProperty = property(deprecatedPropName) val deprecatedProperty = property(deprecatedPropName)
@@ -39,7 +39,7 @@ constructor(
internal set internal set
val disambiguationClassifierInPlatform: String? val disambiguationClassifierInPlatform: String?
get() = disambiguationClassifier?.removeJsCompilerSuffix(KotlinJsCompilerType.legacy) get() = disambiguationClassifier?.removeJsCompilerSuffix(KotlinJsCompilerType.LEGACY)
override val kotlinComponents: Set<KotlinTargetComponent> by lazy { override val kotlinComponents: Set<KotlinTargetComponent> by lazy {
if (irTarget == null) if (irTarget == null)
@@ -71,7 +71,7 @@ constructor(
): KotlinVariant { ): KotlinVariant {
return super.createKotlinVariant(componentName, compilation, usageContexts).apply { return super.createKotlinVariant(componentName, compilation, usageContexts).apply {
irTarget?.let { irTarget?.let {
artifactTargetName = targetName.removeJsCompilerSuffix(KotlinJsCompilerType.legacy) artifactTargetName = targetName.removeJsCompilerSuffix(KotlinJsCompilerType.LEGACY)
} }
} }
} }
@@ -9,10 +9,7 @@
package org.jetbrains.kotlin.gradle.plugin.mpp package org.jetbrains.kotlin.gradle.plugin.mpp
import org.gradle.api.Project import org.gradle.api.Project
import org.jetbrains.kotlin.gradle.plugin.KotlinJsCompilerType import org.jetbrains.kotlin.gradle.plugin.*
import org.jetbrains.kotlin.gradle.plugin.KotlinPlatformType
import org.jetbrains.kotlin.gradle.plugin.removeJsCompilerSuffix
import org.jetbrains.kotlin.gradle.plugin.whenEvaluated
import org.jetbrains.kotlin.gradle.targets.js.KotlinJsTarget import org.jetbrains.kotlin.gradle.targets.js.KotlinJsTarget
import org.jetbrains.kotlin.gradle.targets.js.KotlinJsTargetConfigurator import org.jetbrains.kotlin.gradle.targets.js.KotlinJsTargetConfigurator
import org.jetbrains.kotlin.gradle.targets.js.ir.KotlinJsIrTargetPreset import org.jetbrains.kotlin.gradle.targets.js.ir.KotlinJsIrTargetPreset
@@ -38,7 +35,10 @@ open class KotlinJsTargetPreset(
platformType platformType
).apply { ).apply {
this.irTarget = irPreset?.createTarget( this.irTarget = irPreset?.createTarget(
lowerCamelCaseName(name.removeJsCompilerSuffix(KotlinJsCompilerType.legacy), KotlinJsCompilerType.ir.name) lowerCamelCaseName(
name.removeJsCompilerSuffix(KotlinJsCompilerType.LEGACY),
KotlinJsCompilerType.IR.lowerName
)
) )
project.whenEvaluated { project.whenEvaluated {
@@ -68,7 +68,7 @@ open class KotlinJsTargetPreset(
override fun getName(): String { override fun getName(): String {
return lowerCamelCaseName( return lowerCamelCaseName(
PRESET_NAME, PRESET_NAME,
irPreset?.let { KotlinJsCompilerType.both.name } irPreset?.let { KotlinJsCompilerType.BOTH.lowerName }
) )
} }
@@ -92,7 +92,7 @@ class KotlinJsSingleTargetPreset(
override fun provideTargetDisambiguationClassifier(target: KotlinOnlyTarget<KotlinJsCompilation>): String? = override fun provideTargetDisambiguationClassifier(target: KotlinOnlyTarget<KotlinJsCompilation>): String? =
irPreset?.let { irPreset?.let {
super.provideTargetDisambiguationClassifier(target) super.provideTargetDisambiguationClassifier(target)
?.removePrefix(target.name.removeJsCompilerSuffix(KotlinJsCompilerType.legacy)) ?.removePrefix(target.name.removeJsCompilerSuffix(KotlinJsCompilerType.LEGACY))
?.decapitalize() ?.decapitalize()
} }
@@ -37,7 +37,7 @@ constructor(
internal set internal set
val disambiguationClassifierInPlatform: String? val disambiguationClassifierInPlatform: String?
get() = disambiguationClassifier?.removeJsCompilerSuffix(KotlinJsCompilerType.ir) get() = disambiguationClassifier?.removeJsCompilerSuffix(KotlinJsCompilerType.IR)
var producingType: KotlinJsProducingType? = null var producingType: KotlinJsProducingType? = null
@@ -62,7 +62,7 @@ open class KotlinJsIrTargetPreset(
companion object { companion object {
val PRESET_NAME = lowerCamelCaseName( val PRESET_NAME = lowerCamelCaseName(
"js", "js",
KotlinJsCompilerType.ir.name KotlinJsCompilerType.IR.lowerName
) )
} }
} }
@@ -78,7 +78,7 @@ class KotlinJsIrSingleTargetPreset(
override fun provideTargetDisambiguationClassifier(target: KotlinOnlyTarget<KotlinJsIrCompilation>): String? { override fun provideTargetDisambiguationClassifier(target: KotlinOnlyTarget<KotlinJsIrCompilation>): String? {
return if (mixedMode!!) { return if (mixedMode!!) {
super.provideTargetDisambiguationClassifier(target) super.provideTargetDisambiguationClassifier(target)
?.removePrefix(target.name.removeJsCompilerSuffix(KotlinJsCompilerType.ir)) ?.removePrefix(target.name.removeJsCompilerSuffix(KotlinJsCompilerType.IR))
?.decapitalize() ?.decapitalize()
} else { } else {
null null