What's the best way for a ktor app to determine it...
# ktor
r
What's the best way for a ktor app to determine its version? I have a version.txt file which I read and set to gradle project version property in my .kts build script. But I don't know how to refer to it in the app without passing it as a parameter (would like to avoid this if possible). Would also like to avoid manually reading some file at runtime for this purpose if possible.
a
I think you can generate a code containing a property with the value (Ktor version) at the build time
👍 1
e
In my project we had gradle generate a version info object that we could refrence in code.
Did it as a separate gradel module with this build file:
Copy code
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile

plugins {
    id("myorg.kotlin-library-conventions")
    id("myorg.kotlin-publish-conventions")
}

val gitSha: String = if (project.hasProperty("gitsha")) {
    project.property("gitsha") as String
} else {
    "0000000000000000000000000000000000000000"
}

sourceSets {
    val main by getting {
        kotlin.srcDir("build/generated/kotlin")
    }
}

tasks.register("generateVersionObject") {
    val versionObjectFile = file("build/generated/kotlin/no/domstol/metadata/VersionData.kt")
    outputs.file(versionObjectFile)
    val bv = project.version
    val sha = gitSha
    doLast {
        versionObjectFile.writeText("""
            package no.domstol.metadata

            internal object VersionData {
                val version: String = "$bv"
                val gitSha: String = "$sha"
            }
        """.trimIndent())
    }
}.let { generateVersionTask ->
    tasks.withType<KotlinCompile>().configureEach {
        dependsOn(generateVersionTask)
    }

    tasks.named("sourcesJar") {
        dependsOn(generateVersionTask)
    }
}
🙌 1
thank you color 1
The generated file is internal because there is a more nicely written interface to access the info in the main source set that is not generated
Copy code
object VersionInfo {
    private val fullVersionString = "${VersionData.version} (git:${VersionData.gitSha})"
    private const val minShaLength = 7
    fun asVersionString(product: String): String = "$product/$fullVersionString"

    fun version(): String {
        return "${VersionData.version}.${VersionData.gitSha.take(minShaLength)}"
    }

    fun isSameVersion(versionString: String): Boolean {
        val otherString = versionString.split('/').last()
        return fullVersionString == otherString
    }

    fun ifNotSameVersion(versionString: String, block: (String, String) -> Unit) {
        val otherString = versionString.split('/').last()
        if (fullVersionString != otherString) {
            block(fullVersionString, otherString)
        }
    }
}
Not my nices work, but i did what we need. The values for git sha and version are set by the CI build
r
Thank you! I had a similar thing done for the client apps, so I just needed another opinion. I am going with this approach now 🙂