-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.gradle.kts
163 lines (140 loc) · 4.52 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
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
import org.ajoberstar.grgit.Grgit
import org.gradle.api.tasks.testing.logging.TestExceptionFormat
plugins {
id("java")
alias(libs.plugins.grgit)
}
tasks.withType<JavaCompile>().configureEach {
options.apply {
encoding = "UTF-8"
release.set(17) // don't forget to update README.md
}
}
repositories {
mavenCentral()
}
dependencies {
// when adding new dependencies, please update task processResources
implementation(libs.directories) // https://github.com/dirs-dev/directories-jvm
implementation(libs.gson)
testImplementation(platform(libs.junitBom))
testImplementation(libs.junitJupiter)
testRuntimeOnly(libs.junitPlatformLauncher)
}
tasks.named<Test>("test") {
useJUnitPlatform()
testLogging { // add stdout logging for running `./gradlew test`
lifecycle {
events("passed", "skipped", "failed")
exceptionFormat = TestExceptionFormat.FULL
}
}
afterSuite(KotlinClosure2<TestDescriptor, TestResult, Unit>({ description, result ->
if (description.parent == null) {
val stats: String = "${result.testCount} tests run, ${result.successfulTestCount} successes, " +
"${result.failedTestCount} failures, ${result.skippedTestCount} ignored"
println("-".repeat(stats.length))
println("Testing result for ${project.name}: ${result.resultType}")
println(stats)
println("-".repeat(stats.length))
}
}))
reports {
junitXml.required.set(true)
html.required.set(true) // see ./build/reports/tests/test/index.html
}
}
fun Process.waitForOrKill(millis: Long) {
if (!this.waitFor(millis, TimeUnit.MILLISECONDS)) {
this.destroy()
}
}
/**
* Generate a version string with a lot of information.
* Logic is based on a Shell script used for generating version string of Git:
* https://git.kernel.org/pub/scm/git/git.git/tree/GIT-VERSION-GEN
* TODO maybe generate Version.java from Gradle?
*/
fun calculateVersion(): String {
val defaultVersion: String = "1.8-nongit"
try {
val git: Grgit = Grgit.open(mapOf("dir" to project.rootDir))
/*
* If possible, use an annotated tag which starts with letter 'v' and some numbers.
*/
val description: String? = git.describe(mapOf("match" to listOf("v[0-9]*"), "commit" to "HEAD"))
if (description != null && description.matches(Regex("^v[0-9]+[^ ]*"))) {
val updateIndex: Process = ProcessBuilder("git", "update-index", "-q", "--refresh").start()
updateIndex.waitForOrKill(10000)
val diffIndex: Process = ProcessBuilder("git", "diff-index", "--name-only", "HEAD", "--", ".").start()
val outputIsEmpty: Boolean = diffIndex.inputReader().read() == -1
val version = description.substring(1) // cut off initial 'v'
if (!outputIsEmpty) {
return "$version-dirty"
}
return version
}
} catch (e: Exception) {
logger.warn("Could not use Git.", e)
}
return defaultVersion
}
/**
* Calculate who has built the artifacts.
*/
fun calculateResodayBuilderName(): String {
try {
val gitUserName: Process = ProcessBuilder("git", "config", "user.name").start()
gitUserName.waitForOrKill(10000)
return gitUserName.inputReader().readLine()
} catch (ignored: Exception) {
return System.getProperty("user.name")
}
}
project.version = calculateVersion()
tasks.named<Copy>("processResources") {
filesMatching("**/about.html") {
expand(mapOf("version" to project.version))
}
filesMatching("**/third-party-software.html") {
expand(mapOf(
"directoriesVersion" to libs.directories.get().version,
"gsonVersion" to libs.gson.get().version,
"gradleVersion" to rootProject.gradle.gradleVersion,
"junitVersion" to libs.junitBom.get().version,
"grgitVersion" to libs.plugins.grgit.get().version,
))
}
}
val resodayJarAttributes: Map<String, Any> = mapOf(
"Implementation-Title" to "Resoday built by " + calculateResodayBuilderName(),
"Implementation-Version" to project.version,
"Main-Class" to "dev.andrybak.resoday.Resoday"
)
val jarTask: TaskProvider<Jar> = tasks.named<Jar>("jar") {
manifest {
attributes(resodayJarAttributes)
}
}
tasks.register<Jar>("releaseJar") {
group = "release"
description = "Create a release jar of Resoday"
archiveBaseName.set("resoday")
archiveAppendix.set("release")
destinationDirectory.set(file("build/distributions/"))
manifest {
attributes(resodayJarAttributes)
}
with(jarTask.get() as CopySpec)
from(configurations.runtimeClasspath.get().map {
if (it.isDirectory()) it else zipTree(it)
})
}
tasks.register("release") {
group = "release"
description = "Run all tests and create a release jar"
dependsOn("build", "releaseJar")
}
tasks.named<Wrapper>("wrapper") {
distributionType = Wrapper.DistributionType.ALL
}