Skip to content

Commit

Permalink
This is supposed to work. testing.
Browse files Browse the repository at this point in the history
- this commit meant to make new release and test it by creating an app.
  • Loading branch information
YairLevi committed Jan 17, 2024
1 parent 65d663c commit a7866ec
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 16 deletions.
12 changes: 11 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<kotlin.version>1.8.21</kotlin.version>
<kotlin.version>1.9.22</kotlin.version>
</properties>

<repositories>
Expand Down Expand Up @@ -99,6 +99,16 @@
</build>

<dependencies>
<dependency>
<groupId>io.ktor</groupId>
<artifactId>ktor-server-host-common-jvm</artifactId>
<version>2.3.7</version>
</dependency>
<dependency>
<groupId>io.ktor</groupId>
<artifactId>ktor-server-netty-jvm</artifactId>
<version>2.3.7</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
Expand Down
46 changes: 37 additions & 9 deletions src/main/kotlin/org/levi/coffee/Window.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
package org.levi.coffee


import dev.webview.Webview
import io.ktor.server.engine.*
import io.ktor.server.http.content.*
import io.ktor.server.netty.*
import io.ktor.server.routing.*
import org.levi.coffee.internal.CodeGenerator
import org.levi.coffee.internal.MethodBinder
import org.levi.coffee.internal.util.FileUtil
Expand All @@ -10,13 +15,13 @@ import java.util.*
import java.util.function.Consumer
import kotlin.system.exitProcess

class Window(dev: Boolean = true) {
class Window(val dev: Boolean = true) {
private val log: Logger = LoggerFactory.getLogger(this::class.java)

private val _webview: Webview = Webview(dev)
private val _webview: Webview = Webview(true)
private val _beforeStartCallbacks: MutableList<Runnable> = ArrayList()
private val _onCloseCallbacks: MutableList<Runnable> = ArrayList()
private val _bindObjects: MutableList<Any> = ArrayList()
private val _bindObjects = ArrayList<Any>()
private var _url: String = ""

init {
Expand Down Expand Up @@ -72,7 +77,9 @@ class Window(dev: Boolean = true) {
}

fun bind(vararg objects: Any) {
_bindObjects.addAll(objects)
for (o in objects) {
_bindObjects.add(o)
}
}

fun addBeforeStartCallback(r: Runnable) {
Expand All @@ -85,18 +92,39 @@ class Window(dev: Boolean = true) {


fun run() {
CodeGenerator.generateEventsAPI()
CodeGenerator.generateTypes(*_bindObjects.toTypedArray())
CodeGenerator.generateFunctions(*_bindObjects.toTypedArray())
MethodBinder.bind(_webview, *_bindObjects.toTypedArray())
if (dev) {
val cg = CodeGenerator()
cg.generateTypes(*_bindObjects.toTypedArray())
cg.generateFunctions(*_bindObjects.toTypedArray())
cg.generateEventsAPI()
}

_beforeStartCallbacks.forEach(Consumer { it.run() })
var server: NettyApplicationEngine? = null
if (!dev) {
val prodPort = 4567
server = embeddedServer(Netty, port = prodPort, host = "localhost") {
routing {
staticResources("/", "dist") {
default("index.html")
preCompressed(CompressedFileType.GZIP)
}
}
}
server.start()
_url = "http://localhost:$prodPort"
}

MethodBinder.bind(_webview, *_bindObjects.toTypedArray())
Ipc.setWebview(_webview)
_beforeStartCallbacks.forEach(Consumer { it.run() })

_webview.loadURL(_url)
_webview.run()

_onCloseCallbacks.forEach(Consumer { it.run() })
_webview.close()

server?.stop()

}
}
12 changes: 6 additions & 6 deletions src/main/kotlin/org/levi/coffee/internal/CodeGenerator.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ import java.io.IOException
import java.io.PrintWriter
import kotlin.system.exitProcess

internal object CodeGenerator {
internal class CodeGenerator {
private val log = LoggerFactory.getLogger(this::class.java)

private const val CLIENT_FOLDER_PATH = "frontend/coffee/"
private const val METHODS_FOLDER_PATH = CLIENT_FOLDER_PATH + "methods/"
private const val TYPES_FILE_PATH = CLIENT_FOLDER_PATH + "types.ts"
private val CLIENT_FOLDER_PATH = "frontend/coffee/"
private val METHODS_FOLDER_PATH = CLIENT_FOLDER_PATH + "methods/"
private val TYPES_FILE_PATH = CLIENT_FOLDER_PATH + "types.ts"

private val ipcResources = listOf("events.ts", "window.ts")

Expand All @@ -22,7 +22,7 @@ internal object CodeGenerator {
FileUtil.createOrReplaceFile(TYPES_FILE_PATH)
FileUtil.createOrReplaceDirectory(METHODS_FOLDER_PATH)
}

fun generateEventsAPI() {
for (resource in ipcResources) {
FileUtil.createOrReplaceFile(CLIENT_FOLDER_PATH + resource)
Expand All @@ -31,8 +31,8 @@ internal object CodeGenerator {
?: throw Exception("Failed to find $resource in resources.")

File(CLIENT_FOLDER_PATH + resource).printWriter().use { out -> out.println(eventsResource.readText()) }
log.info("Created events API files.")
}
log.info("Created events API files.")
}

fun generateTypes(vararg objects: Any) {
Expand Down

0 comments on commit a7866ec

Please sign in to comment.