MPP wizard: add skeleton for JVM/JS project
Part of KT-26786
This commit is contained in:
+11
@@ -11,6 +11,7 @@ import com.intellij.openapi.externalSystem.service.project.wizard.ExternalModule
|
||||
import com.intellij.openapi.module.Module
|
||||
import com.intellij.openapi.roots.ui.configuration.ModulesProvider
|
||||
import com.intellij.openapi.vfs.VfsUtil
|
||||
import com.intellij.openapi.vfs.VirtualFile
|
||||
import org.jetbrains.kotlin.idea.KotlinIcons
|
||||
import org.jetbrains.kotlin.idea.util.rootManager
|
||||
import org.jetbrains.plugins.gradle.frameworkSupport.BuildScriptDataBuilder
|
||||
@@ -39,6 +40,7 @@ abstract class KotlinGradleAbstractMultiplatformModuleBuilder : GradleModuleBuil
|
||||
val builder = BuildScriptDataBuilder(buildGradle)
|
||||
GradleKotlinMPPFrameworkSupportProvider().addSupport(builder, module, sdk = null, specifyPluginVersionIfNeeded = true)
|
||||
VfsUtil.saveText(buildGradle, builder.buildConfigurationPart() + builder.buildMainPart() + buildMultiPlatformPart())
|
||||
createProjectSkeleton(module, rootDir)
|
||||
} finally {
|
||||
flushSettingsGradleCopy(module)
|
||||
}
|
||||
@@ -46,6 +48,15 @@ abstract class KotlinGradleAbstractMultiplatformModuleBuilder : GradleModuleBuil
|
||||
|
||||
protected abstract fun buildMultiPlatformPart(): String
|
||||
|
||||
protected fun VirtualFile.createKotlinSampleFileWriter(sourceRootName: String, fileName: String = "Sample.kt") =
|
||||
createChildDirectory(this, sourceRootName)
|
||||
.createChildDirectory(this, "kotlin")
|
||||
.createChildDirectory(this, "sample")
|
||||
.createChildData(this, fileName)
|
||||
.getOutputStream(this).bufferedWriter()
|
||||
|
||||
protected open fun createProjectSkeleton(module: Module, rootDir: VirtualFile) {}
|
||||
|
||||
companion object {
|
||||
const val productionSuffix = "Main"
|
||||
|
||||
|
||||
+115
@@ -5,6 +5,10 @@
|
||||
|
||||
package org.jetbrains.kotlin.idea.configuration
|
||||
|
||||
import com.intellij.openapi.module.Module
|
||||
import com.intellij.openapi.vfs.VirtualFile
|
||||
import java.io.BufferedWriter
|
||||
|
||||
class KotlinGradleWebMultiplatformModuleBuilder : KotlinGradleAbstractMultiplatformModuleBuilder() {
|
||||
|
||||
private val commonName: String = "common"
|
||||
@@ -25,6 +29,117 @@ class KotlinGradleWebMultiplatformModuleBuilder : KotlinGradleAbstractMultiplatf
|
||||
override fun getDescription() =
|
||||
"Multiplatform Gradle projects allow reusing the same Kotlin code between JS Client and JVM Server."
|
||||
|
||||
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")
|
||||
|
||||
try {
|
||||
commonMain.write(
|
||||
"""
|
||||
package sample
|
||||
|
||||
expect class Sample() {
|
||||
fun checkMe(): Int
|
||||
}
|
||||
|
||||
expect object Platform {
|
||||
val name: String
|
||||
}
|
||||
|
||||
fun hello(): String = "Hello from ${"$"}{Platform.name}"
|
||||
|
||||
fun main(args: Array<String>) {
|
||||
println(hello())
|
||||
}
|
||||
""".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()
|
||||
)
|
||||
|
||||
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()
|
||||
)
|
||||
} finally {
|
||||
listOf(commonMain, commonTest, jvmMain, jvmTest, jsMain, jsTest).forEach(BufferedWriter::close)
|
||||
}
|
||||
}
|
||||
|
||||
override fun buildMultiPlatformPart(): String {
|
||||
return """
|
||||
kotlin {
|
||||
|
||||
Reference in New Issue
Block a user