Wizard: Move module templates to modules: separate SourcetModuleIR to MultiplatformModuleIR & SourcesetIR
This commit is contained in:
+3
-5
@@ -5,14 +5,12 @@ import org.jetbrains.kotlin.tools.projectWizard.core.safeAs
|
||||
import org.jetbrains.kotlin.tools.projectWizard.ir.buildsystem.gradle.*
|
||||
import org.jetbrains.kotlin.tools.projectWizard.ir.buildsystem.gradle.multiplatform.DefaultTargetConfigurationIR
|
||||
import org.jetbrains.kotlin.tools.projectWizard.ir.buildsystem.gradle.multiplatform.TargetConfigurationIR
|
||||
import org.jetbrains.kotlin.tools.projectWizard.ir.buildsystem.gradle.multiplatform.TargetIR
|
||||
import org.jetbrains.kotlin.tools.projectWizard.plugins.kotlin.ModuleSubType
|
||||
import org.jetbrains.kotlin.tools.projectWizard.plugins.printer.BuildFilePrinter
|
||||
import org.jetbrains.kotlin.tools.projectWizard.plugins.printer.GradlePrinter
|
||||
import org.jetbrains.kotlin.tools.projectWizard.plugins.printer.MavenPrinter
|
||||
import org.jetbrains.kotlin.tools.projectWizard.settings.buildsystem.DefaultRepository
|
||||
import java.nio.file.Path
|
||||
import javax.xml.transform.Source
|
||||
|
||||
data class BuildFileIR(
|
||||
val name: String,
|
||||
@@ -110,7 +108,7 @@ val BuildFileIR.sourcesets
|
||||
get() = modules.modules.flatMap { module ->
|
||||
when (module) {
|
||||
is SingleplatformModuleIR -> module.sourcesets
|
||||
is SourcesetModuleIR -> listOf(module as SourcesetIR)
|
||||
is MultiplatformModuleIR -> listOf(module as SourcesetIR)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -129,11 +127,11 @@ sealed class ModulesStructureIR : BuildSystemIR, IrsOwner {
|
||||
|
||||
data class MultiplatformModulesStructureIR(
|
||||
val targets: List<BuildSystemIR>,
|
||||
override val modules: List<SourcesetModuleIR>,
|
||||
override val modules: List<MultiplatformModuleIR>,
|
||||
override val irs: List<BuildSystemIR>
|
||||
) : GradleIR, ModulesStructureIR() {
|
||||
@Suppress("UNCHECKED_CAST")
|
||||
override fun withModules(modules: List<ModuleIR>) = copy(modules = modules as List<SourcesetModuleIR>)
|
||||
override fun withModules(modules: List<ModuleIR>) = copy(modules = modules as List<MultiplatformModuleIR>)
|
||||
|
||||
override fun withReplacedIrs(irs: List<BuildSystemIR>): MultiplatformModulesStructureIR = copy(irs = irs)
|
||||
|
||||
|
||||
+9
-37
@@ -17,15 +17,10 @@ sealed class ModuleIR : IrsOwner, BuildSystemIR {
|
||||
abstract val template: Template?
|
||||
abstract val type: ModuleType
|
||||
abstract val originalModule: Module
|
||||
abstract val sourcesets: List<SourcesetIR>
|
||||
}
|
||||
|
||||
|
||||
interface SourcesetIR : BuildSystemIR {
|
||||
val sourcesetType: SourcesetType
|
||||
val path: Path
|
||||
val original: Sourceset
|
||||
}
|
||||
|
||||
data class SingleplatformModuleIR(
|
||||
override val name: String,
|
||||
override val path: Path,
|
||||
@@ -33,7 +28,7 @@ data class SingleplatformModuleIR(
|
||||
override val template: Template?,
|
||||
override val type: ModuleType,
|
||||
override val originalModule: Module,
|
||||
val sourcesets: List<SingleplatformSourcesetIR>
|
||||
override val sourcesets: List<SingleplatformSourcesetIR>
|
||||
) : ModuleIR() {
|
||||
override fun withReplacedIrs(irs: List<BuildSystemIR>): SingleplatformModuleIR = copy(irs = irs)
|
||||
|
||||
@@ -51,42 +46,19 @@ data class SingleplatformModuleIR(
|
||||
}
|
||||
}
|
||||
|
||||
data class SingleplatformSourcesetIR(
|
||||
override val sourcesetType: SourcesetType,
|
||||
override val path: Path,
|
||||
override val irs: List<BuildSystemIR>,
|
||||
override val original: Sourceset
|
||||
) : SourcesetIR, IrsOwner {
|
||||
override fun withReplacedIrs(irs: List<BuildSystemIR>): SingleplatformSourcesetIR = copy(irs = irs)
|
||||
override fun BuildFilePrinter.render() = Unit
|
||||
}
|
||||
|
||||
data class SourcesetModuleIR(
|
||||
data class MultiplatformModuleIR(
|
||||
override val name: String,
|
||||
override val path: Path,
|
||||
override val irs: List<BuildSystemIR>,
|
||||
override val type: ModuleType,
|
||||
override val sourcesetType: SourcesetType,
|
||||
override val template: Template?,
|
||||
override val originalModule: Module,
|
||||
override val original: Sourceset
|
||||
) : SourcesetIR, GradleIR, ModuleIR() {
|
||||
override fun withReplacedIrs(irs: List<BuildSystemIR>): SourcesetModuleIR = copy(irs = irs)
|
||||
override val sourcesets: List<MultiplatformSourcesetIR>
|
||||
) : GradleIR, ModuleIR() {
|
||||
override fun withReplacedIrs(irs: List<BuildSystemIR>): MultiplatformModuleIR = copy(irs = irs)
|
||||
|
||||
override fun GradlePrinter.renderGradle() = getting(name, prefix = null) {
|
||||
val dependencies = irsOfType<DependencyIR>()
|
||||
val needBody = dependencies.isNotEmpty() || dsl == GradlePrinter.GradleDsl.GROOVY
|
||||
if (needBody) {
|
||||
+" "
|
||||
inBrackets {
|
||||
if (dependencies.isNotEmpty()) {
|
||||
indent()
|
||||
sectionCall("dependencies", dependencies)
|
||||
}
|
||||
}
|
||||
}
|
||||
override fun GradlePrinter.renderGradle() {
|
||||
sourcesets.listNl(needFirstIndent = false)
|
||||
}
|
||||
}
|
||||
|
||||
val SourcesetModuleIR.targetName
|
||||
get() = name.removeSuffix(sourcesetType.name.capitalize())
|
||||
}
|
||||
+57
@@ -0,0 +1,57 @@
|
||||
/*
|
||||
* 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.tools.projectWizard.ir.buildsystem
|
||||
|
||||
import org.jetbrains.kotlin.tools.projectWizard.ir.buildsystem.gradle.GradleIR
|
||||
import org.jetbrains.kotlin.tools.projectWizard.plugins.printer.BuildFilePrinter
|
||||
import org.jetbrains.kotlin.tools.projectWizard.plugins.printer.GradlePrinter
|
||||
import org.jetbrains.kotlin.tools.projectWizard.settings.buildsystem.Sourceset
|
||||
import org.jetbrains.kotlin.tools.projectWizard.settings.buildsystem.SourcesetType
|
||||
import java.nio.file.Path
|
||||
|
||||
|
||||
sealed class SourcesetIR : BuildSystemIR {
|
||||
abstract val sourcesetType: SourcesetType
|
||||
abstract val path: Path
|
||||
abstract val original: Sourceset
|
||||
}
|
||||
|
||||
data class SingleplatformSourcesetIR(
|
||||
override val sourcesetType: SourcesetType,
|
||||
override val path: Path,
|
||||
override val irs: List<BuildSystemIR>,
|
||||
override val original: Sourceset
|
||||
) : SourcesetIR(), IrsOwner {
|
||||
override fun withReplacedIrs(irs: List<BuildSystemIR>): SingleplatformSourcesetIR = copy(irs = irs)
|
||||
override fun BuildFilePrinter.render() = Unit
|
||||
}
|
||||
|
||||
data class MultiplatformSourcesetIR(
|
||||
override val sourcesetType: SourcesetType,
|
||||
override val path: Path,
|
||||
val targetName: String,
|
||||
override val irs: List<BuildSystemIR>,
|
||||
override val original: Sourceset
|
||||
) : SourcesetIR(), IrsOwner, GradleIR {
|
||||
override fun withReplacedIrs(irs: List<BuildSystemIR>): MultiplatformSourcesetIR = copy(irs = irs)
|
||||
|
||||
override fun GradlePrinter.renderGradle() = getting(sourcesetName, prefix = null) {
|
||||
val dependencies = irsOfType<DependencyIR>()
|
||||
val needBody = dependencies.isNotEmpty() || dsl == GradlePrinter.GradleDsl.GROOVY
|
||||
if (needBody) {
|
||||
+" "
|
||||
inBrackets {
|
||||
if (dependencies.isNotEmpty()) {
|
||||
indent()
|
||||
sectionCall("dependencies", dependencies)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
val MultiplatformSourcesetIR.sourcesetName
|
||||
get() = targetName + sourcesetType.name.capitalize()
|
||||
+2
-7
@@ -68,13 +68,8 @@ class KotlinPlugin(context: Context) : Plugin(context) {
|
||||
}
|
||||
|
||||
forEachModule { moduleIR ->
|
||||
when (moduleIR) {
|
||||
is SingleplatformModuleIR -> moduleIR.sourcesets.mapSequenceIgnore { sourcesetIR ->
|
||||
sourcesetIR.path.createKotlinAndResourceDirectories()
|
||||
}
|
||||
is SourcesetModuleIR -> {
|
||||
moduleIR.path.createKotlinAndResourceDirectories()
|
||||
}
|
||||
moduleIR.sourcesets.mapSequenceIgnore { sourcesetIR ->
|
||||
sourcesetIR.path.createKotlinAndResourceDirectories()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+18
-11
@@ -160,7 +160,7 @@ class ModulesToIRsConverter(
|
||||
(subModule.configurator as TargetConfigurator).createTargetIrs(subModule)
|
||||
}
|
||||
|
||||
val sourcesetsIrs = module.subModules.flatMap { target ->
|
||||
val targetModuleIrs = module.subModules.map { target ->
|
||||
createTargetSourceset(target, modulePath)
|
||||
}
|
||||
|
||||
@@ -169,7 +169,7 @@ class ModulesToIRsConverter(
|
||||
modulePath,
|
||||
MultiplatformModulesStructureIR(
|
||||
targetIrs,
|
||||
sourcesetsIrs,
|
||||
targetModuleIrs,
|
||||
emptyList()
|
||||
),
|
||||
pomIr,
|
||||
@@ -180,8 +180,8 @@ class ModulesToIRsConverter(
|
||||
).asSingletonList().asSuccess()
|
||||
}
|
||||
|
||||
private fun createTargetSourceset(target: Module, modulePath: Path): List<SourcesetModuleIR> =
|
||||
target.sourcesets.map { sourceset ->
|
||||
private fun createTargetSourceset(target: Module, modulePath: Path): MultiplatformModuleIR {
|
||||
val sourcesetss = target.sourcesets.map { sourceset ->
|
||||
val sourcesetName = target.name + sourceset.sourcesetType.name.capitalize()
|
||||
val sourcesetIrs = buildList<BuildSystemIR> {
|
||||
+sourceset.dependencies.map { it.toIR(sourceset.sourcesetType.toDependencyType()) }
|
||||
@@ -198,17 +198,24 @@ class ModulesToIRsConverter(
|
||||
)
|
||||
}
|
||||
}
|
||||
SourcesetModuleIR(
|
||||
sourcesetName,
|
||||
modulePath / Defaults.SRC_DIR / sourcesetName,
|
||||
sourcesetIrs,
|
||||
sourceset.containingModuleType,
|
||||
MultiplatformSourcesetIR(
|
||||
sourceset.sourcesetType,
|
||||
target.template,
|
||||
target,
|
||||
modulePath / Defaults.SRC_DIR / sourcesetName,
|
||||
target.name,
|
||||
sourcesetIrs,
|
||||
sourceset
|
||||
)
|
||||
}
|
||||
return MultiplatformModuleIR(
|
||||
target.name,
|
||||
modulePath,
|
||||
emptyList(),
|
||||
target.configurator.moduleType,
|
||||
target.template,
|
||||
target,
|
||||
sourcesetss
|
||||
)
|
||||
}
|
||||
|
||||
private fun TaskRunningContext.mutateProjectStructureByModuleConfigurator(
|
||||
module: Module,
|
||||
|
||||
+2
-2
@@ -52,8 +52,8 @@ abstract class BuildFilePrinter {
|
||||
suffix()
|
||||
}
|
||||
|
||||
open fun <T : BuildSystemIR> List<T>.listNl() {
|
||||
indent()
|
||||
open fun <T : BuildSystemIR> List<T>.listNl(needFirstIndent: Boolean = true) {
|
||||
if (needFirstIndent) indent()
|
||||
list(separator = { nl(); indent() }) { it.render(this) }
|
||||
}
|
||||
|
||||
|
||||
+1
-1
@@ -36,7 +36,7 @@ MavenPrinter(override val indent: Int = 4) : BuildFilePrinter() {
|
||||
}
|
||||
|
||||
// We already have line break after node
|
||||
override fun <T : BuildSystemIR> List<T>.listNl() {
|
||||
override fun <T : BuildSystemIR> List<T>.listNl(needFirstIndent: Boolean) {
|
||||
list(separator = { +"" }) { it.render(this) }
|
||||
}
|
||||
|
||||
|
||||
+8
-9
@@ -112,8 +112,8 @@ class TemplatesPlugin(context: Context) : Plugin(context) {
|
||||
val template = module.template ?: return UNIT_SUCCESS
|
||||
val settings = with(template) { settingsAsMap(module.originalModule) }
|
||||
val allSettings = settings + interceptionPointSettings.mapKeys { it.key.name }
|
||||
return with(template) { getFileTemplates(module) }.mapNotNull { (fileTemplateDescriptor, filePath) ->
|
||||
val path = generatePathForFileTemplate(module, filePath) ?: return@mapNotNull null
|
||||
return with(template) { getFileTemplates(module) }.map { (fileTemplateDescriptor, filePath) ->
|
||||
val path = generatePathForFileTemplate(module, filePath)
|
||||
val fileTemplate = FileTemplate(
|
||||
fileTemplateDescriptor,
|
||||
module.path / path,
|
||||
@@ -131,13 +131,12 @@ class TemplatesPlugin(context: Context) : Plugin(context) {
|
||||
}
|
||||
}
|
||||
|
||||
is SourcesetModuleIR -> {
|
||||
if (filePath.sourcesetType == module.sourcesetType) {
|
||||
when (filePath) {
|
||||
is SrcFilePath -> KOTLIN_DIR
|
||||
is ResourcesFilePath -> RESOURCES_DIR
|
||||
}
|
||||
} else null
|
||||
is MultiplatformModuleIR -> {
|
||||
val directory = when (filePath) {
|
||||
is SrcFilePath -> KOTLIN_DIR
|
||||
is ResourcesFilePath -> RESOURCES_DIR
|
||||
}
|
||||
SRC_DIR / "${module.name}${filePath.sourcesetType.name.capitalize()}" / directory
|
||||
}
|
||||
}
|
||||
}
|
||||
-1
@@ -9,7 +9,6 @@ import org.jetbrains.kotlin.tools.projectWizard.core.TaskRunningContext
|
||||
import org.jetbrains.kotlin.tools.projectWizard.core.asPath
|
||||
import org.jetbrains.kotlin.tools.projectWizard.core.buildList
|
||||
import org.jetbrains.kotlin.tools.projectWizard.ir.buildsystem.ModuleIR
|
||||
import org.jetbrains.kotlin.tools.projectWizard.ir.buildsystem.SourcesetIR
|
||||
import org.jetbrains.kotlin.tools.projectWizard.ir.buildsystem.gradle.multiplatform.NativeTargetInternalIR
|
||||
import org.jetbrains.kotlin.tools.projectWizard.ir.buildsystem.gradle.multiplatform.TargetConfigurationIR
|
||||
import org.jetbrains.kotlin.tools.projectWizard.ir.buildsystem.withIrs
|
||||
|
||||
+2
-3
@@ -18,7 +18,6 @@ import org.jetbrains.kotlin.tools.projectWizard.plugins.kotlin.ModuleSubType
|
||||
import org.jetbrains.kotlin.tools.projectWizard.plugins.kotlin.ModuleType
|
||||
import org.jetbrains.kotlin.tools.projectWizard.settings.buildsystem.DefaultRepository
|
||||
import org.jetbrains.kotlin.tools.projectWizard.settings.buildsystem.Module
|
||||
import org.jetbrains.kotlin.tools.projectWizard.settings.buildsystem.Sourceset
|
||||
import org.jetbrains.kotlin.tools.projectWizard.settings.buildsystem.SourcesetType
|
||||
import org.jetbrains.kotlin.tools.projectWizard.settings.version.Version
|
||||
import org.jetbrains.kotlin.tools.projectWizard.transformers.interceptors.TemplateInterceptor
|
||||
@@ -83,7 +82,7 @@ class SimpleJsClientTemplate : Template() {
|
||||
}
|
||||
|
||||
transformBuildFile { buildFileIR ->
|
||||
val jsSourcesetName = module.safeAs<SourcesetModuleIR>()?.targetName ?: return@transformBuildFile null
|
||||
val jsSourcesetName = module.safeAs<MultiplatformModuleIR>()?.name ?: return@transformBuildFile null
|
||||
val jvmTarget = buildFileIR.targets.firstOrNull { target ->
|
||||
target.safeAs<DefaultTargetConfigurationIR>()?.targetAccess?.type == ModuleSubType.jvm
|
||||
} as? DefaultTargetConfigurationIR ?: return@transformBuildFile null
|
||||
@@ -136,7 +135,7 @@ class SimpleJsClientTemplate : Template() {
|
||||
|
||||
override fun TaskRunningContext.getIrsToAddToBuildFile(module: ModuleIR): List<BuildSystemIR> = buildList {
|
||||
+RepositoryIR(DefaultRepository.JCENTER)
|
||||
if (module is SourcesetModuleIR) {
|
||||
if (module is MultiplatformModuleIR) {
|
||||
+GradleImportIR("org.jetbrains.kotlin.gradle.targets.js.webpack.KotlinWebpack")
|
||||
val taskAccessIR = GradleByNameTaskAccessIR(
|
||||
"${module.name}BrowserWebpack",
|
||||
|
||||
+1
-1
@@ -89,7 +89,7 @@ abstract class Template : SettingsOwner {
|
||||
val irsToAddToBuildFile = getIrsToAddToBuildFile(module)
|
||||
|
||||
val targetsUpdater = when (module) {
|
||||
is SourcesetModuleIR -> { target: TargetConfigurationIR ->
|
||||
is MultiplatformModuleIR -> { target: TargetConfigurationIR ->
|
||||
if (target.targetName == module.name) updateTargetIr(module, target)
|
||||
else target
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user