Skip to content

Commit

Permalink
perf(abg): create JARs only when they are actually requested (#1615)
Browse files Browse the repository at this point in the history
In some cases, only e.g. a POM is necessary to fetch for a given action.
Thanks to this change, no Kotlin compilation or JAR creation will happen
when requesting an artifact that doesn't require it.
  • Loading branch information
Vampire authored Sep 18, 2024
1 parent f649d84 commit a112e28
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -109,10 +109,10 @@ private fun Route.artifact(
val file = call.parameters["file"]!!
if (file in bindingArtifacts) {
when (val artifact = bindingArtifacts[file]) {
is TextArtifact -> call.respondText(artifact.data)
is TextArtifact -> call.respondText(text = artifact.data())
is JarArtifact ->
call.respondBytes(
bytes = artifact.data,
bytes = artifact.data(),
contentType = ContentType.parse("application/java-archive"),
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ import kotlin.io.path.div
import kotlin.io.path.writeText

internal data class Jars(
val mainJar: ByteArray,
val sourcesJar: ByteArray,
val mainJar: () -> ByteArray,
val sourcesJar: () -> ByteArray,
)

internal fun buildJars(
Expand All @@ -35,20 +35,28 @@ internal fun buildJars(
generateBinding(owner = owner, name = name, version = version).also {
if (it.isEmpty()) return null
}
val (sourceFilePaths, compilationInputDir) = binding.prepareDirectoryWithSources()

val pathWithJarContents = compileBinding(sourceFilePaths = sourceFilePaths)
val mainJarByteArrayOutputStream = ByteArrayOutputStream()
mainJarByteArrayOutputStream.createZipFile(pathWithJarContents)
pathWithJarContents.toFile().deleteRecursively()
val mainJar by lazy {
val (sourceFilePaths, compilationInputDir) = binding.prepareDirectoryWithSources()
val pathWithJarContents = compileBinding(sourceFilePaths = sourceFilePaths)
val mainJarByteArrayOutputStream = ByteArrayOutputStream()
mainJarByteArrayOutputStream.createZipFile(pathWithJarContents)
pathWithJarContents.toFile().deleteRecursively()
compilationInputDir.toFile().deleteRecursively()
mainJarByteArrayOutputStream.toByteArray()
}

val sourcesJarByteArrayOutputStream = ByteArrayOutputStream()
sourcesJarByteArrayOutputStream.createZipFile(compilationInputDir)
compilationInputDir.toFile().deleteRecursively()
val sourcesJar by lazy {
val (_, compilationInputDir) = binding.prepareDirectoryWithSources()
val sourcesJarByteArrayOutputStream = ByteArrayOutputStream()
sourcesJarByteArrayOutputStream.createZipFile(compilationInputDir)
compilationInputDir.toFile().deleteRecursively()
sourcesJarByteArrayOutputStream.toByteArray()
}

return Jars(
mainJar = mainJarByteArrayOutputStream.toByteArray(),
sourcesJar = sourcesJarByteArrayOutputStream.toByteArray(),
mainJar = { mainJar },
sourcesJar = { sourcesJar },
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ import io.github.typesafegithub.workflows.actionbindinggenerator.domain.ActionCo
sealed interface Artifact

data class TextArtifact(
val data: String,
val data: () -> String,
) : Artifact

data class JarArtifact(
val data: ByteArray,
val data: () -> ByteArray,
) : Artifact

fun ActionCoords.buildVersionArtifacts(): Map<String, Artifact>? {
Expand All @@ -18,12 +18,12 @@ fun ActionCoords.buildVersionArtifacts(): Map<String, Artifact>? {
val module = buildModuleFile(owner = owner, name = name.replace("__", "/"), version = version)
return mapOf(
"$name-$version.jar" to JarArtifact(jars.mainJar),
"$name-$version.jar.md5" to TextArtifact(jars.mainJar.md5Checksum()),
"$name-$version.jar.md5" to TextArtifact { jars.mainJar().md5Checksum() },
"$name-$version-sources.jar" to JarArtifact(jars.sourcesJar),
"$name-$version-sources.jar.md5" to TextArtifact(jars.sourcesJar.md5Checksum()),
"$name-$version.pom" to TextArtifact(pom),
"$name-$version.pom.md5" to TextArtifact(pom.md5Checksum()),
"$name-$version.module" to TextArtifact(module),
"$name-$version.module.md5" to TextArtifact(module.md5Checksum()),
"$name-$version-sources.jar.md5" to TextArtifact { jars.sourcesJar().md5Checksum() },
"$name-$version.pom" to TextArtifact { pom },
"$name-$version.pom.md5" to TextArtifact { pom.md5Checksum() },
"$name-$version.module" to TextArtifact { module },
"$name-$version.module.md5" to TextArtifact { module.md5Checksum() },
)
}

0 comments on commit a112e28

Please sign in to comment.