For release builds use last commit time as build time (mihonapp/mihon#1873)

(cherry picked from commit dae7d179662ff6d6654e7c10e57f1aeeaf579de8)
This commit is contained in:
AntsyLich 2025-03-19 05:16:03 +06:00 committed by Cuong-Tran
parent a43b15cc1a
commit d07d353140
No known key found for this signature in database
GPG key ID: 733AA7624B9315C2
2 changed files with 10 additions and 3 deletions

View file

@ -48,6 +48,8 @@ android {
isShrinkResources = Config.enableCodeShrink
proguardFiles("proguard-android-optimize.txt", "proguard-rules.pro")
buildConfigField("String", "BUILD_TIME", "\"${getBuildTime(useLastCommitTime = true)}\"")
}
val commonMatchingFallbacks = listOf(release.name)

View file

@ -1,6 +1,7 @@
package mihon.buildlogic
import org.gradle.api.Project
import java.time.Instant
import java.time.LocalDateTime
import java.time.ZoneOffset
import java.time.format.DateTimeFormatter
@ -19,9 +20,13 @@ fun Project.getGitSha(): String {
private val BUILD_TIME_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss'Z'")
@Suppress("UnusedReceiverParameter")
fun Project.getBuildTime(): String {
return LocalDateTime.now(ZoneOffset.UTC).format(BUILD_TIME_FORMATTER)
fun Project.getBuildTime(useLastCommitTime: Boolean = false): String {
return if (useLastCommitTime) {
val epoch = runCommand("git log -1 --format=%ct").toLong()
Instant.ofEpochSecond(epoch).atOffset(ZoneOffset.UTC).format(BUILD_TIME_FORMATTER)
} else {
LocalDateTime.now(ZoneOffset.UTC).format(BUILD_TIME_FORMATTER)
}
}
private fun Project.runCommand(command: String): String {