Introduce MPP wizard for mobile (Android / iOS) libraries
#KT-27200 Fixed
This commit is contained in:
+185
@@ -0,0 +1,185 @@
|
||||
/*
|
||||
* Copyright 2010-2018 JetBrains s.r.o. 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.idea.configuration
|
||||
|
||||
import com.intellij.openapi.module.Module
|
||||
import com.intellij.openapi.vfs.VirtualFile
|
||||
import java.io.BufferedWriter
|
||||
|
||||
class KotlinGradleMobileSharedMultiplatformModuleBuilder : KotlinGradleAbstractMultiplatformModuleBuilder() {
|
||||
|
||||
private val commonName: String = "common"
|
||||
private var jvmTargetName: String = "jvm"
|
||||
private var nativeTargetName: String = "ios"
|
||||
|
||||
private val commonSourceName get() = "$commonName$productionSuffix"
|
||||
private val commonTestName get() = "$commonName$testSuffix"
|
||||
private val jvmSourceName get() = "$jvmTargetName$productionSuffix"
|
||||
private val jvmTestName get() = "$jvmTargetName$testSuffix"
|
||||
private val nativeSourceName get() = "$nativeTargetName$productionSuffix"
|
||||
private val nativeTestName get() = "$nativeTargetName$testSuffix"
|
||||
|
||||
override fun getBuilderId() = "kotlin.gradle.multiplatform.mobileshared"
|
||||
|
||||
override fun getPresentableName() = "Kotlin (Mobile Shared Library)"
|
||||
|
||||
override fun getDescription() =
|
||||
"Multiplatform Gradle projects allow sharing the same Kotlin code between two mobile platforms (JVM/Android, Native)."
|
||||
|
||||
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 nativeMain = src.createKotlinSampleFileWriter(nativeSourceName)
|
||||
val nativeTest = src.createKotlinSampleFileWriter(nativeTestName, fileName = "SampleTestsNative.kt")
|
||||
|
||||
try {
|
||||
commonMain.write(
|
||||
"""
|
||||
package sample
|
||||
|
||||
expect class Sample() {
|
||||
fun checkMe(): Int
|
||||
}
|
||||
|
||||
expect object Platform {
|
||||
fun 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 fun name(): String = "Android"
|
||||
}
|
||||
""".trimIndent()
|
||||
)
|
||||
|
||||
nativeMain.write(
|
||||
"""
|
||||
package sample
|
||||
|
||||
actual class Sample {
|
||||
actual fun checkMe() = 7
|
||||
}
|
||||
|
||||
actual object Platform {
|
||||
actual fun 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()
|
||||
)
|
||||
|
||||
nativeTest.write(
|
||||
"""
|
||||
package sample
|
||||
|
||||
import kotlin.test.Test
|
||||
import kotlin.test.assertTrue
|
||||
|
||||
class SampleTestsNative {
|
||||
@Test
|
||||
fun testHello() {
|
||||
assertTrue("Native" in hello())
|
||||
}
|
||||
}
|
||||
""".trimIndent()
|
||||
)
|
||||
} finally {
|
||||
listOf(commonMain, commonTest, jvmMain, jvmTest, nativeMain, nativeTest).forEach(BufferedWriter::close)
|
||||
}
|
||||
}
|
||||
|
||||
override fun buildMultiPlatformPart(): String {
|
||||
return """
|
||||
kotlin {
|
||||
targets {
|
||||
fromPreset(presets.jvm, '$jvmTargetName')
|
||||
// This preset is for iPhone emulator
|
||||
// Switch here to presets.iosArm64 to build library for iPhone device
|
||||
fromPreset(presets.iosX64, '$nativeTargetName') {
|
||||
compilations.main.outputKinds('FRAMEWORK')
|
||||
}
|
||||
}
|
||||
sourceSets {
|
||||
$commonSourceName {
|
||||
dependencies {
|
||||
implementation 'org.jetbrains.kotlin:kotlin-stdlib-common'
|
||||
}
|
||||
}
|
||||
$commonTestName {
|
||||
dependencies {
|
||||
implementation 'org.jetbrains.kotlin:kotlin-test-common'
|
||||
implementation 'org.jetbrains.kotlin:kotlin-test-annotations-common'
|
||||
}
|
||||
}
|
||||
$jvmSourceName {
|
||||
dependencies {
|
||||
implementation 'org.jetbrains.kotlin:kotlin-stdlib'
|
||||
}
|
||||
}
|
||||
$jvmTestName {
|
||||
dependencies {
|
||||
implementation 'org.jetbrains.kotlin:kotlin-test'
|
||||
implementation 'org.jetbrains.kotlin:kotlin-test-junit'
|
||||
}
|
||||
}
|
||||
$nativeSourceName {
|
||||
}
|
||||
$nativeTestName {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
configurations {
|
||||
compileClasspath
|
||||
}
|
||||
""".trimIndent()
|
||||
}
|
||||
}
|
||||
@@ -82,5 +82,6 @@
|
||||
<moduleBuilder implementation="org.jetbrains.kotlin.idea.configuration.KotlinGradleWebMultiplatformModuleBuilder"/>
|
||||
<moduleBuilder implementation="org.jetbrains.kotlin.idea.configuration.KotlinGradleSharedMultiplatformModuleBuilder"/>
|
||||
<moduleBuilder implementation="org.jetbrains.kotlin.idea.configuration.KotlinGradleMobileMultiplatformModuleBuilder"/>
|
||||
<moduleBuilder implementation="org.jetbrains.kotlin.idea.configuration.KotlinGradleMobileSharedMultiplatformModuleBuilder"/>
|
||||
</extensions>
|
||||
</idea-plugin>
|
||||
|
||||
@@ -82,5 +82,6 @@
|
||||
<moduleBuilder implementation="org.jetbrains.kotlin.idea.configuration.KotlinGradleWebMultiplatformModuleBuilder"/>
|
||||
<moduleBuilder implementation="org.jetbrains.kotlin.idea.configuration.KotlinGradleSharedMultiplatformModuleBuilder"/>
|
||||
<moduleBuilder implementation="org.jetbrains.kotlin.idea.configuration.KotlinGradleMobileMultiplatformModuleBuilder"/>
|
||||
<moduleBuilder implementation="org.jetbrains.kotlin.idea.configuration.KotlinGradleMobileSharedMultiplatformModuleBuilder"/>
|
||||
</extensions>
|
||||
</idea-plugin>
|
||||
|
||||
@@ -90,6 +90,7 @@
|
||||
<moduleBuilder implementation="org.jetbrains.kotlin.idea.configuration.KotlinGradleWebMultiplatformModuleBuilder"/>
|
||||
<moduleBuilder implementation="org.jetbrains.kotlin.idea.configuration.KotlinGradleSharedMultiplatformModuleBuilder"/>
|
||||
<moduleBuilder implementation="org.jetbrains.kotlin.idea.configuration.KotlinGradleMobileMultiplatformModuleBuilder"/>
|
||||
<moduleBuilder implementation="org.jetbrains.kotlin.idea.configuration.KotlinGradleMobileSharedMultiplatformModuleBuilder"/>
|
||||
|
||||
<buildSystemTypeDetector implementation="org.jetbrains.kotlin.idea.configuration.GradleDetector"/>
|
||||
</extensions>
|
||||
|
||||
Reference in New Issue
Block a user