Skip to content

Commit

Permalink
Rename Target to KpmTarget to avoid compilation problems
Browse files Browse the repository at this point in the history
  • Loading branch information
terrakok committed Mar 21, 2023
1 parent 798d211 commit 319d884
Show file tree
Hide file tree
Showing 11 changed files with 64 additions and 64 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@ ${generatePluginsBlock()}
${if (GradlePlugin.PUBLISH in projectInfo.gradlePlugins) generatePublishPluginConfig("0.1") else NAN}
${generateKotlinBlock()}
${if (Target.ANDROID in projectInfo.targets) generateAndroidPluginConfig(projectInfo, "21", "31") else NAN}
${if (KmpTarget.ANDROID in projectInfo.targets) generateAndroidPluginConfig(projectInfo, "21", "31") else NAN}
${if (GradlePlugin.APPLICATION in projectInfo.gradlePlugins) generateApplicationPluginConfig() else NAN}
""".trimIndent().deleteNans()

private fun generatePluginsBlock(): String = """
plugins {
kotlin("multiplatform")
${if (KmpLibrary.SERIALIZATION in projectInfo.dependencies) "kotlin(\"plugin.serialization\")" else NAN}
${if (Target.ANDROID in projectInfo.targets) "id(\"com.android.library\")" else NAN}
${if (KmpTarget.ANDROID in projectInfo.targets) "id(\"com.android.library\")" else NAN}
${if (GradlePlugin.APPLICATION in projectInfo.gradlePlugins) "application" else NAN}
${if (GradlePlugin.PUBLISH in projectInfo.gradlePlugins) "`maven-publish`" else NAN}
${if (GradlePlugin.SQL_DELIGHT in projectInfo.gradlePlugins) "id(\"com.squareup.sqldelight\")" else NAN}
Expand All @@ -43,18 +43,18 @@ kotlin {
""".trimIndent()

private fun registerTargets(
targets: Set<Target>
targets: Set<KmpTarget>
) = targets.joinToString("\n ") {
when (it) {
Target.ANDROID -> "android()"
Target.JVM -> if (projectInfo.gradlePlugins.contains(GradlePlugin.APPLICATION)) "jvm {\n withJava()\n }" else "jvm()"
Target.JS -> "js(IR) {\n browser()\n nodejs()\n }"
Target.LINUX -> "linuxX64()"
Target.MACOS -> "macosX64()\n macosArm64()"
Target.IOS -> "iosX64()\n iosArm64()\n iosSimulatorArm64()"
Target.TV_OS -> "tvosX64()\n tvosArm64()\n tvosSimulatorArm64()"
Target.WATCH_OS -> "watchosX64()\n watchosArm64()\n watchosSimulatorArm64()"
Target.WINDOWS -> "mingwX64()"
KmpTarget.ANDROID -> "android()"
KmpTarget.JVM -> if (projectInfo.gradlePlugins.contains(GradlePlugin.APPLICATION)) "jvm {\n withJava()\n }" else "jvm()"
KmpTarget.JS -> "js(IR) {\n browser()\n nodejs()\n }"
KmpTarget.LINUX -> "linuxX64()"
KmpTarget.MACOS -> "macosX64()\n macosArm64()"
KmpTarget.IOS -> "iosX64()\n iosArm64()\n iosSimulatorArm64()"
KmpTarget.TV_OS -> "tvosX64()\n tvosArm64()\n tvosSimulatorArm64()"
KmpTarget.WATCH_OS -> "watchosX64()\n watchosArm64()\n watchosSimulatorArm64()"
KmpTarget.WINDOWS -> "mingwX64()"
}
}

Expand Down Expand Up @@ -102,7 +102,7 @@ kotlin {
}

private fun sourceSet(
target: Target,
target: KmpTarget,
deps: List<String>,
type: SourceSetType,
sourceSetDelegate: SourceSetDelegate
Expand All @@ -119,7 +119,7 @@ kotlin {
}
}

private fun Target.getDeps(compilation: SourceSetType) = projectInfo.dependencies
private fun KmpTarget.getDeps(compilation: SourceSetType) = projectInfo.dependencies
.filter { it.targets.size == 1 && it.targets.single() == this && it.sourceSetType == compilation }
.map { "libs." + it.versionCatalogName }

Expand All @@ -128,13 +128,13 @@ kotlin {
val intention = "\n| "
return projectInfo.targets.joinToString(intention) {
when (it) {
Target.ANDROID, Target.JVM, Target.JS -> NAN
Target.LINUX -> "val linuxX64$compilation by getting"
Target.MACOS -> "val macosX64$compilation by getting ${intention}val macosArm64$compilation by getting"
Target.IOS -> "val iosX64$compilation by getting ${intention}val iosArm64$compilation by getting${intention}val iosSimulatorArm64$compilation by getting"
Target.TV_OS -> "val tvosX64$compilation by getting ${intention}val tvosArm64$compilation by getting ${intention}val tvosSimulatorArm64$compilation by getting"
Target.WATCH_OS -> "val watchosX64$compilation by getting ${intention}val watchosArm64$compilation by getting ${intention}val watchosSimulatorArm64$compilation by getting"
Target.WINDOWS -> "val mingwX64$compilation by getting"
KmpTarget.ANDROID, KmpTarget.JVM, KmpTarget.JS -> NAN
KmpTarget.LINUX -> "val linuxX64$compilation by getting"
KmpTarget.MACOS -> "val macosX64$compilation by getting ${intention}val macosArm64$compilation by getting"
KmpTarget.IOS -> "val iosX64$compilation by getting ${intention}val iosArm64$compilation by getting${intention}val iosSimulatorArm64$compilation by getting"
KmpTarget.TV_OS -> "val tvosX64$compilation by getting ${intention}val tvosArm64$compilation by getting ${intention}val tvosSimulatorArm64$compilation by getting"
KmpTarget.WATCH_OS -> "val watchosX64$compilation by getting ${intention}val watchosArm64$compilation by getting ${intention}val watchosSimulatorArm64$compilation by getting"
KmpTarget.WINDOWS -> "val mingwX64$compilation by getting"
}
}
}
Expand All @@ -155,9 +155,9 @@ kotlin {

return projectInfo.targets.joinToString("\n| ") {
when (it) {
Target.ANDROID, Target.JVM, Target.JS ->
KmpTarget.ANDROID, KmpTarget.JVM, KmpTarget.JS ->
sourceSet(it, it.getDeps(type), type, GETTING)
Target.LINUX, Target.MACOS, Target.TV_OS, Target.WATCH_OS, Target.WINDOWS, Target.IOS ->
KmpTarget.LINUX, KmpTarget.MACOS, KmpTarget.TV_OS, KmpTarget.WATCH_OS, KmpTarget.WINDOWS, KmpTarget.IOS ->
sourceSet(it, it.getDeps(type) + additionalDeps, type, CREATING)
}
}
Expand All @@ -171,17 +171,17 @@ kotlin {
val nativeParent = if (commonNative) "native$compilation" else "common$compilation"
val nativeSourceSet = if (commonNative) "native$compilation.dependsOn(common$compilation)$intention" else ""

fun sourceSetName(target: Target) = if (type == MAIN) target.mainSourceSetName else target.testSourceSetName
fun sourceSetName(target: KmpTarget) = if (type == MAIN) target.mainSourceSetName else target.testSourceSetName

return nativeSourceSet + projectInfo.targets.joinToString(intention) {
when (it) {
Target.ANDROID, Target.JVM, Target.JS -> "${sourceSetName(it)}.dependsOn(common$compilation)"
Target.LINUX -> "${sourceSetName(it)}.dependsOn($nativeParent)${intention}linuxX64$compilation.dependsOn(linux$compilation)"
Target.MACOS -> "${sourceSetName(it)}.dependsOn($nativeParent)${intention}macosX64$compilation.dependsOn(macos$compilation)${intention}macosArm64$compilation.dependsOn(macos$compilation)"
Target.IOS -> "${sourceSetName(it)}.dependsOn($nativeParent)${intention}iosX64$compilation.dependsOn(ios$compilation)${intention}iosArm64$compilation.dependsOn(ios$compilation)${intention}iosSimulatorArm64$compilation.dependsOn(ios$compilation)"
Target.TV_OS -> "${sourceSetName(it)}.dependsOn($nativeParent)${intention}tvosX64$compilation.dependsOn(tvos$compilation)${intention}tvosArm64$compilation.dependsOn(tvos$compilation)${intention}tvosSimulatorArm64$compilation.dependsOn(tvos$compilation)"
Target.WATCH_OS -> "${sourceSetName(it)}.dependsOn($nativeParent)${intention}watchosX64$compilation.dependsOn(watchos$compilation)${intention}watchosArm64$compilation.dependsOn(watchos$compilation)${intention}watchosSimulatorArm64$compilation.dependsOn(watchos$compilation)"
Target.WINDOWS -> "${sourceSetName(it)}.dependsOn($nativeParent)${intention}mingwX64$compilation.dependsOn(windows$compilation)"
KmpTarget.ANDROID, KmpTarget.JVM, KmpTarget.JS -> "${sourceSetName(it)}.dependsOn(common$compilation)"
KmpTarget.LINUX -> "${sourceSetName(it)}.dependsOn($nativeParent)${intention}linuxX64$compilation.dependsOn(linux$compilation)"
KmpTarget.MACOS -> "${sourceSetName(it)}.dependsOn($nativeParent)${intention}macosX64$compilation.dependsOn(macos$compilation)${intention}macosArm64$compilation.dependsOn(macos$compilation)"
KmpTarget.IOS -> "${sourceSetName(it)}.dependsOn($nativeParent)${intention}iosX64$compilation.dependsOn(ios$compilation)${intention}iosArm64$compilation.dependsOn(ios$compilation)${intention}iosSimulatorArm64$compilation.dependsOn(ios$compilation)"
KmpTarget.TV_OS -> "${sourceSetName(it)}.dependsOn($nativeParent)${intention}tvosX64$compilation.dependsOn(tvos$compilation)${intention}tvosArm64$compilation.dependsOn(tvos$compilation)${intention}tvosSimulatorArm64$compilation.dependsOn(tvos$compilation)"
KmpTarget.WATCH_OS -> "${sourceSetName(it)}.dependsOn($nativeParent)${intention}watchosX64$compilation.dependsOn(watchos$compilation)${intention}watchosArm64$compilation.dependsOn(watchos$compilation)${intention}watchosSimulatorArm64$compilation.dependsOn(watchos$compilation)"
KmpTarget.WINDOWS -> "${sourceSetName(it)}.dependsOn($nativeParent)${intention}mingwX64$compilation.dependsOn(windows$compilation)"
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package org.jetbrains.webwiz.generator.files

import org.jetbrains.webwiz.generator.ProjectFile
import org.jetbrains.webwiz.models.ProjectInfo
import org.jetbrains.webwiz.models.Target
import org.jetbrains.webwiz.models.KmpTarget

class CommonPlatformKt(val projectInfo: ProjectInfo) : ProjectFile {
override val path = "${projectInfo.moduleName}/src/commonMain/kotlin" +
Expand All @@ -20,7 +20,7 @@ class Greeting {
""".trimIndent()
}

class TargetPlatformKt(val target: Target, val projectInfo: ProjectInfo) : ProjectFile {
class TargetPlatformKt(val target: KmpTarget, val projectInfo: ProjectInfo) : ProjectFile {
override val path = "${projectInfo.moduleName}/src/${target.mainSourceSetName}/kotlin" +
"/${projectInfo.packageName.replace('.', '/')}" +
"/Platform.${target.targetName}.kt"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package org.jetbrains.webwiz.generator.files

import org.jetbrains.webwiz.generator.ProjectFile
import org.jetbrains.webwiz.models.ProjectInfo
import org.jetbrains.webwiz.models.Target
import org.jetbrains.webwiz.models.KmpTarget
import org.jetbrains.webwiz.models.isJvm

class CommonPlatformTestKt(val projectInfo: ProjectInfo) : ProjectFile {
Expand All @@ -26,7 +26,7 @@ class CommonTest {
""".trimIndent()
}

class TargetPlatformTestKt(val target: Target, val projectInfo: ProjectInfo) :
class TargetPlatformTestKt(val target: KmpTarget, val projectInfo: ProjectInfo) :
ProjectFile {
override val path = "${projectInfo.moduleName}/src/${target.testSourceSetName}/kotlin" +
"/${projectInfo.packageName.replace('.', '/')}" +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import org.jetbrains.webwiz.generator.ProjectFile
import org.jetbrains.webwiz.models.GradlePlugin
import org.jetbrains.webwiz.models.KmpLibrary
import org.jetbrains.webwiz.models.ProjectInfo
import org.jetbrains.webwiz.models.Target
import org.jetbrains.webwiz.models.KmpTarget

class RootBuildGradle(val projectInfo: ProjectInfo) : ProjectFile {
override val path = "build.gradle.kts"
Expand All @@ -15,7 +15,7 @@ class RootBuildGradle(val projectInfo: ProjectInfo) : ProjectFile {
if (projectInfo.dependencies.contains(KmpLibrary.SERIALIZATION)) {
appendLine(" kotlin(\"plugin.serialization\").apply(false)")
}
if (projectInfo.targets.contains(Target.ANDROID)) {
if (projectInfo.targets.contains(KmpTarget.ANDROID)) {
appendLine(" id(\"com.android.library\").apply(false)")
}
if (GradlePlugin.SQL_DELIGHT in projectInfo.gradlePlugins) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class SettingsGradle(val projectInfo: ProjectInfo) : ProjectFile {
if (projectInfo.dependencies.contains(KmpLibrary.SERIALIZATION)) {
appendLine(" kotlin(\"plugin.serialization\").version(\"${projectInfo.kotlinVersion.versionName}\")")
}
if (projectInfo.targets.contains(Target.ANDROID)) {
if (projectInfo.targets.contains(KmpTarget.ANDROID)) {
appendLine(" id(\"com.android.library\").version(\"$AndroidGradlePluginVersion\")")
}
if (GradlePlugin.SQL_DELIGHT in projectInfo.gradlePlugins) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package org.jetbrains.webwiz.models

enum class GradlePlugin(
val mandatory: Set<Target>,
val forbidden: Set<Target>,
val mandatory: Set<KmpTarget>,
val forbidden: Set<KmpTarget>,
val userName: String
) {
PUBLISH(
Expand All @@ -11,8 +11,8 @@ enum class GradlePlugin(
"Maven Publish"
),
APPLICATION(
setOf(Target.JVM),
setOf(Target.ANDROID),
setOf(KmpTarget.JVM),
setOf(KmpTarget.ANDROID),
"JVM Application"
),
SQL_DELIGHT(
Expand All @@ -26,6 +26,6 @@ enum class GradlePlugin(
"Realm Kotlin Multiplatform"
);

fun canBeApplied(targets: Set<Target>): Boolean =
fun canBeApplied(targets: Set<KmpTarget>): Boolean =
targets.containsAll(mandatory) && !targets.any { it in forbidden }
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package org.jetbrains.webwiz.models

import org.jetbrains.webwiz.models.SourceSetType.MAIN
import org.jetbrains.webwiz.models.Target.*
import org.jetbrains.webwiz.models.KmpTarget.*

internal const val AndroidGradlePluginVersion = "7.4.2"
internal const val GradleVersion = "8.0.2"
Expand All @@ -10,7 +10,7 @@ internal const val SqlDelightVersion = "1.5.5"
internal const val RealmVersion = "1.5.2"

enum class KmpLibrary(
val targets: Set<Target>,
val targets: Set<KmpTarget>,
val userName: String,
val versionCatalogName: String,
val dep: String,
Expand Down Expand Up @@ -129,8 +129,8 @@ enum class KmpLibrary(
MAIN
);

fun canBeApplied(targets: Set<Target>): Boolean {
val isCommon = targets.size == Target.values().size
fun canBeApplied(targets: Set<KmpTarget>): Boolean {
val isCommon = targets.size == KmpTarget.values().size
if (isCommon) return true

val isSingleTargetLib = this.targets.size == 1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ data class ProjectInfo(
val moduleName: String,
val packageName: String,
val kotlinVersion: KotlinVersion,
val targets: Set<Target>,
val targets: Set<KmpTarget>,
val dependencies: Set<KmpLibrary>,
val gradlePlugins: Set<GradlePlugin>,
val enableTests: Boolean
Expand Down
20 changes: 10 additions & 10 deletions src/commonMain/kotlin/org/jetbrains/webwiz/models/Targets.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package org.jetbrains.webwiz.models

enum class Target(
enum class KmpTarget(
val targetName: String,
val userName: String,
val mainSourceSetName: String,
Expand All @@ -18,17 +18,17 @@ enum class Target(
}

private val NativeTargets = setOf(
Target.LINUX,
Target.MACOS,
Target.IOS,
Target.TV_OS,
Target.WATCH_OS,
Target.WINDOWS
KmpTarget.LINUX,
KmpTarget.MACOS,
KmpTarget.IOS,
KmpTarget.TV_OS,
KmpTarget.WATCH_OS,
KmpTarget.WINDOWS
)

fun Target.isJvm() = this in setOf(Target.JVM, Target.ANDROID)
fun Target.isNative() = this in NativeTargets
fun KmpTarget.isJvm() = this in setOf(KmpTarget.JVM, KmpTarget.ANDROID)
fun KmpTarget.isNative() = this in NativeTargets

fun Set<Target>.isNativeTargets() = this.all { it in NativeTargets }
fun Set<KmpTarget>.isNativeTargets() = this.all { it in NativeTargets }
fun ProjectInfo.isCommonNativeTargetPresent() = targets.filter { it in NativeTargets }.size > 1
fun ProjectInfo.isSingleNativeTargetPresent() = targets.filter { it in NativeTargets }.size == 1
8 changes: 4 additions & 4 deletions src/jsMain/kotlin/org/jetbrains/webwiz/content/Chips.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,18 @@ import org.jetbrains.compose.web.dom.*
import org.jetbrains.webwiz.models.GradlePlugin
import org.jetbrains.webwiz.models.KmpLibrary
import org.jetbrains.webwiz.models.ProjectInfo
import org.jetbrains.webwiz.models.Target
import org.jetbrains.webwiz.models.KmpTarget
import org.jetbrains.webwiz.style.WtOffsets

@Composable
fun TargetChips(projectInfo: ProjectInfo, update: (ProjectInfo) -> Unit) {
Div({ classes(WtOffsets.targetsCheckboxesListStyle) }) {
Target.values().forEach { t ->
KmpTarget.values().forEach { t ->
val id = "target_checkbox_${t.name}"
Span({ classes(WtOffsets.targetsCheckboxesStyle) }) {
CheckboxInput(projectInfo.targets.contains(t)) {
onChange { event ->
val new: Set<Target> = when {
val new: Set<KmpTarget> = when {
event.value -> projectInfo.targets.plus(t)
projectInfo.targets.size > 1 -> projectInfo.targets.minus(t)
else -> projectInfo.targets
Expand All @@ -32,7 +32,7 @@ fun TargetChips(projectInfo: ProjectInfo, update: (ProjectInfo) -> Unit) {
}
}

private fun ProjectInfo.applyNewTargets(targets: Set<Target>): ProjectInfo = copy(
private fun ProjectInfo.applyNewTargets(targets: Set<KmpTarget>): ProjectInfo = copy(
targets = targets,
dependencies = dependencies.filter { it.canBeApplied(targets) }.toSet(),
gradlePlugins = gradlePlugins.filter { it.canBeApplied(targets) }.toSet()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import org.jetbrains.webwiz.generator.generate
import org.jetbrains.webwiz.models.KmpLibrary
import org.jetbrains.webwiz.models.KotlinVersion
import org.jetbrains.webwiz.models.ProjectInfo
import org.jetbrains.webwiz.models.Target
import org.jetbrains.webwiz.models.KmpTarget
import org.jetbrains.webwiz.setHighlightedCode
import org.jetbrains.webwiz.style.WtContainer
import org.jetbrains.webwiz.style.WtOffsets
Expand All @@ -26,7 +26,7 @@ private val defaultProject = ProjectInfo(
moduleName = "shared",
packageName = "org.sample.application",
kotlinVersion = KotlinVersion.Stable,
targets = setOf(Target.ANDROID, Target.IOS),
targets = setOf(KmpTarget.ANDROID, KmpTarget.IOS),
enableTests = true,
dependencies = setOf(
KmpLibrary.COROUTINES,
Expand Down

0 comments on commit 319d884

Please sign in to comment.