Skip to content

Commit

Permalink
[Fixes #328] Add Git hash/branch to props and logging
Browse files Browse the repository at this point in the history
  • Loading branch information
gdude2002 committed Sep 3, 2024
1 parent e42dde8 commit c227e2d
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 4 deletions.
15 changes: 14 additions & 1 deletion buildSrc/src/main/kotlin/GitTools.kt
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,23 @@ fun Project.getCurrentGitBranch(): String { // https://gist.github.com/lordcode
var gitBranch = "Unknown branch"

try {
gitBranch = runCommand("git rev-parse --abbrev-ref HEAD")
gitBranch = runCommand("git rev-parse --abbrev-ref HEAD").trim()
} catch (t: Throwable) {
println(t)
}

return gitBranch
}


fun Project.getCurrentGitHash(): String { // https://gist.github.com/lordcodes/15b2a4aecbeff7c3238a70bfd20f0931
var gitHash = "unknown"

try {
gitHash = runCommand("git rev-parse --short HEAD").trim()
} catch (t: Throwable) {
println(t)
}

return gitHash
}
7 changes: 5 additions & 2 deletions buildSrc/src/main/kotlin/kordex-module.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,14 @@ tasks {
description = "Generate KordEx properties file"

comment = "Generated during KordEx compilation"
destinationFile = layout.buildDirectory.file("kordex.properties")
destinationFile = layout.buildDirectory.file("kordex-build.properties")
encoding = "UTF-8"

property("versions.kordEx", project.version)
property("git.branch", getCurrentGitBranch())
property("git.hash", getCurrentGitHash())

property("versions.kord", libs.findVersion("kord").get())
property("versions.kordEx", project.version)
}

build {
Expand Down
25 changes: 25 additions & 0 deletions kord-extensions/src/main/kotlin/dev/kordex/core/_Properties.kt
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,19 @@ public val kordexProps: Properties by lazy {
props
}

/** Convenient access to the properties stored within `kordex-build.properties` in your bot's resources. **/
public val kordexBuildProps: Properties by lazy {
val props = Properties()

props.load(
ExtensibleBotBuilder::class.java.getResourceAsStream(
"/kordex-build.properties"
)
)

props
}

/**
* Location of the data collection state file.
*
Expand Down Expand Up @@ -88,10 +101,22 @@ public val KORDEX_MODULES: List<String> by lazy {
/** Current Kord Extensions version. **/
public val KORDEX_VERSION: String? by lazy {
kordexProps["versions.kordEx"] as? String
?: kordexBuildProps["versions.kordEx"] as? String
}

/** Current Kord version. **/
public val KORD_VERSION: String? by lazy {
kordexProps["versions.kord"] as? String
?: kordexProps["kordVersion"] as? String
?: kordexBuildProps["versions.kord"] as? String
}

/** Git branch used to build this KordEx release. **/
public val KORDEX_GIT_BRANCH: String? by lazy {
kordexBuildProps["git.branch"] as? String
}

/** Hash corresponding with the Git commit used to build this KordEx release. **/
public val KORDEX_GIT_HASH: String? by lazy {
kordexBuildProps["git.hash"] as? String
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ import dev.kord.rest.builder.message.create.MessageCreateBuilder
import dev.kordex.core.DATA_COLLECTION
import dev.kordex.core.DEV_MODE
import dev.kordex.core.ExtensibleBot
import dev.kordex.core.KORDEX_GIT_BRANCH
import dev.kordex.core.KORDEX_GIT_HASH
import dev.kordex.core.KORDEX_VERSION
import dev.kordex.core.KORD_VERSION
import dev.kordex.core.annotations.BotBuilderDSL
Expand Down Expand Up @@ -522,7 +524,10 @@ public open class ExtensibleBotBuilder {

/** @suppress Internal function used to build a bot instance. **/
public open suspend fun build(token: String): ExtensibleBot {
logger.info { "Starting bot with Kord Extensions v$KORDEX_VERSION and Kord v$KORD_VERSION" }
logger.info {
"Starting bot with Kord Extensions v$KORDEX_VERSION ($KORDEX_GIT_BRANCH@$KORDEX_GIT_HASH) " +
"and Kord v$KORD_VERSION"
}

hooksBuilder.beforeKoinSetup { // We have to do this super-duper early for safety
loadModule { single { dataAdapterCallback() } bind DataAdapter::class }
Expand Down

0 comments on commit c227e2d

Please sign in to comment.