Skip to content

Commit 54bfca6

Browse files
authored
fix(jit): fail if compilation failed (#1346)
Part of #1318. Thanks to this, the server responds with HTTP 500 if the binding couldn't be compiled.
1 parent f32cfa7 commit 54bfca6

File tree

2 files changed

+25
-15
lines changed
  • jit-binding-server/src/main/kotlin/io/github/typesafegithub/workflows/jitbindingserver
  • maven-binding-builder/src/main/kotlin/io/github/typesafegithub/workflows/mavenbinding

2 files changed

+25
-15
lines changed

jit-binding-server/src/main/kotlin/io/github/typesafegithub/workflows/jitbindingserver/Main.kt

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import io.ktor.http.HttpStatusCode
88
import io.ktor.server.application.call
99
import io.ktor.server.engine.embeddedServer
1010
import io.ktor.server.netty.Netty
11-
import io.ktor.server.response.respondOutputStream
11+
import io.ktor.server.response.respondBytes
1212
import io.ktor.server.response.respondText
1313
import io.ktor.server.routing.get
1414
import io.ktor.server.routing.head
@@ -23,12 +23,13 @@ fun main() {
2323
val version = call.parameters["version"]!!
2424
val file = call.parameters["file"]!!
2525
when (file) {
26-
"$name-$version.jar" ->
27-
call.respondOutputStream(
26+
"$name-$version.jar" -> {
27+
val jarByteArray = buildJar(owner = owner, name = name, version = version)
28+
call.respondBytes(
29+
bytes = jarByteArray,
2830
contentType = ContentType.parse("application/java-archive"),
29-
status = HttpStatusCode.OK,
30-
producer = { this.buildJar(owner = owner, name = name, version = version) },
3131
)
32+
}
3233
"$name-$version.pom" -> call.respondText(buildPomFile(owner = owner, name = name, version = version))
3334
"$name-$version.module" -> call.respondText(buildModuleFile(owner = owner, name = name, version = version))
3435
else -> call.respondText(text = "Not found", status = HttpStatusCode.NotFound)

maven-binding-builder/src/main/kotlin/io/github/typesafegithub/workflows/mavenbinding/JarBuilding.kt

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,27 +5,31 @@ import io.github.typesafegithub.workflows.actionbindinggenerator.domain.NewestFo
55
import io.github.typesafegithub.workflows.actionbindinggenerator.generation.ActionBinding
66
import io.github.typesafegithub.workflows.actionbindinggenerator.generation.ClientType
77
import io.github.typesafegithub.workflows.actionbindinggenerator.generation.generateBinding
8+
import org.jetbrains.kotlin.cli.common.ExitCode
89
import org.jetbrains.kotlin.cli.common.arguments.K2JVMCompilerArguments
910
import org.jetbrains.kotlin.cli.common.messages.MessageRenderer
1011
import org.jetbrains.kotlin.cli.common.messages.PrintingMessageCollector
1112
import org.jetbrains.kotlin.cli.jvm.K2JVMCompiler
1213
import org.jetbrains.kotlin.config.Services
13-
import java.io.OutputStream
14+
import java.io.ByteArrayOutputStream
15+
import java.io.PrintStream
1416
import java.nio.file.Path
1517
import kotlin.io.path.Path
1618
import kotlin.io.path.createParentDirectories
1719
import kotlin.io.path.createTempDirectory
1820
import kotlin.io.path.div
1921
import kotlin.io.path.writeText
2022

21-
fun OutputStream.buildJar(
23+
fun buildJar(
2224
owner: String,
2325
name: String,
2426
version: String,
25-
) {
27+
): ByteArray {
2628
val binding = generateBinding(owner = owner, name = name, version = version)
2729
val pathWithJarContents = binding.compileBinding()
28-
return this.createZipFile(pathWithJarContents)
30+
val byteArrayOutputStream = ByteArrayOutputStream()
31+
byteArrayOutputStream.createZipFile(pathWithJarContents)
32+
return byteArrayOutputStream.toByteArray()
2933
}
3034

3135
private fun generateBinding(
@@ -62,16 +66,21 @@ private fun ActionBinding.compileBinding(): Path {
6266
noReflect = true
6367
includeRuntime = false
6468
}
69+
val compilerMessagesOutputStream = ByteArrayOutputStream()
6570
val compilerMessageCollector =
6671
PrintingMessageCollector(
67-
System.out,
72+
PrintStream(compilerMessagesOutputStream),
6873
MessageRenderer.GRADLE_STYLE,
6974
false,
7075
)
71-
K2JVMCompiler().exec(
72-
messageCollector = compilerMessageCollector,
73-
services = Services.EMPTY,
74-
arguments = args,
75-
)
76+
val exitCode =
77+
K2JVMCompiler().exec(
78+
messageCollector = compilerMessageCollector,
79+
services = Services.EMPTY,
80+
arguments = args,
81+
)
82+
require(exitCode == ExitCode.OK) {
83+
"Binding compilation failed! Compiler messages: $compilerMessagesOutputStream"
84+
}
7685
return compilationOutput
7786
}

0 commit comments

Comments
 (0)