-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.gradle.kts
More file actions
90 lines (72 loc) · 2.49 KB
/
Copy pathbuild.gradle.kts
File metadata and controls
90 lines (72 loc) · 2.49 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
85
86
87
88
89
90
import java.io.ByteArrayOutputStream
plugins {
alias(libs.plugins.kotlinJvm) apply false
alias(libs.plugins.shadow) apply false
alias(libs.plugins.paperweight) apply false
alias(libs.plugins.updateDeps) apply true
alias(libs.plugins.dokka) apply false
alias(libs.plugins.dokkaJavadoc) apply false
}
allprojects {
group = "cc.worldmandia"
version = libraryVersion
}
subprojects {
apply(plugin = "org.jetbrains.dokka")
apply(plugin = "org.jetbrains.dokka-javadoc")
}
abstract class GitValueSource : ValueSource<String, GitValueSource.Params> {
interface Params : ValueSourceParameters {
val commands: ListProperty<String>
}
@get:Inject
abstract val execOperations: ExecOperations
override fun obtain(): String? {
val output = ByteArrayOutputStream()
val error = ByteArrayOutputStream()
val commandsList = parameters.commands.get()
val result = execOperations.exec {
commandLine("git")
args(commandsList)
standardOutput = output
errorOutput = error
isIgnoreExitValue = true
}
if (result.exitValue != 0) {
return null
}
return output.toString().trim()
}
}
private fun Project.gitProvider(vararg command: String): Provider<String> {
return providers.of(GitValueSource::class) {
parameters.commands.set(command.toList())
}
}
private val Project.tagProvider: Provider<List<String>>
get() = gitProvider("tag", "--no-column", "--points-at", "HEAD")
.map { output ->
output.takeIf { it.isNotBlank() }?.lines() ?: emptyList()
}
val Project.commitHash: String
get() = gitProvider("rev-parse", "--verify", "HEAD").getOrElse("")
val Project.shortCommitHash: String
get() = gitProvider("rev-parse", "--short", "HEAD").getOrElse("")
val Project.isRelease: Boolean
get() = providers.gradleProperty("isRelease")
.map { it.toBoolean() }
.getOrElse(false)
val Project.libraryVersion: String
get() {
val tags = tagProvider.getOrElse(emptyList())
if (tags.isNotEmpty()) {
return tags.first()
}
val branchName = gitProvider("branch", "--show-current").getOrElse("unknown")
val nextPlannedVersion = providers.gradleProperty("nextPlannedApiVersion").getOrElse("0.0.1")
val snapshotPrefix = when (branchName) {
"master", "main" -> nextPlannedVersion
else -> branchName.replace('/', '-')
}
return if (isRelease) snapshotPrefix else "$snapshotPrefix-SNAPSHOT"
}