-
Notifications
You must be signed in to change notification settings - Fork 6
/
build.gradle.kts
80 lines (71 loc) · 2.42 KB
/
build.gradle.kts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import java.io.FileWriter
plugins {
id("com.android.application") version "8.1.3" apply false
// must correspond to compose compiler extension version:
// https://developer.android.com/jetpack/androidx/releases/compose-kotlin#pre-release_kotlin_compatibility
id("org.jetbrains.kotlin.android") version "1.9.10" apply false
}
allprojects {
repositories {
google()
mavenCentral()
@Suppress("DEPRECATION")
//noinspection JcenterRepositoryObsolete
jcenter {
content {
includeModule("com.oasisfeng.nevo", "sdk")
}
}
}
}
tasks.register("appVersion") {
description = "Calculate app version and save to version.properties"
// execute on configuration phase
val name = getVersionName()
val code = 20000 + getVersionCode()
logger.lifecycle("AppVersionName: {}\nAppVersionCode: {}", name, code)
FileWriter(File(rootProject.projectDir, "version.properties")).use { fw ->
fw.write("name=$name\ncode=$code\n")
fw.flush()
}
}
tasks.register("clean", Delete::class) {
delete(rootProject.buildDir)
}
fun String.runCommand(currentWorkingDir: File = file("./")): String {
val byteOut = java.io.ByteArrayOutputStream()
project.exec {
workingDir = currentWorkingDir
commandLine = this@runCommand.split(" ")
standardOutput = byteOut
}
return String(byteOut.toByteArray()).trim()
}
fun getVersionCode(): Int {
val cmd = "git rev-list HEAD --count"
return try {
cmd.runCommand().toInt()
} catch (e: Exception) {
logger.error("Failed to get version code with git, return 1 by default.", e)
1
}
}
fun getVersionName(): String {
val cmd = "git describe --tags --long --abbrev=7 --dirty=_dev"
try {
val v = cmd.runCommand()
val pattern = """^v(?<v>[\d|.]+)-\d+-g[A-Za-z0-9]{7}(?<s>_dev)?$""".toRegex()
val g = pattern.matchEntire(v)?.groups
if (g == null || g["v"] == null) {
logger.error(
"Failed to get version name with git.\n" +
"Cannot match git tag describe, return <UNKNOWN> by default. raw=$v"
)
return "UNKNOWN"
}
return g["v"]!!.value + (g["s"]?.value ?: "")
} catch (e: Exception) {
logger.error("Failed to get version name with git, return <UNKNOWN> by default.", e)
return "UNKNOWN"
}
}