org.jetbrains.kotlin.gradle.targets.js.ir.KotlinJsIrTargetPreset.kt Maven / Gradle / Ivy
/*
* 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.jetbrains.kotlin.gradle.plugin.*
import org.jetbrains.kotlin.gradle.plugin.mpp.KotlinCompilationFactory
import org.jetbrains.kotlin.gradle.plugin.mpp.KotlinOnlyTarget
import org.jetbrains.kotlin.gradle.plugin.mpp.KotlinOnlyTargetPreset
import org.jetbrains.kotlin.gradle.plugin.statistics.KotlinBuildStatsService
import org.jetbrains.kotlin.gradle.utils.lowerCamelCaseName
import org.jetbrains.kotlin.gradle.utils.runProjectConfigurationHealthCheckWhenEvaluated
import org.jetbrains.kotlin.statistics.metrics.StringMetrics
import org.jetbrains.kotlin.util.capitalizeDecapitalize.decapitalizeAsciiOnly
open class KotlinJsIrTargetPreset(
project: Project
) : KotlinOnlyTargetPreset(
project
) {
internal var mixedMode: Boolean? = null
open val isMpp: Boolean
get() = true
override val platformType: KotlinPlatformType = KotlinPlatformType.js
override fun instantiateTarget(name: String): KotlinJsIrTarget {
return project.objects.newInstance(KotlinJsIrTarget::class.java, project, platformType, mixedMode).apply {
this.isMpp = [email protected]
if (!mixedMode) {
project.runProjectConfigurationHealthCheckWhenEvaluated {
if (!isBrowserConfigured && !isNodejsConfigured) {
project.logger.warn(
"""
Please choose a JavaScript environment to build distributions and run tests.
Not choosing any of them will be an error in the future releases.
kotlin {
js {
// To build distributions for and run tests on browser or Node.js use one or both of:
browser()
nodejs()
}
}
""".trimIndent()
)
}
val buildStatsService = KotlinBuildStatsService.getInstance()
when {
isBrowserConfigured && isNodejsConfigured -> buildStatsService?.report(StringMetrics.JS_TARGET_MODE, "both")
isBrowserConfigured -> buildStatsService?.report(StringMetrics.JS_TARGET_MODE, "browser")
isNodejsConfigured -> buildStatsService?.report(StringMetrics.JS_TARGET_MODE, "nodejs")
!isBrowserConfigured && !isNodejsConfigured -> buildStatsService?.report(StringMetrics.JS_TARGET_MODE, "none")
}
Unit
}
}
}
}
override fun createKotlinTargetConfigurator(): AbstractKotlinTargetConfigurator =
KotlinJsIrTargetConfigurator()
override fun getName(): String = JS_PRESET_NAME
//TODO[Ilya Goncharov] remove public morozov
public override fun createCompilationFactory(
forTarget: KotlinJsIrTarget
): KotlinCompilationFactory =
KotlinJsIrCompilationFactory(forTarget)
companion object {
val JS_PRESET_NAME = lowerCamelCaseName(
"js",
KotlinJsCompilerType.IR.lowerName
)
}
}
class KotlinJsIrSingleTargetPreset(
project: Project
) : KotlinJsIrTargetPreset(
project
) {
override val isMpp: Boolean
get() = false
// In a Kotlin/JS single-platform project, we don't need any disambiguation suffixes or prefixes in the names:
override fun provideTargetDisambiguationClassifier(target: KotlinOnlyTarget): String? {
return if (mixedMode!!) {
super.provideTargetDisambiguationClassifier(target)
?.removePrefix(target.name.removeJsCompilerSuffix(KotlinJsCompilerType.IR))
?.decapitalizeAsciiOnly()
} else {
null
}
}
override fun createKotlinTargetConfigurator(): KotlinOnlyTargetConfigurator =
KotlinJsIrTargetConfigurator()
}