├── .gitignore
├── gradle
└── wrapper
│ ├── gradle-wrapper.jar
│ └── gradle-wrapper.properties
├── js
├── src
│ ├── main
│ │ └── kotlin
│ │ │ └── mp
│ │ │ ├── MultiPlatformCloseableInterfaceJsImpl.kt
│ │ │ ├── MultiPlatformFunctionJsImpl.kt
│ │ │ ├── SubClassHeaderDelegatedTo.kt
│ │ │ ├── InternalClassHeaderDelegatedTo.kt
│ │ │ └── ClassWithMultiPlatformFunctionality.kt
│ └── test
│ │ └── kotlin
│ │ └── mp
│ │ └── ClassWithMultiPlatformFunctionalityTest.kt
└── build.gradle
├── jvm
├── src
│ ├── main
│ │ └── kotlin
│ │ │ └── mp
│ │ │ ├── MultiPlatformFunctionJvmImpl.kt
│ │ │ ├── MultiPlatformCloseableInterfaceJvmImpl.kt
│ │ │ ├── SubClassHeaderDelegatedTo.kt
│ │ │ ├── InternalClassHeaderDelegatedTo.kt
│ │ │ └── ClassWithMultiPlatformFunctionality.kt
│ └── test
│ │ └── kotlin
│ │ └── mp
│ │ └── ClassWithMultiPlatformFunctionalityTest.kt
└── build.gradle
├── settings.gradle
├── common
├── src
│ ├── main
│ │ └── kotlin
│ │ │ └── mp
│ │ │ ├── CommonInterface.kt
│ │ │ ├── CommonFunction.kt
│ │ │ ├── MultiPlatformFunctionHeader.kt
│ │ │ ├── MultiPlatformCloseableInterfaceHeader.kt
│ │ │ ├── CommonObject.kt
│ │ │ ├── SubClassHeaderDelegatedTo.kt
│ │ │ ├── CommonClass.kt
│ │ │ ├── InternalClassHeaderDelegatedTo.kt
│ │ │ ├── ClassWithMultiPlatformFunctionality.kt
│ │ │ ├── CommonExtensionFunction.kt
│ │ │ ├── CommonClassDelegatingToSubClassHeader.kt
│ │ │ ├── CommonClassDelegatingToInternalClassHeader.kt
│ │ │ └── CommonGenericExtensionFunction.kt
│ └── test
│ │ └── kotlin
│ │ └── mp
│ │ └── CommonClassTest.kt
└── build.gradle
├── jvm-app
├── build.gradle
└── src
│ └── main
│ └── kotlin
│ └── jvm
│ └── JvmApp.kt
├── js-app
├── JsApp-minified.html
├── JsApp.html
├── build.gradle
└── src
│ └── main
│ └── kotlin
│ └── js
│ └── JsApp.kt
├── LICENSE
├── gradlew.bat
├── Readme.md
├── CODE_OF_CONDUCT.md
└── gradlew
/.gitignore:
--------------------------------------------------------------------------------
1 | .gradle/
2 | .idea/
3 | gradle.properties
4 | js/node_modules
5 | js/npm-debug.log
6 | js/package-lock.json
7 |
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/jstuyts/kotlin-multiplatform-recipes/HEAD/gradle/wrapper/gradle-wrapper.jar
--------------------------------------------------------------------------------
/js/src/main/kotlin/mp/MultiPlatformCloseableInterfaceJsImpl.kt:
--------------------------------------------------------------------------------
1 | package mp
2 |
3 | actual interface MpCloseable {
4 | actual fun close()
5 | }
6 |
--------------------------------------------------------------------------------
/jvm/src/main/kotlin/mp/MultiPlatformFunctionJvmImpl.kt:
--------------------------------------------------------------------------------
1 | package mp
2 |
3 | actual fun multiPlatformFunction() = "This is the Java-specific implementation"
4 |
--------------------------------------------------------------------------------
/js/src/main/kotlin/mp/MultiPlatformFunctionJsImpl.kt:
--------------------------------------------------------------------------------
1 | package mp
2 |
3 | actual fun multiPlatformFunction() = "This is the JavaScript-specific implementation"
4 |
--------------------------------------------------------------------------------
/jvm/src/main/kotlin/mp/MultiPlatformCloseableInterfaceJvmImpl.kt:
--------------------------------------------------------------------------------
1 | package mp
2 |
3 | import java.io.Closeable
4 |
5 | actual typealias MpCloseable = Closeable
6 |
--------------------------------------------------------------------------------
/settings.gradle:
--------------------------------------------------------------------------------
1 | rootProject.name = 'kotlin-multiplatform-recipes'
2 |
3 | include ':common'
4 | include ':jvm'
5 | include ':jvm-app'
6 | include ':js'
7 | include ':js-app'
8 |
--------------------------------------------------------------------------------
/common/src/main/kotlin/mp/CommonInterface.kt:
--------------------------------------------------------------------------------
1 | package mp
2 |
3 | /**
4 | * An interface that is the same on all platforms.
5 | */
6 | interface CommonInterface {
7 | fun doIt()
8 | }
9 |
--------------------------------------------------------------------------------
/common/src/main/kotlin/mp/CommonFunction.kt:
--------------------------------------------------------------------------------
1 | package mp
2 |
3 | /**
4 | * A function that is the same on all platforms.
5 | */
6 | fun commonFunction() = "This function implementation is the same on all platforms"
7 |
--------------------------------------------------------------------------------
/common/src/main/kotlin/mp/MultiPlatformFunctionHeader.kt:
--------------------------------------------------------------------------------
1 | package mp
2 |
3 | /**
4 | * A function that is multi-platform, i.e. the implementation differs per platform.
5 | */
6 | expect fun multiPlatformFunction(): String
7 |
--------------------------------------------------------------------------------
/common/src/main/kotlin/mp/MultiPlatformCloseableInterfaceHeader.kt:
--------------------------------------------------------------------------------
1 | package mp
2 |
3 | /**
4 | * An interface that is multi-platform, i.e. the implementation differs per platform.
5 | */
6 | expect interface MpCloseable {
7 | fun close()
8 | }
9 |
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.properties:
--------------------------------------------------------------------------------
1 | distributionBase=GRADLE_USER_HOME
2 | distributionPath=wrapper/dists
3 | distributionUrl=https\://services.gradle.org/distributions/gradle-4.10.2-all.zip
4 | zipStoreBase=GRADLE_USER_HOME
5 | zipStorePath=wrapper/dists
6 |
--------------------------------------------------------------------------------
/jvm/src/main/kotlin/mp/SubClassHeaderDelegatedTo.kt:
--------------------------------------------------------------------------------
1 | package mp
2 |
3 | actual class SubClassHeaderDelegatedTo : CommonClassDelegatingToSubClassHeader() {
4 | actual override fun doIt() {
5 | println("Delegated to a Java sub class")
6 | }
7 | }
8 |
--------------------------------------------------------------------------------
/js/src/main/kotlin/mp/SubClassHeaderDelegatedTo.kt:
--------------------------------------------------------------------------------
1 | package mp
2 |
3 | actual class SubClassHeaderDelegatedTo : CommonClassDelegatingToSubClassHeader() {
4 | actual override fun doIt() {
5 | println("Delegated to a JavaScript sub class")
6 | }
7 | }
8 |
--------------------------------------------------------------------------------
/common/src/main/kotlin/mp/CommonObject.kt:
--------------------------------------------------------------------------------
1 | package mp
2 |
3 | /**
4 | * An object that is the same on all platforms.
5 | */
6 | object CommonObject {
7 | fun execute(task: CommonInterface) {
8 | task.doIt()
9 | }
10 |
11 | override fun toString() = "The common object"
12 | }
13 |
--------------------------------------------------------------------------------
/common/src/main/kotlin/mp/SubClassHeaderDelegatedTo.kt:
--------------------------------------------------------------------------------
1 | package mp
2 |
3 | /**
4 | * This class provides the multi-platform implementation for [CommonClassDelegatingToSubClassHeader].
5 | */
6 | expect class SubClassHeaderDelegatedTo : CommonClassDelegatingToSubClassHeader {
7 | override fun doIt()
8 | }
9 |
--------------------------------------------------------------------------------
/common/src/main/kotlin/mp/CommonClass.kt:
--------------------------------------------------------------------------------
1 | package mp
2 |
3 | /**
4 | * A class that is the same on all platforms.
5 | */
6 | class CommonClass {
7 | fun execute(task: CommonInterface) {
8 | task.doIt()
9 | }
10 |
11 | override fun toString() = "An instance of the common class"
12 | }
13 |
--------------------------------------------------------------------------------
/jvm/src/main/kotlin/mp/InternalClassHeaderDelegatedTo.kt:
--------------------------------------------------------------------------------
1 | package mp
2 |
3 | internal actual class InternalClassHeaderDelegatedTo actual constructor(private val owner: CommonClassDelegatingToInternalClassHeader) {
4 | actual fun doIt() {
5 | println("Internal actual class in Java for: $owner")
6 | }
7 | }
8 |
--------------------------------------------------------------------------------
/js/src/main/kotlin/mp/InternalClassHeaderDelegatedTo.kt:
--------------------------------------------------------------------------------
1 | package mp
2 |
3 | internal actual class InternalClassHeaderDelegatedTo actual constructor(private val owner: CommonClassDelegatingToInternalClassHeader) {
4 | actual fun doIt() {
5 | println("Internal actual class in JavaScript for: $owner")
6 | }
7 | }
8 |
--------------------------------------------------------------------------------
/jvm-app/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'kotlin'
2 |
3 | dependencies {
4 | compile project(':jvm')
5 | compile libraries.kotlin_stdlib
6 | }
7 |
8 | task run(dependsOn: classes, type: JavaExec) {
9 | main = 'jvm.JvmAppKt'
10 | classpath = sourceSets.main.runtimeClasspath
11 | ignoreExitValue(true)
12 | }
13 |
--------------------------------------------------------------------------------
/common/src/main/kotlin/mp/InternalClassHeaderDelegatedTo.kt:
--------------------------------------------------------------------------------
1 | package mp
2 |
3 | /**
4 | * This class provides the multi-platform implementation for [CommonClassDelegatingToInternalClassHeader].
5 | */
6 | internal expect class InternalClassHeaderDelegatedTo(owner: CommonClassDelegatingToInternalClassHeader) {
7 | fun doIt()
8 | }
9 |
--------------------------------------------------------------------------------
/common/src/main/kotlin/mp/ClassWithMultiPlatformFunctionality.kt:
--------------------------------------------------------------------------------
1 | package mp
2 |
3 | /**
4 | * This is the expected class showing that the multi-platform implementation can add additional members besides the
5 | * member declared here.
6 | */
7 | expect class ClassWithMultiPlatformFunctionality {
8 | fun commonFunctionality()
9 | }
10 |
--------------------------------------------------------------------------------
/jvm/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'kotlin-platform-jvm'
2 |
3 | archivesBaseName = 'kotlin-multiplatform-recipes-jvm'
4 |
5 | dependencies {
6 | expectedBy project(':common')
7 | compile libraries.kotlin_stdlib
8 | testCompile libraries.kotlin_test_junit
9 | }
10 |
11 | task sourcesJar(type: Jar) {
12 | classifier = 'sources'
13 | from sourceSets.main.kotlin
14 | }
15 |
16 | artifacts {
17 | archives sourcesJar
18 | }
19 |
--------------------------------------------------------------------------------
/common/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'kotlin-platform-common'
2 |
3 | archivesBaseName = 'kotlin-multiplatform-recipes-common'
4 |
5 | dependencies {
6 | compile libraries.kotlin_stdlib_common
7 | testCompile libraries.kotlin_test_annotations_common
8 | testCompile libraries.kotlin_test_common
9 | }
10 |
11 | task sourcesJar(type: Jar) {
12 | classifier = 'sources'
13 | from sourceSets.main.kotlin
14 | }
15 |
16 | artifacts {
17 | archives sourcesJar
18 | }
19 |
--------------------------------------------------------------------------------
/jvm/src/main/kotlin/mp/ClassWithMultiPlatformFunctionality.kt:
--------------------------------------------------------------------------------
1 | package mp
2 |
3 | actual class ClassWithMultiPlatformFunctionality {
4 | actual fun commonFunctionality() {
5 | println("This is multi-platform functionality implemented in Java")
6 | }
7 |
8 | fun javaFunctionality() {
9 | println("This is Java-specific functionality")
10 | }
11 |
12 | override fun toString(): String {
13 | return "A Java class implementing common and providing Java-specific functionality"
14 | }
15 | }
16 |
--------------------------------------------------------------------------------
/js-app/JsApp-minified.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
7 |
8 | Kotlin multi-platform modules
9 |
10 |
11 |
12 | Open the developer console to see the output.
13 |
14 |
15 |
16 |
17 |
--------------------------------------------------------------------------------
/common/src/main/kotlin/mp/CommonExtensionFunction.kt:
--------------------------------------------------------------------------------
1 | package mp
2 |
3 | /**
4 | * Extension function for a multi-platform interface.
5 | */
6 | fun MpCloseable.printThis() = println(this)
7 |
8 | /**
9 | * Extension function for a common class.
10 | */
11 | fun CommonClass.printThis() = println(this)
12 |
13 | /**
14 | * Extension function for a common interface.
15 | */
16 | fun CommonInterface.printThis() = println(this)
17 |
18 | /**
19 | * Extension function for a common object.
20 | */
21 | fun CommonObject.printThis() = println(this)
22 |
--------------------------------------------------------------------------------
/js/src/main/kotlin/mp/ClassWithMultiPlatformFunctionality.kt:
--------------------------------------------------------------------------------
1 | package mp
2 |
3 | actual class ClassWithMultiPlatformFunctionality {
4 | actual fun commonFunctionality() {
5 | println("This is multi-platform functionality implemented in JavaScript")
6 | }
7 |
8 | fun javascriptFunctionality() {
9 | println("This is JavaScript-specific functionality")
10 | }
11 |
12 | override fun toString(): String {
13 | return "A JavaScript class implementing common and providing JavaScript-specific functionality"
14 | }
15 | }
16 |
--------------------------------------------------------------------------------
/jvm/src/test/kotlin/mp/ClassWithMultiPlatformFunctionalityTest.kt:
--------------------------------------------------------------------------------
1 | package mp
2 |
3 | import kotlin.test.assertEquals
4 | import kotlin.test.Test
5 |
6 | /**
7 | * Shows that tests for Java-specific functionality will run at the same time as tests for common functionality.
8 | */
9 | class ClassWithMultiPlatformFunctionalityTest {
10 | @Test
11 | fun stringRepresentationIsCorrect() {
12 | val instance = ClassWithMultiPlatformFunctionality()
13 |
14 | val stringRepresentation = instance.toString()
15 |
16 | assertEquals("A Java class implementing common and providing Java-specific functionality", stringRepresentation)
17 | }
18 | }
19 |
--------------------------------------------------------------------------------
/js-app/JsApp.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
7 |
8 | Kotlin multi-platform modules
9 |
10 |
11 |
12 | Open the developer console to see the output.
13 |
14 |
15 |
16 |
17 |
18 |
19 |
--------------------------------------------------------------------------------
/common/src/main/kotlin/mp/CommonClassDelegatingToSubClassHeader.kt:
--------------------------------------------------------------------------------
1 | package mp
2 |
3 | /**
4 | * Shows the multi-platform implementation of a class by delegating to an expected sub class. Properties:
5 | *
6 | * * Adds artificial types to the class hierarchy.
7 | * * Allows for platform-specific members in the multi-platform sub class.
8 | *
9 | * See [CommonClassDelegatingToInternalClassHeader] for an alternative way to have a multi-platform implementation of a
10 | * class.
11 | */
12 | abstract class CommonClassDelegatingToSubClassHeader protected constructor() {
13 | fun execute() {
14 | doIt()
15 | }
16 |
17 | protected abstract fun doIt()
18 | }
19 |
--------------------------------------------------------------------------------
/js/src/test/kotlin/mp/ClassWithMultiPlatformFunctionalityTest.kt:
--------------------------------------------------------------------------------
1 | package mp
2 |
3 | import kotlin.test.Test
4 | import kotlin.test.assertEquals
5 |
6 | /**
7 | * Shows that tests for JavaScript-specific functionality will run at the same time as tests for common functionality.
8 | */
9 | class ClassWithMultiPlatformFunctionalityTest {
10 | @Test
11 | fun stringRepresentationIsCorrect() {
12 | val instance = ClassWithMultiPlatformFunctionality()
13 |
14 | val stringRepresentation = instance.toString()
15 |
16 | assertEquals("A JavaScript class implementing common and providing JavaScript-specific functionality", stringRepresentation)
17 | }
18 | }
19 |
--------------------------------------------------------------------------------
/common/src/main/kotlin/mp/CommonClassDelegatingToInternalClassHeader.kt:
--------------------------------------------------------------------------------
1 | package mp
2 |
3 | /**
4 | * Shows the multi-platform implementation of a class by delegating to an instance of an expected class. Properties:
5 | *
6 | * * Keeps the class hierarchy shallow.
7 | * * Needs a platform-specific sub class for platform-specific members.
8 | *
9 | * See [CommonClassDelegatingToSubClassHeader] for an alternative way to have a multi-platform implementation of a
10 | * class.
11 | */
12 | class CommonClassDelegatingToInternalClassHeader {
13 | private val multiPlatformCode = InternalClassHeaderDelegatedTo(this)
14 |
15 | fun execute() {
16 | multiPlatformCode.doIt()
17 | }
18 |
19 | override fun toString() = "An instance of the common class delegating to an internal expected class"
20 | }
21 |
--------------------------------------------------------------------------------
/common/src/test/kotlin/mp/CommonClassTest.kt:
--------------------------------------------------------------------------------
1 | package mp
2 |
3 | import kotlin.test.assertEquals
4 | import kotlin.test.assertTrue
5 | import kotlin.test.Test
6 |
7 | /**
8 | * Shows that tests for common functionality will be run in the multi-platform modules.
9 | */
10 | class CommonClassTest {
11 | @Test
12 | fun executeInvokesDoItOfGivenArgument() {
13 | val instance = CommonClass()
14 |
15 | var hasBeenInvoked = false
16 | instance.execute(object : CommonInterface {
17 | override fun doIt() {
18 | hasBeenInvoked = true
19 | }
20 | })
21 |
22 | assertTrue(hasBeenInvoked)
23 | }
24 |
25 | @Test
26 | fun stringRepresentationIsCorrect() {
27 | val instance = CommonClass()
28 |
29 | val stringRepresentation = instance.toString()
30 |
31 | assertEquals("An instance of the common class", stringRepresentation)
32 | }
33 | }
34 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | MIT License
2 |
3 | Copyright (c) 2017 Johan Stuyts
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
22 |
--------------------------------------------------------------------------------
/common/src/main/kotlin/mp/CommonGenericExtensionFunction.kt:
--------------------------------------------------------------------------------
1 | package mp
2 |
3 | /**
4 | * Generic extension function for a multi-platform interface.
5 | */
6 | // Copied from "kotlin.io.Closeable.kt"
7 | fun TCloseable.mpUse(block: (TCloseable) -> TResult): TResult {
8 | var closed = false
9 | try {
10 | return block(this)
11 | } catch (e: Exception) {
12 | closed = true
13 | try {
14 | this?.close()
15 | } catch (closeException: Exception) {
16 | }
17 | throw e
18 | } finally {
19 | if (!closed) {
20 | this?.close()
21 | }
22 | }
23 | }
24 |
25 | /**
26 | * Generic extension function for a common class.
27 | */
28 | fun TCommonClass.mpUse(block: (TCommonClass) -> TResult): TResult {
29 | return block(this)
30 | }
31 |
32 | /**
33 | * Generic extension function for a common interface.
34 | */
35 | fun TCommonInterface.mpUse(block: (TCommonInterface) -> TResult): TResult {
36 | return block(this)
37 | }
38 |
39 | /**
40 | * Generic extension function for a common object.
41 | */
42 | fun TCommonObject.mpUse(block: (TCommonObject) -> TResult): TResult {
43 | return block(this)
44 | }
45 |
--------------------------------------------------------------------------------
/js-app/build.gradle:
--------------------------------------------------------------------------------
1 | import com.google.javascript.jscomp.CompilerOptions
2 |
3 | apply plugin: 'kotlin2js'
4 | apply plugin: 'kotlin-dce-js'
5 | apply plugin: "com.eriwen.gradle.js"
6 |
7 | dependencies {
8 | compile project(':js')
9 | compile libraries.kotlin_stdlib_js
10 | }
11 |
12 | [compileKotlin2Js, compileTestKotlin2Js]*.configure {
13 | kotlinOptions.moduleKind = "umd"
14 | kotlinOptions.sourceMap = true
15 | kotlinOptions.sourceMapEmbedSources = "always"
16 | }
17 |
18 | combineJs {
19 | source = [
20 | // The order is important. That's why the documented way with a reference to a source set using wildcards
21 | // is not used.
22 | "${buildDir}/kotlin-js-min/main/kotlin.js",
23 | "${buildDir}/kotlin-js-min/main/kotlin-multiplatform-recipes-js.js",
24 | "${buildDir}/kotlin-js-min/main/js-app.js"
25 | ]
26 | dest = file("${temporaryDir}/${archivesBaseName}-combined.js")
27 | }
28 |
29 | minifyJs {
30 | source = combineJs
31 | dest = file("${temporaryDir}/${archivesBaseName}-minified.js")
32 | closure {
33 | compilerOptions = new CompilerOptions().with {
34 | setLanguage(CompilerOptions.LanguageMode.ECMASCRIPT5)
35 | return it
36 | }
37 | }
38 | }
39 |
40 | combineJs.dependsOn runDceKotlinJs
41 | build.dependsOn minifyJs
42 |
43 | task minifiedJar(type: Jar) {
44 | classifier = 'minified'
45 | from minifyJs
46 | }
47 |
48 | artifacts {
49 | archives minifiedJar
50 | }
51 |
--------------------------------------------------------------------------------
/js/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'kotlin-platform-js'
2 | apply plugin: 'com.moowork.node'
3 |
4 | archivesBaseName = 'kotlin-multiplatform-recipes-js'
5 |
6 | dependencies {
7 | expectedBy project(':common')
8 | compile libraries.kotlin_stdlib_js
9 | testCompile libraries.kotlin_test_js
10 | }
11 |
12 | [compileKotlin2Js, compileTestKotlin2Js]*.configure {
13 | kotlinOptions.moduleKind = "umd"
14 | kotlinOptions.sourceMap = true
15 | kotlinOptions.sourceMapEmbedSources = "always"
16 | }
17 |
18 | task populateNodeModules(type: Copy, dependsOn: compileKotlin2Js) {
19 | from compileKotlin2Js.destinationDir
20 |
21 | configurations.testCompile.each {
22 | from zipTree(it.absolutePath).matching { include '*.js' }
23 | }
24 |
25 | into "${buildDir}/node_modules"
26 | }
27 |
28 | node {
29 | version = nodeVersion
30 | download = true
31 | }
32 |
33 | task installQunit(type: NpmTask) {
34 | inputs.property('qunitVersion', qunitVersion)
35 | outputs.dir file('node_modules/qunit')
36 |
37 | args = ['install', "qunit@${qunitVersion}"]
38 | }
39 |
40 | task runQunit(type: NodeTask, dependsOn: [compileTestKotlin2Js, populateNodeModules, installQunit]) {
41 | script = file('node_modules/qunit/bin/qunit')
42 | args = [projectDir.toPath().relativize(file(compileTestKotlin2Js.outputFile).toPath())]
43 | }
44 |
45 | test.dependsOn runQunit
46 |
47 | task sourcesJar(type: Jar) {
48 | classifier = 'sources'
49 | from sourceSets.main.kotlin
50 | }
51 |
52 | artifacts {
53 | archives sourcesJar
54 | }
55 |
--------------------------------------------------------------------------------
/gradlew.bat:
--------------------------------------------------------------------------------
1 | @if "%DEBUG%" == "" @echo off
2 | @rem ##########################################################################
3 | @rem
4 | @rem Gradle startup script for Windows
5 | @rem
6 | @rem ##########################################################################
7 |
8 | @rem Set local scope for the variables with windows NT shell
9 | if "%OS%"=="Windows_NT" setlocal
10 |
11 | set DIRNAME=%~dp0
12 | if "%DIRNAME%" == "" set DIRNAME=.
13 | set APP_BASE_NAME=%~n0
14 | set APP_HOME=%DIRNAME%
15 |
16 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
17 | set DEFAULT_JVM_OPTS=
18 |
19 | @rem Find java.exe
20 | if defined JAVA_HOME goto findJavaFromJavaHome
21 |
22 | set JAVA_EXE=java.exe
23 | %JAVA_EXE% -version >NUL 2>&1
24 | if "%ERRORLEVEL%" == "0" goto init
25 |
26 | echo.
27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
28 | echo.
29 | echo Please set the JAVA_HOME variable in your environment to match the
30 | echo location of your Java installation.
31 |
32 | goto fail
33 |
34 | :findJavaFromJavaHome
35 | set JAVA_HOME=%JAVA_HOME:"=%
36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe
37 |
38 | if exist "%JAVA_EXE%" goto init
39 |
40 | echo.
41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
42 | echo.
43 | echo Please set the JAVA_HOME variable in your environment to match the
44 | echo location of your Java installation.
45 |
46 | goto fail
47 |
48 | :init
49 | @rem Get command-line arguments, handling Windows variants
50 |
51 | if not "%OS%" == "Windows_NT" goto win9xME_args
52 |
53 | :win9xME_args
54 | @rem Slurp the command line arguments.
55 | set CMD_LINE_ARGS=
56 | set _SKIP=2
57 |
58 | :win9xME_args_slurp
59 | if "x%~1" == "x" goto execute
60 |
61 | set CMD_LINE_ARGS=%*
62 |
63 | :execute
64 | @rem Setup the command line
65 |
66 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
67 |
68 | @rem Execute Gradle
69 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS%
70 |
71 | :end
72 | @rem End local scope for the variables with windows NT shell
73 | if "%ERRORLEVEL%"=="0" goto mainEnd
74 |
75 | :fail
76 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
77 | rem the _cmd.exe /c_ return code!
78 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1
79 | exit /b 1
80 |
81 | :mainEnd
82 | if "%OS%"=="Windows_NT" endlocal
83 |
84 | :omega
85 |
--------------------------------------------------------------------------------
/Readme.md:
--------------------------------------------------------------------------------
1 | # NO LONGER RELEVANT
2 |
3 | Multi-platform Kotlin has undergone signifcant changes after I stopped updating this. Please see the [current documentation](https://kotlinlang.org/docs/multiplatform.html).
4 |
5 | # Recipes for Multi-Platform Kotlin Modules
6 |
7 | This project is a testbed for multi-platform Kotlin modules. It shows how you could implement modules that have code that is common for all platforms, and that may need platform-specific code for (some of) its types and functions.
8 |
9 | Note that you have to change settings in IntelliJ IDEA to be able to build the project and run the examples from your IDE. See the item about IntelliJ IDEA under *current issues* below.
10 |
11 | Recipes for unit tests for common and platform-specific code are available. Execute the following command to run the Java tests. The test output can be found in `jvm/build/reports/tests/test/index.html`:
12 |
13 | ./gradlew :jvm:test
14 |
15 | Execute the following command to run the JavaScript tests. The test output can be found in the build output:
16 |
17 | ./gradlew :js:test
18 |
19 | ## Current Issues
20 |
21 | Not everything is working as it should as multi-platform support is still being developed. This is the list of known issues:
22 |
23 | * IntelliJ IDEA support using stable plug-ins is broken. The experience should be better with an [EAP](https://discuss.kotlinlang.org/c/eap) or [development](https://github.com/jetbrains/kotlin#-installing-the-latest-kotlin-plugin) build, and using the associated Gradle plug-in and standard library from the [Kotlin development repository](https://bintray.com/kotlin/kotlin-dev/kotlin):
24 | * You have to build and run the examples using the Gradle runner by enabling the following setting: Settings › Build, Execution, Deployment › Build Tools › Gradle › Runner › Delegate IDE build/run actions to gradle), or use the command line. To run the Java app:
25 |
26 | ./gradlew :jvm-app:run
27 |
28 | Or to run the JavaScript app:
29 |
30 | ./gradlew :js-app:runDceKotlinJs
31 |
32 | And open `js-app/JsApp.html`. The JavaScript has been mapped to the sources of the Kotlin standard library and module `js`, so you can set breakpoints in Kotlin code. Note that for some reason, the source maps are not available if you open the browser from within IntelliJ. Copy the file path of `jsApp.html` and paste that in the address bar of your browser.
33 |
34 | If you want to use the minified JavaScript run:
35 |
36 | ./gradlew :js-app:minifyJs
37 |
38 | And open `js-app/JsApp-minified.html`.
39 |
--------------------------------------------------------------------------------
/CODE_OF_CONDUCT.md:
--------------------------------------------------------------------------------
1 | # Contributor Covenant Code of Conduct
2 |
3 | ## Our Pledge
4 |
5 | In the interest of fostering an open and welcoming environment, we as contributors and maintainers pledge to making participation in our project and our community a harassment-free experience for everyone, regardless of age, body size, disability, ethnicity, gender identity and expression, level of experience, nationality, personal appearance, race, religion, or sexual identity and orientation.
6 |
7 | ## Our Standards
8 |
9 | Examples of behavior that contributes to creating a positive environment include:
10 |
11 | * Using welcoming and inclusive language
12 | * Being respectful of differing viewpoints and experiences
13 | * Gracefully accepting constructive criticism
14 | * Focusing on what is best for the community
15 | * Showing empathy towards other community members
16 |
17 | Examples of unacceptable behavior by participants include:
18 |
19 | * The use of sexualized language or imagery and unwelcome sexual attention or advances
20 | * Trolling, insulting/derogatory comments, and personal or political attacks
21 | * Public or private harassment
22 | * Publishing others' private information, such as a physical or electronic address, without explicit permission
23 | * Other conduct which could reasonably be considered inappropriate in a professional setting
24 |
25 | ## Our Responsibilities
26 |
27 | Project maintainers are responsible for clarifying the standards of acceptable behavior and are expected to take appropriate and fair corrective action in response to any instances of unacceptable behavior.
28 |
29 | Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, or to ban temporarily or permanently any contributor for other behaviors that they deem inappropriate, threatening, offensive, or harmful.
30 |
31 | ## Scope
32 |
33 | This Code of Conduct applies both within project spaces and in public spaces when an individual is representing the project or its community. Examples of representing a project or community include using an official project e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event. Representation of a project may be further defined and clarified by project maintainers.
34 |
35 | ## Enforcement
36 |
37 | Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by contacting the project team at j.stuyts@javathinker.com. The project team will review and investigate all complaints, and will respond in a way that it deems appropriate to the circumstances. The project team is obligated to maintain confidentiality with regard to the reporter of an incident. Further details of specific enforcement policies may be posted separately.
38 |
39 | Project maintainers who do not follow or enforce the Code of Conduct in good faith may face temporary or permanent repercussions as determined by other members of the project's leadership.
40 |
41 | ## Attribution
42 |
43 | This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4, available at [http://contributor-covenant.org/version/1/4][version]
44 |
45 | [homepage]: http://contributor-covenant.org
46 | [version]: http://contributor-covenant.org/version/1/4/
47 |
--------------------------------------------------------------------------------
/js-app/src/main/kotlin/js/JsApp.kt:
--------------------------------------------------------------------------------
1 | package js
2 |
3 | import mp.ClassWithMultiPlatformFunctionality
4 | import mp.CommonClass
5 | import mp.CommonClassDelegatingToInternalClassHeader
6 | import mp.CommonInterface
7 | import mp.CommonObject
8 | import mp.MpCloseable
9 | import mp.SubClassHeaderDelegatedTo
10 | import mp.commonFunction
11 | import mp.mpUse
12 | import mp.multiPlatformFunction
13 | import mp.printThis
14 |
15 | fun main(arguments: Array) {
16 | header("Common function")
17 | println(commonFunction())
18 |
19 | header("Multi-platform function")
20 | println(multiPlatformFunction())
21 |
22 | val canBeClosed = CanBeClosed()
23 |
24 | header("Common extension function on expected interface")
25 | canBeClosed.printThis()
26 |
27 | header("Common generic extension function on expected interface")
28 | canBeClosed.mpUse {
29 | println("Using it through 'mpUse': $it")
30 | }
31 |
32 | // Instantiation of a common class
33 | val commonClass = CommonClass()
34 |
35 | header("Common extension function on an instance of a common class")
36 | commonClass.printThis()
37 |
38 | header("Common generic extension function on an instance of a common class")
39 | commonClass.mpUse { cc ->
40 | // Instantiation of a common interface, and passing it to a function
41 | cc.execute(object : CommonInterface {
42 | override fun doIt() {
43 | println("Doing it in JavaScript through a common class instance")
44 | // Common extension function on an instance of a common interface
45 | printThis()
46 | }
47 |
48 | override fun toString() = "Common interface instance passed to an instance of the common class"
49 | })
50 | }
51 |
52 | header("Use of a common object, and common extension function on a common object")
53 | CommonObject.printThis()
54 |
55 | header("Common generic extension function on a common object")
56 | CommonObject.mpUse { co ->
57 | co.execute(object : CommonInterface {
58 | override fun doIt() {
59 | println("Doing it in JavaScript through the common object")
60 | // Common generic extension function on an instance of a common interface
61 | mpUse { ci ->
62 | println("Using common interface instance: $ci")
63 | }
64 | }
65 |
66 | override fun toString() = "Common interface instance passed to the common object"
67 | })
68 | }
69 |
70 | val classWithMultiPlatformFunctionality = ClassWithMultiPlatformFunctionality()
71 |
72 | header("Invocation of a function defined by the expected class")
73 | classWithMultiPlatformFunctionality.commonFunctionality()
74 |
75 | header("Invocation of a function defined by the JavaScript class implementation")
76 | classWithMultiPlatformFunctionality.javascriptFunctionality()
77 |
78 | header("Invocation of a function on an instance of a common class that delegates to a multi-platform internal class")
79 | CommonClassDelegatingToInternalClassHeader().execute()
80 |
81 | // Instantiation of a multi-platform sub class, that has a base class containing common code
82 | val subClassDelegatedTo = SubClassHeaderDelegatedTo()
83 |
84 | header("Invocation of a base class function that delegates to a multi-platform member function")
85 | subClassDelegatedTo.execute()
86 | }
87 |
88 | class CanBeClosed : MpCloseable {
89 | override fun close() {
90 | println("Closed: $this")
91 | }
92 |
93 | override fun toString() = "I can be closed"
94 | }
95 |
96 | private fun header(text: String) {
97 | println()
98 | println("##### $text #####")
99 | }
100 |
101 |
--------------------------------------------------------------------------------
/jvm-app/src/main/kotlin/jvm/JvmApp.kt:
--------------------------------------------------------------------------------
1 | package jvm
2 |
3 | import mp.ClassWithMultiPlatformFunctionality
4 | import mp.CommonClass
5 | import mp.CommonClassDelegatingToInternalClassHeader
6 | import mp.CommonInterface
7 | import mp.CommonObject
8 | import mp.MpCloseable
9 | import mp.SubClassHeaderDelegatedTo
10 | import mp.commonFunction
11 | import mp.mpUse
12 | import mp.multiPlatformFunction
13 | import mp.printThis
14 |
15 | fun main(arguments: Array) {
16 | header("Common function")
17 | println(commonFunction())
18 |
19 | header("Multi-platform function")
20 | println(multiPlatformFunction())
21 |
22 | val canBeClosed = CanBeClosed()
23 |
24 | header("Common extension function on expected interface")
25 | canBeClosed.printThis()
26 |
27 | header("Common generic extension function on expected interface")
28 | canBeClosed.mpUse {
29 | println("Using it through 'mpUse': $it")
30 | }
31 |
32 | header("Extension function from the standard library on a type-aliased expected interface")
33 | canBeClosed.use {
34 | println("Using it through 'use': $it")
35 | }
36 |
37 | // Instantiation of a common class
38 | val commonClass = CommonClass()
39 |
40 | header("Common extension function on an instance of a common class")
41 | commonClass.printThis()
42 |
43 | header("Common generic extension function on an instance of a common class")
44 | commonClass.mpUse { cc ->
45 | // Instantiation of a common interface, and passing it to a function
46 | cc.execute(object : CommonInterface {
47 | override fun doIt() {
48 | println("Doing it in Java through a common class instance")
49 | // Common extension function on an instance of a common interface
50 | printThis()
51 | }
52 |
53 | override fun toString() = "Common interface instance passed to an instance of the common class"
54 | })
55 | }
56 |
57 | header("Use of a common object, and common extension function on a common object")
58 | CommonObject.printThis()
59 |
60 | header("Common generic extension function on a common object")
61 | CommonObject.mpUse { co ->
62 | co.execute(object : CommonInterface {
63 | override fun doIt() {
64 | println("Doing it in Java through the common object")
65 | // Common generic extension function on an instance of a common interface
66 | mpUse { ci ->
67 | println("Using common interface instance: $ci")
68 | }
69 | }
70 |
71 | override fun toString() = "Common interface instance passed to the common object"
72 | })
73 | }
74 |
75 | val classWithMultiPlatformFunctionality = ClassWithMultiPlatformFunctionality()
76 |
77 | header("Invocation of a function defined by the expected class")
78 | classWithMultiPlatformFunctionality.commonFunctionality()
79 |
80 | header("Invocation of a function defined by the Java class implementation")
81 | classWithMultiPlatformFunctionality.javaFunctionality()
82 |
83 | header("Invocation of a function on an instance of a common class that delegates to a multi-platform internal class")
84 | CommonClassDelegatingToInternalClassHeader().execute()
85 |
86 | // Instantiation of a multi-platform sub class, that has a base class containing common code
87 | val subClassDelegatedTo = SubClassHeaderDelegatedTo()
88 |
89 | header("Invocation of a base class function that delegates to a multi-platform member function")
90 | subClassDelegatedTo.execute()
91 | }
92 |
93 | class CanBeClosed : MpCloseable {
94 | override fun close() {
95 | println("Closed: $this")
96 | }
97 |
98 | override fun toString() = "I can be closed"
99 | }
100 |
101 | private fun header(text: String) {
102 | println()
103 | println("##### $text #####")
104 | }
105 |
--------------------------------------------------------------------------------
/gradlew:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env sh
2 |
3 | ##############################################################################
4 | ##
5 | ## Gradle start up script for UN*X
6 | ##
7 | ##############################################################################
8 |
9 | # Attempt to set APP_HOME
10 | # Resolve links: $0 may be a link
11 | PRG="$0"
12 | # Need this for relative symlinks.
13 | while [ -h "$PRG" ] ; do
14 | ls=`ls -ld "$PRG"`
15 | link=`expr "$ls" : '.*-> \(.*\)$'`
16 | if expr "$link" : '/.*' > /dev/null; then
17 | PRG="$link"
18 | else
19 | PRG=`dirname "$PRG"`"/$link"
20 | fi
21 | done
22 | SAVED="`pwd`"
23 | cd "`dirname \"$PRG\"`/" >/dev/null
24 | APP_HOME="`pwd -P`"
25 | cd "$SAVED" >/dev/null
26 |
27 | APP_NAME="Gradle"
28 | APP_BASE_NAME=`basename "$0"`
29 |
30 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
31 | DEFAULT_JVM_OPTS=""
32 |
33 | # Use the maximum available, or set MAX_FD != -1 to use that value.
34 | MAX_FD="maximum"
35 |
36 | warn () {
37 | echo "$*"
38 | }
39 |
40 | die () {
41 | echo
42 | echo "$*"
43 | echo
44 | exit 1
45 | }
46 |
47 | # OS specific support (must be 'true' or 'false').
48 | cygwin=false
49 | msys=false
50 | darwin=false
51 | nonstop=false
52 | case "`uname`" in
53 | CYGWIN* )
54 | cygwin=true
55 | ;;
56 | Darwin* )
57 | darwin=true
58 | ;;
59 | MINGW* )
60 | msys=true
61 | ;;
62 | NONSTOP* )
63 | nonstop=true
64 | ;;
65 | esac
66 |
67 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar
68 |
69 | # Determine the Java command to use to start the JVM.
70 | if [ -n "$JAVA_HOME" ] ; then
71 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
72 | # IBM's JDK on AIX uses strange locations for the executables
73 | JAVACMD="$JAVA_HOME/jre/sh/java"
74 | else
75 | JAVACMD="$JAVA_HOME/bin/java"
76 | fi
77 | if [ ! -x "$JAVACMD" ] ; then
78 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME
79 |
80 | Please set the JAVA_HOME variable in your environment to match the
81 | location of your Java installation."
82 | fi
83 | else
84 | JAVACMD="java"
85 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
86 |
87 | Please set the JAVA_HOME variable in your environment to match the
88 | location of your Java installation."
89 | fi
90 |
91 | # Increase the maximum file descriptors if we can.
92 | if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then
93 | MAX_FD_LIMIT=`ulimit -H -n`
94 | if [ $? -eq 0 ] ; then
95 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then
96 | MAX_FD="$MAX_FD_LIMIT"
97 | fi
98 | ulimit -n $MAX_FD
99 | if [ $? -ne 0 ] ; then
100 | warn "Could not set maximum file descriptor limit: $MAX_FD"
101 | fi
102 | else
103 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT"
104 | fi
105 | fi
106 |
107 | # For Darwin, add options to specify how the application appears in the dock
108 | if $darwin; then
109 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\""
110 | fi
111 |
112 | # For Cygwin, switch paths to Windows format before running java
113 | if $cygwin ; then
114 | APP_HOME=`cygpath --path --mixed "$APP_HOME"`
115 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"`
116 | JAVACMD=`cygpath --unix "$JAVACMD"`
117 |
118 | # We build the pattern for arguments to be converted via cygpath
119 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null`
120 | SEP=""
121 | for dir in $ROOTDIRSRAW ; do
122 | ROOTDIRS="$ROOTDIRS$SEP$dir"
123 | SEP="|"
124 | done
125 | OURCYGPATTERN="(^($ROOTDIRS))"
126 | # Add a user-defined pattern to the cygpath arguments
127 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then
128 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)"
129 | fi
130 | # Now convert the arguments - kludge to limit ourselves to /bin/sh
131 | i=0
132 | for arg in "$@" ; do
133 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -`
134 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option
135 |
136 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition
137 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"`
138 | else
139 | eval `echo args$i`="\"$arg\""
140 | fi
141 | i=$((i+1))
142 | done
143 | case $i in
144 | (0) set -- ;;
145 | (1) set -- "$args0" ;;
146 | (2) set -- "$args0" "$args1" ;;
147 | (3) set -- "$args0" "$args1" "$args2" ;;
148 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;;
149 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;;
150 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;;
151 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;;
152 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;;
153 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;;
154 | esac
155 | fi
156 |
157 | # Escape application args
158 | save () {
159 | for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done
160 | echo " "
161 | }
162 | APP_ARGS=$(save "$@")
163 |
164 | # Collect all arguments for the java command, following the shell quoting and substitution rules
165 | eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS"
166 |
167 | # by default we should be in the correct project dir, but when run from Finder on Mac, the cwd is wrong
168 | if [ "$(uname)" = "Darwin" ] && [ "$HOME" = "$PWD" ]; then
169 | cd "$(dirname "$0")"
170 | fi
171 |
172 | exec "$JAVACMD" "$@"
173 |
--------------------------------------------------------------------------------