Skip to content

Commit

Permalink
Merge pull request #109 from outfoxx/task/native
Browse files Browse the repository at this point in the history
Support building native images using Graal
  • Loading branch information
kdubb authored Oct 27, 2024
2 parents b9ad2a1 + 8481383 commit e9afd71
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 10 deletions.
19 changes: 19 additions & 0 deletions cli/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ plugins {
application
alias(libs.plugins.jib)
alias(libs.plugins.shadow)
alias(libs.plugins.graalNative)
}

dependencies {
Expand Down Expand Up @@ -43,6 +44,24 @@ tasks {
}
}

graalvmNative {
binaries.all {
resources.autodetect()
}
binaries {
named("main") {
imageName = "sunday"
sharedLibrary = false
}
}
}
tasks.generateResourcesConfigFile {
dependsOn(tasks.shadowJar)
}
tasks.nativeCompile {
classpathJar = tasks.shadowJar.flatMap { it.archiveFile }
}

publishing {
publications {
create<MavenPublication>("cli") {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,11 @@ package io.outfoxx.sunday.generator
import com.github.ajalt.clikt.core.CliktCommand
import com.github.ajalt.clikt.core.Context
import com.github.ajalt.clikt.output.MordantMarkdownHelpFormatter
import io.outfoxx.sunday.generator.utils.LocalSundayDefinitionResoureceLoader

class GenerateCommand : CliktCommand(name = "sunday") {
init {
LocalSundayDefinitionResoureceLoader.check()
configureContext {
helpFormatter = { MordantMarkdownHelpFormatter(it, showDefaultValues = true, showRequiredTag = true) }
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import amf.core.client.common.transform.PipelineId
import amf.core.client.common.validation.SeverityLevels
import amf.core.client.platform.model.document.Document
import amf.core.client.platform.validation.AMFValidationResult
import io.outfoxx.sunday.generator.utils.LocalResourceLoader
import io.outfoxx.sunday.generator.utils.LocalSundayDefinitionResoureceLoader
import java.net.URI
import java.util.concurrent.ExecutionException

Expand Down Expand Up @@ -71,7 +71,7 @@ open class APIProcessor {

val ramlClient =
RAMLConfiguration.RAML10()
.withResourceLoader(LocalResourceLoader)
.withResourceLoader(LocalSundayDefinitionResoureceLoader)
.baseUnitClient()

val (unresolvedDocument, validationResults) =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,18 @@ import amf.core.client.platform.resource.HttpResourceLoader
import amf.core.client.platform.resource.ResourceLoader
import java.util.concurrent.CompletableFuture

object LocalResourceLoader : ResourceLoader {
object LocalSundayDefinitionResoureceLoader : ResourceLoader {

private val sundayRAML: ByteArray
init {
val content = LocalSundayDefinitionResoureceLoader::class.java.getResource("/sunday.raml")
?: throw IllegalStateException("Could not find sunday.raml resource")
sundayRAML = content.readBytes()
}

fun check() {
check(sundayRAML.isNotEmpty()) { "Could not find sunday.raml resource" }
}

private val httpLoader = HttpResourceLoader()

Expand All @@ -30,12 +41,7 @@ object LocalResourceLoader : ResourceLoader {
}

override fun fetch(resource: String): CompletableFuture<Content> {
val bytes = LocalResourceLoader::class.java.getResource("/sunday.raml")?.openStream()?.readAllBytes()
return if (bytes != null) {
val content = Content(String(bytes, Charsets.UTF_8), resource)
CompletableFuture.completedFuture(content)
} else {
return httpLoader.fetch(resource)
}
val content = Content(String(sundayRAML, Charsets.UTF_8), resource)
return CompletableFuture.completedFuture(content)
}
}
2 changes: 2 additions & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ dockerJava = "3.4.0"
dokka = "1.9.0"
# @pin
githubRelease = "2.4.1"
graalNative = "0.10.3"
hamcrest = "3.0"
jackson = "2.18.0"
jakartaJaxrs = "4.0.0"
Expand Down Expand Up @@ -44,6 +45,7 @@ jacocoTool = "0.8.12"
[plugins]
dokka = { id = "org.jetbrains.dokka", version.ref = "dokka" }
githubRelease = { id = "com.github.breadmoirai.github-release", version.ref = "githubRelease" }
graalNative = { id = "org.graalvm.buildtools.native", version.ref = "graalNative" }
jib = { id = "com.google.cloud.tools.jib", version.ref = "jib" }
kotlin = { id = "org.jetbrains.kotlin.jvm", version.ref = "kotlin" }
licenser = { id = "dev.yumi.gradle.licenser", version.ref = "licenser" }
Expand Down

0 comments on commit e9afd71

Please sign in to comment.