Skip to content

Commit

Permalink
[Gradle, JS] Inject ir's classes into legacy for mixed mode
Browse files Browse the repository at this point in the history
  • Loading branch information
ilgonmic committed Jan 20, 2020
1 parent e0b4b61 commit 72fbb1b
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -157,8 +157,8 @@ open class KotlinJsPluginWrapper @Inject constructor(

return when (propertiesProvider.jsMode) {
JsMode.IR -> KotlinJsIrPlugin(kotlinPluginVersion)
JsMode.LEGACY -> KotlinJsPlugin(kotlinPluginVersion)
JsMode.MIXED -> KotlinJsPlugin(kotlinPluginVersion)
JsMode.LEGACY -> KotlinJsPlugin(kotlinPluginVersion, false)
JsMode.MIXED -> KotlinJsPlugin(kotlinPluginVersion, true)
}
}

Expand All @@ -167,11 +167,9 @@ open class KotlinJsPluginWrapper @Inject constructor(
override fun defineExtension(project: Project) {
val propertiesProvider = PropertiesProvider(project)

when (propertiesProvider.jsMode) {
JsMode.IR -> projectExtensionClass = KotlinJsIrProjectExtension::class
JsMode.LEGACY -> projectExtensionClass = KotlinJsProjectExtension::class
JsMode.MIXED -> {
} //TODO MIXED MODE
projectExtensionClass = when (propertiesProvider.jsMode) {
JsMode.IR -> KotlinJsIrProjectExtension::class
JsMode.LEGACY, JsMode.MIXED -> KotlinJsProjectExtension::class
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,10 +133,21 @@ class KotlinMultiplatformPlugin(
}

fun setupDefaultPresets(project: Project) {
val propertiesProvider = PropertiesProvider(project)

with(project.multiplatformExtension.presets) {
add(KotlinJvmTargetPreset(project, kotlinPluginVersion))
add(KotlinJsTargetPreset(project, kotlinPluginVersion))
add(KotlinJsIrTargetPreset(project, kotlinPluginVersion))
when (propertiesProvider.jsMode) {
JsMode.LEGACY -> add(KotlinJsTargetPreset(project, kotlinPluginVersion, null))
JsMode.IR -> add(KotlinJsIrTargetPreset(project, kotlinPluginVersion))
JsMode.MIXED -> add(
KotlinJsTargetPreset(
project,
kotlinPluginVersion,
KotlinJsIrTargetPreset(project, kotlinPluginVersion)
)
)
}
add(KotlinAndroidTargetPreset(project, kotlinPluginVersion))
add(KotlinJvmWithJavaTargetPreset(project, kotlinPluginVersion))

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@
package org.jetbrains.kotlin.gradle.plugin.mpp

import org.gradle.api.Project
import org.jetbrains.kotlin.gradle.targets.js.ir.KotlinJsIrCompilation

class KotlinJsCompilationFactory(
val project: Project,
val target: KotlinOnlyTarget<KotlinJsCompilation>
val target: KotlinOnlyTarget<KotlinJsCompilation>,
val irTarget: KotlinOnlyTarget<KotlinJsIrCompilation>?
) : KotlinCompilationFactory<KotlinJsCompilation> {
override val itemClass: Class<KotlinJsCompilation>
get() = KotlinJsCompilation::class.java
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,12 @@ import org.jetbrains.kotlin.gradle.dsl.KotlinJsProjectExtension
import org.jetbrains.kotlin.gradle.dsl.kotlinExtension
import org.jetbrains.kotlin.gradle.plugin.configureDefaultVersionsResolutionStrategy
import org.jetbrains.kotlin.gradle.plugin.mpp.KotlinJsSingleTargetPreset
import org.jetbrains.kotlin.gradle.targets.js.ir.KotlinJsIrSingleTargetPreset
import org.jetbrains.kotlin.gradle.utils.checkGradleCompatibility

open class KotlinJsPlugin(
private val kotlinPluginVersion: String
private val kotlinPluginVersion: String,
private val mixedMode: Boolean
) : Plugin<Project> {

override fun apply(project: Project) {
Expand All @@ -29,7 +31,9 @@ open class KotlinJsPlugin(
val kotlinExtension = project.kotlinExtension as KotlinJsProjectExtension
configureDefaultVersionsResolutionStrategy(project, kotlinPluginVersion)

val target = KotlinJsSingleTargetPreset(project, kotlinPluginVersion).createTarget("Js")
val irPrest = if (mixedMode) KotlinJsIrSingleTargetPreset(project, kotlinPluginVersion) else null

val target = KotlinJsSingleTargetPreset(project, kotlinPluginVersion, irPrest).createTarget("Js")

kotlinExtension.target = target

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@ package org.jetbrains.kotlin.gradle.targets.js
import org.gradle.api.NamedDomainObjectContainer
import org.gradle.api.Project
import org.gradle.api.tasks.TaskProvider
import org.jetbrains.kotlin.gradle.plugin.*
import org.jetbrains.kotlin.gradle.plugin.AbstractKotlinTargetConfigurator.Companion.runTaskNameSuffix
import org.jetbrains.kotlin.gradle.plugin.KotlinPlatformType
import org.jetbrains.kotlin.gradle.plugin.KotlinTargetWithTests
import org.jetbrains.kotlin.gradle.plugin.mpp.KotlinJsCompilation
import org.jetbrains.kotlin.gradle.plugin.mpp.KotlinOnlyTarget
import org.jetbrains.kotlin.gradle.targets.js.dsl.KotlinJsBrowserDsl
import org.jetbrains.kotlin.gradle.targets.js.dsl.KotlinJsNodeDsl
import org.jetbrains.kotlin.gradle.targets.js.dsl.KotlinJsTargetDsl
import org.jetbrains.kotlin.gradle.targets.js.ir.KotlinJsIrTarget
import org.jetbrains.kotlin.gradle.targets.js.subtargets.KotlinBrowserJs
import org.jetbrains.kotlin.gradle.targets.js.subtargets.KotlinNodeJs
import org.jetbrains.kotlin.gradle.tasks.locateTask
Expand All @@ -23,11 +25,16 @@ import org.jetbrains.kotlin.gradle.testing.testTaskName
import org.jetbrains.kotlin.gradle.utils.lowerCamelCaseName
import javax.inject.Inject

open class KotlinJsTarget @Inject constructor(project: Project, platformType: KotlinPlatformType) :
open class KotlinJsTarget
@Inject
constructor(
project: Project,
platformType: KotlinPlatformType,
val irTarget: KotlinJsIrTarget?
) :
KotlinOnlyTarget<KotlinJsCompilation>(project, platformType),
KotlinTargetWithTests<JsAggregatingExecutionSource, KotlinJsReportAggregatingTestRun>,
KotlinJsTargetDsl
{
KotlinJsTargetDsl {
override lateinit var testRuns: NamedDomainObjectContainer<KotlinJsReportAggregatingTestRun>
internal set

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,14 @@ import org.gradle.api.Project
import org.jetbrains.kotlin.gradle.plugin.KotlinPlatformType
import org.jetbrains.kotlin.gradle.targets.js.KotlinJsTarget
import org.jetbrains.kotlin.gradle.targets.js.KotlinJsTargetConfigurator
import org.jetbrains.kotlin.gradle.targets.js.ir.KotlinJsIrSingleTargetPreset
import org.jetbrains.kotlin.gradle.targets.js.ir.KotlinJsIrTarget
import org.jetbrains.kotlin.gradle.targets.js.ir.KotlinJsIrTargetPreset

open class KotlinJsTargetPreset(
project: Project,
kotlinPluginVersion: String
kotlinPluginVersion: String,
irPreset: KotlinJsIrTargetPreset?
) : KotlinOnlyTargetPreset<KotlinJsTarget, KotlinJsCompilation>(
project,
kotlinPluginVersion
Expand All @@ -24,15 +28,40 @@ open class KotlinJsTargetPreset(
get() = KotlinPlatformType.js

override fun instantiateTarget(): KotlinJsTarget {
return project.objects.newInstance(KotlinJsTarget::class.java, project, platformType)

val irTarget = KotlinJsIrTarget(project, platformType)

return project.objects.newInstance(
KotlinJsTarget::class.java,
project,
platformType,
irTarget
)
}

override fun createTarget(name: String): KotlinJsTarget {
val result = instantiateTarget().apply {
targetName = name
disambiguationClassifier = provideTargetDisambiguationClassifier(this)
preset = this@KotlinJsTargetPreset

val compilationFactory = createCompilationFactory(this)
compilations = project.container(compilationFactory.itemClass, compilationFactory)
}

createKotlinTargetConfigurator().configureTarget(result)

return result
}

override fun createKotlinTargetConfigurator() = KotlinJsTargetConfigurator(kotlinPluginVersion)

override fun getName(): String = PRESET_NAME

override fun createCompilationFactory(forTarget: KotlinOnlyTarget<KotlinJsCompilation>) =
KotlinJsCompilationFactory(project, forTarget)
override fun createCompilationFactory(forTarget: KotlinOnlyTarget<KotlinJsCompilation>): KotlinJsCompilationFactory {
forTarget as KotlinJsTarget
return KotlinJsCompilationFactory(project, forTarget, forTarget.irTarget)
}

companion object {
const val PRESET_NAME = "js"
Expand All @@ -41,9 +70,13 @@ open class KotlinJsTargetPreset(

class KotlinJsSingleTargetPreset(
project: Project,
kotlinPluginVersion: String
) :
KotlinJsTargetPreset(project, kotlinPluginVersion) {
kotlinPluginVersion: String,
irPreset: KotlinJsIrSingleTargetPreset?
) : KotlinJsTargetPreset(
project,
kotlinPluginVersion,
irPreset
) {

// In a Kotlin/JS single-platform project, we don't need any disambiguation suffixes or prefixes in the names:
override fun provideTargetDisambiguationClassifier(target: KotlinOnlyTarget<KotlinJsCompilation>): String? = null
Expand Down

0 comments on commit 72fbb1b

Please sign in to comment.