Native: move samples to backend.native/tests/
This commit is contained in:
committed by
Space
parent
b7337d2e64
commit
7bf6d64cfb
@@ -0,0 +1,96 @@
|
||||
import org.jetbrains.kotlin.gradle.dsl.KotlinJvmCompile
|
||||
import org.jetbrains.kotlin.gradle.targets.jvm.KotlinJvmTarget
|
||||
import org.jetbrains.kotlin.gradle.tasks.AbstractKotlinNativeCompile
|
||||
|
||||
plugins {
|
||||
kotlin("multiplatform")
|
||||
}
|
||||
|
||||
repositories {
|
||||
jcenter()
|
||||
}
|
||||
|
||||
val hostOs = System.getProperty("os.name")
|
||||
val isWindows = hostOs.startsWith("Windows")
|
||||
|
||||
val packageName = "kotlinx.interop.wasm.dom"
|
||||
val jsinteropKlibFile = buildDir.resolve("klib").resolve("$packageName-jsinterop.klib")
|
||||
|
||||
kotlin {
|
||||
wasm32("html5Canvas") {
|
||||
binaries {
|
||||
executable {
|
||||
entryPoint = "sample.html5canvas.main"
|
||||
}
|
||||
}
|
||||
}
|
||||
jvm("httpServer")
|
||||
sourceSets {
|
||||
val html5CanvasMain by getting {
|
||||
dependencies {
|
||||
implementation(files(jsinteropKlibFile))
|
||||
}
|
||||
}
|
||||
val httpServerMain by getting {
|
||||
dependencies {
|
||||
implementation("io.ktor:ktor-server-netty:1.2.1")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
val jsinterop by tasks.creating(Exec::class) {
|
||||
workingDir = projectDir
|
||||
|
||||
val ext = if (isWindows) ".bat" else ""
|
||||
val distributionPath = project.properties["kotlin.native.home"] as String?
|
||||
|
||||
if (distributionPath != null) {
|
||||
val jsinteropCommand = file(distributionPath).resolve("bin").resolve("jsinterop$ext")
|
||||
|
||||
inputs.property("jsinteropCommand", jsinteropCommand)
|
||||
inputs.property("jsinteropPackageName", packageName)
|
||||
outputs.file(jsinteropKlibFile)
|
||||
|
||||
commandLine(
|
||||
jsinteropCommand,
|
||||
"-pkg", packageName,
|
||||
"-o", jsinteropKlibFile,
|
||||
"-target", "wasm32"
|
||||
)
|
||||
} else {
|
||||
doFirst {
|
||||
// Abort build execution if the distribution path isn't specified.
|
||||
throw GradleException(
|
||||
"""
|
||||
|
|
||||
|Kotlin/Native distribution path must be specified to build the JavaScript interop.
|
||||
|Use 'kotlin.native.home' project property to specify it.
|
||||
""".trimMargin()
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
tasks.withType(AbstractKotlinNativeCompile::class).all {
|
||||
dependsOn(jsinterop)
|
||||
}
|
||||
|
||||
val assemble by tasks.getting
|
||||
|
||||
// This is to run embedded HTTP server with Ktor:
|
||||
val runProgram by tasks.creating(JavaExec::class) {
|
||||
dependsOn(assemble)
|
||||
|
||||
val httpServer: KotlinJvmTarget by kotlin.targets
|
||||
val httpServerMainCompilation = httpServer.compilations["main"]
|
||||
|
||||
main = "sample.html5canvas.httpserver.HttpServer"
|
||||
classpath = files(httpServerMainCompilation.output) + httpServerMainCompilation.runtimeDependencyFiles
|
||||
args = listOf(projectDir.toString())
|
||||
}
|
||||
|
||||
tasks.withType(KotlinJvmCompile::class).all {
|
||||
runProgram.dependsOn(this)
|
||||
kotlinOptions.jvmTarget = "1.8"
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
kotlin.code.style=official
|
||||
kotlin.import.noCommonSourceSets=true
|
||||
@@ -0,0 +1,14 @@
|
||||
<html>
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
|
||||
<script src="build/bin/html5Canvas/releaseExecutable/html5Canvas.wasm.js" wasm="build/bin/html5Canvas/releaseExecutable/html5Canvas.wasm"> </script>
|
||||
|
||||
</head>
|
||||
<body>
|
||||
<canvas id="myCanvas" width="640" height="480" style="border:1px solid #000000;">
|
||||
</canvas>
|
||||
<p>Draw something using the mouse!</p>
|
||||
</body>
|
||||
</html>
|
||||
|
||||
+53
@@ -0,0 +1,53 @@
|
||||
/*
|
||||
* 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 sample.html5canvas
|
||||
|
||||
import kotlinx.interop.wasm.dom.*
|
||||
import kotlinx.wasm.jsinterop.*
|
||||
|
||||
fun main() {
|
||||
|
||||
val canvas = document.getElementById("myCanvas").asCanvas
|
||||
val ctx = canvas.getContext("2d")
|
||||
val rect = canvas.getBoundingClientRect()
|
||||
val rectLeft = rect.left
|
||||
val rectTop = rect.top
|
||||
|
||||
var mouseX: Int = 0
|
||||
var mouseY: Int = 0
|
||||
var draw: Boolean = false
|
||||
|
||||
document.setter("onmousemove") { arguments: ArrayList<JsValue> ->
|
||||
val event = MouseEvent(arguments[0])
|
||||
mouseX = event.getInt("clientX") - rectLeft
|
||||
mouseY = event.getInt("clientY") - rectTop
|
||||
|
||||
if (mouseX < 0) mouseX = 0
|
||||
if (mouseX > 639) mouseX = 639
|
||||
if (mouseY < 0) mouseY = 0
|
||||
if (mouseY > 479) mouseY = 479
|
||||
}
|
||||
|
||||
document.setter("onmousedown") {
|
||||
draw = true
|
||||
}
|
||||
|
||||
document.setter("onmouseup") {
|
||||
draw = false
|
||||
}
|
||||
|
||||
setInterval(10) {
|
||||
if (draw) {
|
||||
ctx.strokeStyle = "#222222"
|
||||
ctx.lineTo(mouseX, mouseY)
|
||||
ctx.stroke()
|
||||
} else {
|
||||
ctx.moveTo(mouseX, mouseY)
|
||||
ctx.stroke()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+59
@@ -0,0 +1,59 @@
|
||||
/*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
@file:JvmName("HttpServer")
|
||||
|
||||
package sample.html5canvas.httpserver
|
||||
|
||||
import io.ktor.application.call
|
||||
import io.ktor.http.ContentType
|
||||
import io.ktor.http.content.LocalFileContent
|
||||
import io.ktor.http.content.default
|
||||
import io.ktor.http.content.files
|
||||
import io.ktor.http.content.static
|
||||
import io.ktor.response.respond
|
||||
import io.ktor.routing.get
|
||||
import io.ktor.routing.routing
|
||||
import io.ktor.server.engine.embeddedServer
|
||||
import io.ktor.server.netty.Netty
|
||||
import java.io.File
|
||||
|
||||
fun main(args: Array<String>) {
|
||||
|
||||
check(args.size == 1) { "Invalid number of arguments: $args.\nExpected one argument with content root." }
|
||||
|
||||
val contentRoot = File(args[0])
|
||||
check(contentRoot.isDirectory) { "Invalid content root: $contentRoot." }
|
||||
|
||||
println(
|
||||
"""
|
||||
|
||||
IMPORTANT: Please open http://localhost:8080/ in your browser!
|
||||
|
||||
To stop embedded HTTP server use Ctrl+C (Cmd+C for Mac OS X).
|
||||
|
||||
""".trimIndent()
|
||||
)
|
||||
|
||||
val server = embeddedServer(Netty, 8080) {
|
||||
routing {
|
||||
val wasm = "build/bin/html5Canvas/releaseExecutable/html5Canvas.wasm"
|
||||
get(wasm) {
|
||||
// TODO: ktor as of now doesn't know about 'application/wasm'.
|
||||
// The newer browsers (firefox and chrome at least) don't allow
|
||||
// 'application/octet-stream' for wasm anymore.
|
||||
// We provide the proper content type here and,
|
||||
// at the same time, put it into the ktor database.
|
||||
// Remove this whole get() clause when ktor fix is available.
|
||||
call.respond(LocalFileContent(File(wasm), ContentType("application", "wasm")))
|
||||
}
|
||||
static("/") {
|
||||
files(contentRoot)
|
||||
default("index.html")
|
||||
}
|
||||
}
|
||||
}
|
||||
server.start(wait = true)
|
||||
}
|
||||
Reference in New Issue
Block a user