-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathbuild.gradle.kts
More file actions
212 lines (177 loc) · 6.06 KB
/
build.gradle.kts
File metadata and controls
212 lines (177 loc) · 6.06 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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
plugins {
id("pmd")
id("com.diffplug.spotless") version "8.3.0"
id("com.github.ben-manes.versions") version "0.53.0"
}
fun runCommand(vararg args: String): String {
val process = ProcessBuilder(*args).directory(rootDir).redirectErrorStream(true).start()
val output = process.inputStream.bufferedReader().readText()
val exitCode = process.waitFor()
if (exitCode != 0) {
throw GradleException("Command failed with exit code $exitCode: ${args.joinToString(" ")}")
}
return output.trim()
}
val gitHash: String by lazy { runCommand("git", "rev-parse", "--short", "HEAD") }
val gitTag: String? by lazy {
runCatching { runCommand("git", "describe", "--abbrev=0", "--tags") }.getOrNull()
}
val commitCount: Int by lazy {
val range = if (gitTag.isNullOrEmpty()) "HEAD" else "$gitTag..HEAD"
runCommand("git", "rev-list", "--count", range).toInt()
}
val branch: String by lazy {
// First, try GitHub Actions environment variable
val githubBranch = System.getenv("GITHUB_REF_NAME")
if (!githubBranch.isNullOrBlank()) githubBranch
// Fallback to local git command
else {
runCommand("git", "rev-parse", "--abbrev-ref", "HEAD")
}
}
fun parseTag(tag: String): Triple<Int, Int, Int>? {
val regex = Regex("""v?(\d+)\.(\d+)\.(\d+)""")
val match = regex.matchEntire(tag.trim()) ?: return null
val (major, minor, patch) = match.destructured
return Triple(major.toInt(), minor.toInt(), patch.toInt())
}
fun calcVersion(): String {
var (major, minor, patch) = parseTag(gitTag ?: "") ?: Triple(0, 1, 0)
if (branch == "main") {
return "$major.${minor + 1}.$patch-m$commitCount"
}
if (branch.startsWith("release/v")) {
if (commitCount == 0) {
return "$major.$minor.$patch"
} else {
return "$major.$minor.${patch + 1}-rc$commitCount"
}
}
return "$major.${minor + 1}.$patch-a$commitCount-g$gitHash"
}
val calculatedVersion: String by lazy { calcVersion() }
// prints when Gradle evaluates the build
println("DBOS Transact version: $calculatedVersion")
allprojects {
group = "dev.dbos"
version = calculatedVersion
extra["commitCount"] = "$commitCount"
repositories {
mavenCentral()
gradlePluginPortal()
}
}
spotless {
kotlinGradle {
target("*.gradle.kts")
ktfmt("0.61").googleStyle()
trimTrailingWhitespace()
endWithNewline()
}
}
subprojects {
apply(plugin = "java")
apply(plugin = "pmd")
apply(plugin = "com.diffplug.spotless")
apply(plugin = "com.github.ben-manes.versions")
// PMD configuration
extensions.configure<org.gradle.api.plugins.quality.PmdExtension> {
toolVersion = "7.16.0"
ruleSets = listOf() // disable defaults
ruleSetFiles = files("${rootDir}/config/pmd/ruleset.xml")
isConsoleOutput = true
}
// Spotless configuration
extensions.configure<com.diffplug.gradle.spotless.SpotlessExtension> {
java {
googleJavaFormat()
importOrder("dev.dbos", "java", "javax", "")
removeUnusedImports()
trimTrailingWhitespace()
endWithNewline()
}
kotlin {
target("**/*.kt")
targetExclude("build/**/*.kt")
ktfmt("0.61").googleStyle()
trimTrailingWhitespace()
endWithNewline()
}
kotlinGradle {
target("**/*.gradle.kts")
ktfmt("0.61").googleStyle()
trimTrailingWhitespace()
endWithNewline()
}
}
tasks.withType<com.github.benmanes.gradle.versions.updates.DependencyUpdatesTask> {
rejectVersionIf {
val isUnstable =
listOf("alpha", "beta", "rc", "cr", "m", "preview", "b", "ea").any { qualifier ->
candidate.version.lowercase().contains(qualifier)
}
val isStable =
listOf("release", "final", "ga").any { qualifier ->
currentVersion.lowercase().contains(qualifier)
}
isUnstable && !isStable
}
}
plugins.withId("java") {
// Force the published bytecode to be Java 17
extensions.getByType<JavaPluginExtension>().apply {
sourceCompatibility = JavaVersion.VERSION_17
targetCompatibility = JavaVersion.VERSION_17
}
// Allow the compiler to see higher-version APIs for reflection but keep the bytecode target at
// 17.
tasks.withType<JavaCompile> { options.release.set(17) }
// use the environment's JDK instead of the toolchain's JDK for tests
tasks.withType<Test> { javaLauncher.set(null as JavaLauncher?) }
tasks.withType<Test> {
useJUnitPlatform()
testLogging {
events("failed")
showStandardStreams = true
showExceptions = true
showCauses = true
showStackTraces = true
exceptionFormat = org.gradle.api.tasks.testing.logging.TestExceptionFormat.FULL
}
addTestListener(
object : TestListener {
private val failedTests = mutableListOf<String>()
override fun beforeSuite(suite: TestDescriptor) {}
override fun beforeTest(testDescriptor: TestDescriptor) {}
override fun afterTest(testDescriptor: TestDescriptor, result: TestResult) {
if (result.resultType == TestResult.ResultType.FAILURE) {
failedTests.add("${testDescriptor.className}.${testDescriptor.name}")
}
}
override fun afterSuite(suite: TestDescriptor, result: TestResult) {
if (suite.parent == null) {
println("\nTest Results:")
println(" Tests run: ${result.testCount}")
println(" Passed: ${result.successfulTestCount}")
println(" Failed: ${result.failedTestCount}")
println(" Skipped: ${result.skippedTestCount}")
if (failedTests.isNotEmpty()) {
println("\nFailed Tests:")
failedTests.forEach { println(" - $it") }
}
}
}
}
)
}
tasks.named<Jar>("jar") {
manifest {
attributes["Implementation-Version"] = project.version
attributes["Implementation-Title"] = project.name
attributes["Implementation-Vendor"] = "DBOS, Inc"
attributes["Implementation-Vendor-Id"] = project.group
attributes["SCM-Revision"] = gitHash
}
}
}
}