-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.gradle.kts
More file actions
84 lines (66 loc) · 2.74 KB
/
Copy pathbuild.gradle.kts
File metadata and controls
84 lines (66 loc) · 2.74 KB
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
81
82
83
84
// Top-level build file where you can add configuration options common to all sub-projects/modules.
import de.bixilon.kutil.exception.ExceptionUtil.ignoreAll
import de.bixilon.kutil.primitive.BooleanUtil.toBoolean
import de.bixilon.kutil.stream.InputStreamUtil.readAsString
import de.bixilon.kutil.string.WhitespaceUtil.removeMultipleWhitespaces
import de.bixilon.kutil.string.WhitespaceUtil.trimWhitespaces
plugins {
alias(libs.plugins.android.application) apply false
alias(libs.plugins.kotlin.compose) apply false
alias(libs.plugins.android.test) apply false
alias(libs.plugins.baselineprofile) apply false
alias(libs.plugins.kotlin.multiplatform) apply false
alias(libs.plugins.android.multiplatform.library) apply false
alias(libs.plugins.compose.multiplatform) apply false
alias(libs.plugins.kotlin.jvm) apply false
alias(libs.plugins.kotlin.serialization) apply false
}
buildscript {
dependencies {
classpath(libs.kutil)
}
}
fun getEnv(name: String): String? = System.getenv(name)?.takeIf { it.isNotBlank() }
data class GitStatus(
val commit: String,
val branch: String,
val clean: Boolean,
val tag: String?,
)
fun loadGitFromEnv(): GitStatus? {
val commit = getEnv("CI_COMMIT_SHA") ?: return null
val branch = getEnv("CI_COMMIT_BRANCH") ?: return null
val tag = getEnv("CI_COMMIT_TAG")
return GitStatus(commit, branch, true, tag)
}
val FDROID = project.properties["fdroid"]?.toBoolean() ?: false
val hasGit by lazy { project.rootDir.resolve(".git").exists() }
fun executeGit(vararg args: String): String? {
if (!hasGit) return null
val process = ProcessBuilder()
.command(*(arrayOf("git") + args))
.directory(project.rootDir)
.redirectOutput(ProcessBuilder.Redirect.PIPE)
.start()
if (process.waitFor() != 0) return null
return process.inputStream.readAsString()
.trimWhitespaces()
.trim { it == '\n' || it == '\r' }
.removeMultipleWhitespaces()
.takeIf { it.isNotBlank() }
}
fun loadGitFromGit(): GitStatus? {
val commit = executeGit("rev-parse", "HEAD") ?: return null
val branch = executeGit("branch", "--show-current") ?: "master"
val clean = if (FDROID) true else executeGit("status", "--porcelain") == null
val tag = executeGit("describe", "--exact-match", "--tags")
return GitStatus(commit, branch, clean, tag)
}
val git by lazy { ignoreAll { loadGitFromGit() } ?: loadGitFromEnv() }
allprojects {
var version = (git?.tag?.removePrefix("v") ?: git?.commit?.substring(0, 10) ?: "unknown")
git?.takeIf { !it.clean }?.let { version += "-dirty" }
project.extra.set("version", version)
project.extra.set("versionCode", 6)
project.extra.set("commit", git?.commit ?: "unknown")
}