MPP wizard: add skeleton for shared JVM/JS/iOS project

Part of KT-26786
This commit is contained in:
Mikhail Glukhikh
2018-09-20 13:12:05 +03:00
parent 47baaf10c6
commit 83faba6424
@@ -5,6 +5,10 @@
package org.jetbrains.kotlin.idea.configuration package org.jetbrains.kotlin.idea.configuration
import com.intellij.openapi.module.Module
import com.intellij.openapi.vfs.VirtualFile
import java.io.BufferedWriter
class KotlinGradleSharedMultiplatformModuleBuilder : KotlinGradleAbstractMultiplatformModuleBuilder() { class KotlinGradleSharedMultiplatformModuleBuilder : KotlinGradleAbstractMultiplatformModuleBuilder() {
private val commonName: String = "common" private val commonName: String = "common"
@@ -28,6 +32,145 @@ class KotlinGradleSharedMultiplatformModuleBuilder : KotlinGradleAbstractMultipl
override fun getDescription() = override fun getDescription() =
"Multiplatform Gradle projects allow sharing the same Kotlin code between all three main platforms (JVM, JS, iOS)." "Multiplatform Gradle projects allow sharing the same Kotlin code between all three main platforms (JVM, JS, iOS)."
override fun createProjectSkeleton(module: Module, rootDir: VirtualFile) {
val src = rootDir.createChildDirectory(this, "src")
val commonMain = src.createKotlinSampleFileWriter(commonSourceName)
val commonTest = src.createKotlinSampleFileWriter(commonTestName, fileName = "SampleTests.kt")
val jvmMain = src.createKotlinSampleFileWriter(jvmSourceName)
val jvmTest = src.createKotlinSampleFileWriter(jvmTestName, fileName = "SampleTestsJVM.kt")
val jsMain = src.createKotlinSampleFileWriter(jsSourceName)
val jsTest = src.createKotlinSampleFileWriter(jsTestName, fileName = "SampleTestsJS.kt")
val nativeMain = src.createKotlinSampleFileWriter(nativeSourceName)
val nativeTest = src.createKotlinSampleFileWriter(nativeTestName, fileName = "SampleTestsIOS.kt")
try {
commonMain.write(
"""
package sample
expect class Sample() {
fun checkMe(): Int
}
expect object Platform {
val name: String
}
fun hello(): String = "Hello from ${"$"}{Platform.name}"
""".trimIndent()
)
jvmMain.write(
"""
package sample
actual class Sample {
actual fun checkMe() = 42
}
actual object Platform {
actual val name: String = "JVM"
}
""".trimIndent()
)
jsMain.write(
"""
package sample
actual class Sample {
actual fun checkMe() = 12
}
actual object Platform {
actual val name: String = "JS"
}
""".trimIndent()
)
nativeMain.write(
"""
package sample
actual class Sample {
actual fun checkMe() = 7
}
actual object Platform {
actual val name: String = "iOS"
}
""".trimIndent()
)
commonTest.write(
"""
package sample
import kotlin.test.Test
import kotlin.test.assertTrue
class SampleTests {
@Test
fun testMe() {
assertTrue(Sample().checkMe() > 0)
}
}
""".trimIndent()
)
jvmTest.write(
"""
package sample
import kotlin.test.Test
import kotlin.test.assertTrue
class SampleTestsJVM {
@Test
fun testHello() {
assertTrue("JVM" in hello())
}
}
""".trimIndent()
)
jsTest.write(
"""
package sample
import kotlin.test.Test
import kotlin.test.assertTrue
class SampleTestsJS {
@Test
fun testHello() {
assertTrue("JS" in hello())
}
}
""".trimIndent()
)
nativeTest.write(
"""
package sample
import kotlin.test.Test
import kotlin.test.assertTrue
class SampleTestsIOS {
@Test
fun testHello() {
assertTrue("iOS" in hello())
}
}
""".trimIndent()
)
} finally {
listOf(commonMain, commonTest, jvmMain, jvmTest, jsMain, jsTest, nativeMain, nativeTest).forEach(BufferedWriter::close)
}
}
override fun buildMultiPlatformPart(): String { override fun buildMultiPlatformPart(): String {
return """ return """
kotlin { kotlin {