Skip to content

Commit dd13802

Browse files
committed
chore: add git commit to generated buildconfig
1 parent 6cbef6c commit dd13802

File tree

8 files changed

+45
-14
lines changed

8 files changed

+45
-14
lines changed

common/build.gradle.kts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import common.jvmArguments
2+
import common.versionCatalogMapOf
23

34
plugins {
45
application
@@ -15,9 +16,7 @@ application {
1516

1617
buildConfig {
1718
version = project.version.toString()
18-
val catalog = project.versionCatalogs.named("libs")
19-
val catalogMap = catalog.versionAliases.associateWith { catalog.findVersion(it).get().toString() }
20-
catalogVersions = catalogMap
19+
catalogVersions = project.versionCatalogMapOf()
2120
}
2221

2322
dependencies {

common/src/commonMain/kotlin/dev/suresh/Greeting.kt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,14 @@ class Greeting {
2323
fun greeting() =
2424
"""
2525
| Platform : Kotlin $platform
26-
| Build Time : ${BuildConfig.time}
26+
| Build Time : ${BuildConfig.buildTime}
2727
| Build Version : ${BuildConfig.version}
2828
| Java Version : ${BuildConfig.java}
2929
| Kotlin Version : ${KotlinVersion.CURRENT}
3030
| Gradle Version : ${BuildConfig.gradle}
31+
| Git Hash : ${BuildConfig.gitHash}
32+
| Git Message : ${BuildConfig.gitMessage}
33+
| Git Tag : ${BuildConfig.gitTags}
3134
| ${KData("Foo", 20, "test")}
3235
| ${kotlinxTests()}
3336
"""

gradle/build-logic/common-plugins/src/main/kotlin/common/ProjectExtns.kt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,19 @@ val Project.incubatorModules
106106
.joinToString(",") { it.substringBefore("@").trim() }
107107
}
108108

109+
/**
110+
* Retrieves a map of version aliases and their corresponding versions from the project's version
111+
* catalog.
112+
*
113+
* @param name The name of the version catalog. Defaults to "libs" if not specified.
114+
* @return A map where the keys are the version aliases and the values are their corresponding
115+
* versions.
116+
*/
117+
fun Project.versionCatalogMapOf(name: String = "libs") = run {
118+
val catalog = catalogs.named(name)
119+
catalog.versionAliases.associateWith { catalog.findVersion(it).get().toString() }
120+
}
121+
109122
/**
110123
* Print all the catalog version strings and it's values.
111124
*

gradle/build-logic/common-plugins/src/main/kotlin/common/Repo.kt

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@ object Repo {
4444
* @param artifactId the artifact ID of the Maven artifact
4545
* @return the latest download URL for the specified Maven artifact
4646
*/
47-
fun latestDownloadUrl(groupId: String, artifactId: String): String {
48-
return "https://search.maven.org/remote_content?g=${groupId}&a=${artifactId}&v=LATEST"
49-
}
47+
fun latestDownloadUrl(groupId: String, artifactId: String) =
48+
"https://search.maven.org/remote_content?g=${groupId}&a=${artifactId}&v=LATEST"
5049
}

gradle/build-logic/common-plugins/src/main/kotlin/plugins/misc.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ spotless {
4848
}
4949

5050
format("misc") {
51-
target("**/*.md", "**/.gitignore")
51+
target("**/*.md", "**/.gitignore", "**/.kte")
5252
trimTrailingWhitespace()
5353
indentWithSpaces(2)
5454
endWithNewline()

gradle/build-logic/common-plugins/src/main/kotlin/tasks/BuildConfig.kt

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package tasks
22

3+
import com.javiersc.semver.project.gradle.plugin.SemverExtension
34
import gg.jte.ContentType
45
import gg.jte.TemplateEngine
56
import gg.jte.output.StringOutput
@@ -8,9 +9,7 @@ import org.gradle.api.DefaultTask
89
import org.gradle.api.Project
910
import org.gradle.api.file.DirectoryProperty
1011
import org.gradle.api.tasks.*
11-
import org.gradle.kotlin.dsl.listProperty
12-
import org.gradle.kotlin.dsl.mapProperty
13-
import org.gradle.kotlin.dsl.property
12+
import org.gradle.kotlin.dsl.*
1413
import org.gradle.language.base.plugins.LifecycleBasePlugin
1514

1615
@CacheableTask
@@ -38,14 +37,27 @@ abstract class BuildConfig @Inject constructor(private val extension: BuildConfi
3837
val file = dir.resolve("$className.kt")
3938
logger.quiet("Generated build config file: ${file.absolutePath}")
4039

40+
// Get git commit info
41+
val gitCommit = run {
42+
val commit = project.the<SemverExtension>().commits.get().first()
43+
mapOf(
44+
"gitHash" to commit.hash,
45+
"gitMessage" to commit.message,
46+
"gitTimestampEpochSecond" to commit.timestampEpochSecond.toString(),
47+
"gitTags" to commit.tags.joinToString(),
48+
)
49+
}
50+
4151
// the<VersionCatalogsExtension>().named("libs").
4252
val params =
4353
mapOf(
4454
"className" to className,
4555
"pkg" to pkg,
4656
"version" to extension.version.get(),
57+
"gitCommit" to gitCommit,
4758
"catalogVersions" to extension.catalogVersions.get(),
48-
"dependencies" to extension.dependencies.get())
59+
"dependencies" to extension.dependencies.get(),
60+
)
4961

5062
val content = StringOutput()
5163
val tmplEngine =

gradle/build-logic/common-plugins/src/main/resources/BuildConfig.kte

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,20 @@
77
@param version: String
88
@param catalogVersions: Map<String, String>
99
@param dependencies: List<String>
10+
@param gitCommit: Map<String, String>
1011

1112
/* GENERATED, DO NOT EDIT MANUALLY! */
1213
@if(pkg.isNotBlank())package ${pkg}@endif
1314
object ${className} {
1415

15-
const val time = "${LocalDateTime.now().toString()}"
16+
const val buildTime = "${LocalDateTime.now().toString()}"
1617

1718
const val version = "${version}"
1819

20+
@for((k,v) in gitCommit)
21+
const val ${k} = "${v}"
22+
@endfor
23+
1924
@for((k,v) in catalogVersions)
2025
const val ${k.camelCase} = "${v}"
2126
@endfor

gradle/libs.versions.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ dokka = "1.8.20"
4646
intellij-markdown = "0.4.1"
4747
jgit = "6.5.0.202303070854-r"
4848
jte = "3.0.1"
49-
junit = "5.10.0-RC2"
49+
junit = "5.10.0"
5050
koin = "3.4.1"
5151
kotest = "5.6.2"
5252
mockk = "1.13.5"

0 commit comments

Comments
 (0)