refactor: Don't use buildSrc as much as possible

I personally hate buildSrc ngl
This commit is contained in:
Ahmad Ansori Palembani 2024-06-08 06:46:07 +07:00
parent c451d4010e
commit 05e799a925
Signed by: null2264
GPG key ID: BA64F8B60AF3EFB6
2 changed files with 16 additions and 38 deletions

View file

@ -1,4 +1,7 @@
import java.io.ByteArrayOutputStream import java.io.ByteArrayOutputStream
import java.time.LocalDateTime
import java.time.ZoneOffset
import java.time.format.DateTimeFormatter
plugins { plugins {
id("com.android.application") id("com.android.application")
@ -26,6 +29,13 @@ fun runCommand(command: String): String {
return String(byteOut.toByteArray()).trim() return String(byteOut.toByteArray()).trim()
} }
val commitCount by lazy { runCommand("git rev-list --count HEAD") }
val commitHash by lazy { runCommand("git rev-parse --short HEAD") }
val buildTime: String by lazy {
val df = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss'Z'")
LocalDateTime.now(ZoneOffset.UTC).format(df)
}
val supportedAbis = setOf("armeabi-v7a", "arm64-v8a", "x86", "x86_64") val supportedAbis = setOf("armeabi-v7a", "arm64-v8a", "x86", "x86_64")
android { android {
@ -41,9 +51,9 @@ android {
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner" testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
multiDexEnabled = true multiDexEnabled = true
buildConfigField("String", "COMMIT_COUNT", "\"${getCommitCount()}\"") buildConfigField("String", "COMMIT_COUNT", "\"${commitCount}\"")
buildConfigField("String", "COMMIT_SHA", "\"${getGitSha()}\"") buildConfigField("String", "COMMIT_SHA", "\"${commitHash}\"")
buildConfigField("String", "BUILD_TIME", "\"${getBuildTime()}\"") buildConfigField("String", "BUILD_TIME", "\"${buildTime}\"")
buildConfigField("Boolean", "INCLUDE_UPDATER", "false") buildConfigField("Boolean", "INCLUDE_UPDATER", "false")
buildConfigField("Boolean", "BETA", "false") buildConfigField("Boolean", "BETA", "false")
buildConfigField("Boolean", "NIGHTLY", "false") buildConfigField("Boolean", "NIGHTLY", "false")
@ -74,7 +84,7 @@ android {
buildTypes { buildTypes {
getByName("debug") { getByName("debug") {
applicationIdSuffix = ".debugYokai" applicationIdSuffix = ".debugYokai"
versionNameSuffix = "-d${getCommitCount()}" versionNameSuffix = "-d${commitCount}"
} }
getByName("release") { getByName("release") {
applicationIdSuffix = ".yokai" applicationIdSuffix = ".yokai"
@ -87,7 +97,7 @@ android {
buildConfigField("boolean", "BETA", "true") buildConfigField("boolean", "BETA", "true")
matchingFallbacks.add("release") matchingFallbacks.add("release")
versionNameSuffix = "-b${getCommitCount()}" versionNameSuffix = "-b${commitCount}"
} }
create("nightly") { create("nightly") {
initWith(getByName("release")) initWith(getByName("release"))
@ -96,7 +106,7 @@ android {
signingConfig = signingConfigs.getByName("debug") signingConfig = signingConfigs.getByName("debug")
matchingFallbacks.add("release") matchingFallbacks.add("release")
versionNameSuffix = "-b${getCommitCount()}" versionNameSuffix = "-b${commitCount}"
applicationIdSuffix = ".nightlyYokai" applicationIdSuffix = ".nightlyYokai"
} }
} }

View file

@ -1,32 +0,0 @@
import org.gradle.api.Project
import java.io.ByteArrayOutputStream
import java.text.SimpleDateFormat
import java.util.TimeZone
import java.util.Date
// Git is needed in your system PATH for these commands to work.
// If it's not installed, you can return a random value as a workaround
fun Project.getCommitCount(): String {
return runCommand("git rev-list --count HEAD")
// return "1"
}
fun Project.getGitSha(): String {
return runCommand("git rev-parse --short HEAD")
// return "1"
}
fun Project.getBuildTime(): String {
val df = SimpleDateFormat("yyyy-MM-dd'T'HH:mm'Z'")
df.timeZone = TimeZone.getTimeZone("UTC")
return df.format(Date())
}
fun Project.runCommand(command: String): String {
val byteOut = ByteArrayOutputStream()
project.exec {
commandLine = command.split(" ")
standardOutput = byteOut
}
return String(byteOut.toByteArray()).trim()
}