forked from serpro69/semver.kt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.gradle.kts
324 lines (281 loc) · 10.6 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
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
import io.github.serpro69.semverkt.gradle.plugin.tasks.TagTask
import org.gradle.api.tasks.testing.TestResult.ResultType
import org.gradle.api.tasks.testing.logging.TestExceptionFormat
import org.gradle.api.tasks.testing.logging.TestLogEvent
import org.jetbrains.dokka.gradle.DokkaTask
import org.jetbrains.kotlin.gradle.dsl.KotlinVersion
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
import java.util.*
import org.jetbrains.kotlin.config.KotlinCompilerVersion.VERSION as KOTLIN_VERSION
plugins {
java
kotlin("jvm") version "1.9.21" apply false
id("org.jetbrains.dokka") version "1.9.10"
`maven-publish`
signing
id("io.github.gradle-nexus.publish-plugin") version "2.0.0-rc-1"
id("io.github.serpro69.semantic-versioning") apply false
}
repositories {
mavenCentral()
}
group = "io.github.serpro69"
allprojects {
configurations.matching { it.name != "detekt" }.all {
resolutionStrategy.eachDependency {
if (requested.group == "org.jetbrains.kotlin" && requested.name.startsWith("kotlin")) {
useVersion(KOTLIN_VERSION)
because("All Kotlin modules should use the same version, and compiler uses $KOTLIN_VERSION")
}
}
}
}
subprojects {
group = parent?.group?.toString() ?: "io.github.serpro69"
val subProject = this@subprojects
val projectArtifactId = "${rootProject.name}-${subProject.name}"
val isGradlePlugin = subProject.name == "semantic-versioning"
val isSnapshot by lazy {
provider {
subProject.version.toString().startsWith("0.0.0")
|| subProject.version.toString().endsWith("SNAPSHOT")
}
}
val newTag by lazy { provider { subProject.tasks.getByName("tag").didWork } }
repositories {
mavenCentral()
}
apply {
plugin("java")
plugin("org.jetbrains.kotlin.jvm")
plugin("org.jetbrains.dokka")
if (!isGradlePlugin) plugin("maven-publish")
plugin("signing")
}
dependencies {
implementation(kotlin("stdlib-jdk8"))
testImplementation("io.kotest:kotest-runner-junit5-jvm:5.7.2")
testImplementation("io.kotest:kotest-assertions-core-jvm:5.7.2")
testImplementation("io.github.serpro69:kotlin-faker:1.15.0")
if (subProject.name in listOf("release", "semantic-versioning")) {
// val jgitVer = "6.8.0.202311291450-r"
val jgitVer = "5.13.2.202306221912-r"
implementation("org.eclipse.jgit:org.eclipse.jgit:$jgitVer")
implementation("org.eclipse.jgit:org.eclipse.jgit.ssh.jsch:$jgitVer")
}
}
tasks.withType<JavaCompile> {
options.encoding = "UTF-8"
}
java {
toolchain {
languageVersion.set(JavaLanguageVersion.of(8))
}
}
tasks.withType<KotlinCompile> {
compilerOptions {
// support gradle 7.5+
languageVersion.set(KotlinVersion.KOTLIN_1_6)
}
}
tasks.withType<Test> {
useJUnitPlatform {}
testLogging {
// set options for log level LIFECYCLE
events = setOf(
TestLogEvent.FAILED,
TestLogEvent.SKIPPED,
// TestLogEvent.STANDARD_OUT
)
exceptionFormat = TestExceptionFormat.FULL
showExceptions = true
showCauses = true
showStackTraces = true
displayGranularity = 2 // shorter test names
// set options for log level DEBUG and INFO
debug {
events = setOf(
TestLogEvent.STARTED,
TestLogEvent.FAILED,
TestLogEvent.PASSED,
TestLogEvent.SKIPPED,
TestLogEvent.STANDARD_ERROR,
TestLogEvent.STANDARD_OUT
)
exceptionFormat = TestExceptionFormat.FULL
}
info.events = debug.events
info.exceptionFormat = debug.exceptionFormat
afterSuite(KotlinClosure2({ desc: TestDescriptor, result: TestResult ->
if (desc.parent == null) { // will match the outermost suite
val pass = "${Color.GREEN}${result.successfulTestCount} passed${Color.NONE}"
val fail = "${Color.RED}${result.failedTestCount} failed${Color.NONE}"
val skip = "${Color.YELLOW}${result.skippedTestCount} skipped${Color.NONE}"
val type = when (val r: ResultType = result.resultType) {
ResultType.SUCCESS -> "${Color.GREEN}$r${Color.NONE}"
ResultType.FAILURE -> "${Color.RED}$r${Color.NONE}"
ResultType.SKIPPED -> "${Color.YELLOW}$r${Color.NONE}"
}
val output = "Results: $type (${result.testCount} tests, $pass, $fail, $skip)"
val startItem = "| "
val endItem = " |"
val repeatLength = startItem.length + output.length + endItem.length - 36
println("")
println("\n" + ("-" * repeatLength) + "\n" + startItem + output + endItem + "\n" + ("-" * repeatLength))
}
}))
}
}
val jar by tasks.getting(Jar::class) {
archiveBaseName.set(projectArtifactId)
afterEvaluate { // allow declaring additional dependencies in subproject's build.gradle file
manifest {
attributes(
mapOf(
"Implementation-Title" to projectArtifactId,
"Implementation-Version" to subProject.version,
"Class-Path" to configurations.compileClasspath.get().joinToString(" ") { it.name }
)
)
}
}
}
val sourcesJar by tasks.creating(Jar::class) {
archiveBaseName.set(projectArtifactId)
archiveClassifier.set("sources")
from(sourceSets.getByName("main").allSource)
from("LICENCE.md") {
into("META-INF")
}
}
val dokkaJavadocJar by tasks.creating(Jar::class) {
archiveBaseName.set(projectArtifactId)
archiveClassifier.set("javadoc")
dependsOn(tasks.dokkaJavadoc)
from(tasks.dokkaJavadoc.get().outputDirectory.orNull)
}
artifacts {
archives(sourcesJar)
archives(dokkaJavadocJar)
}
val artifactGroup = subProject.group.toString()
val artifactVersion = subProject.version.toString()
val releaseTagName = "v$artifactVersion"
val pomUrl = "https://github.com/serpro69/${rootProject.name}"
val pomScmUrl = "https://github.com/serpro69/${rootProject.name}"
val pomIssueUrl = "https://github.com/serpro69/${rootProject.name}/issues"
val pomDesc = "https://github.com/serpro69/${rootProject.name}"
val ghRepo = "serpro69/${rootProject.name}"
val ghReadme = "README.md"
val pomLicenseName = "MIT"
val pomLicenseUrl = "https://opensource.org/licenses/mit-license.php"
val pomLicenseDist = "repo"
val pomDeveloperId = "serpro69"
val pomDeveloperName = "Serhii Prodan"
val publicationName = projectArtifactId
.split(Regex("""[\.-]"""))
.joinToString("") {
it.replaceFirstChar { c -> if (c.isLowerCase()) c.titlecase(Locale.getDefault()) else c.toString() }
}
if (!isGradlePlugin) { // com.gradle.plugin-publish applies its own publishing config
publishing {
publications {
create<MavenPublication>(publicationName) {
groupId = artifactGroup
artifactId = projectArtifactId
version = artifactVersion
from(components["java"])
artifact(sourcesJar)
artifact(dokkaJavadocJar)
pom {
packaging = "jar"
name.set(projectArtifactId)
description.set(pomDesc)
url.set(pomUrl)
scm {
url.set(pomScmUrl)
}
issueManagement {
url.set(pomIssueUrl)
}
licenses {
license {
name.set(pomLicenseName)
url.set(pomLicenseUrl)
}
}
developers {
developer {
id.set(pomDeveloperId)
name.set(pomDeveloperName)
}
}
}
}
}
}
signing {
sign(publishing.publications[publicationName])
}
tasks.withType<PublishToMavenRepository>().configureEach {
dependsOn(subProject.tasks.getByName("tag"))
dependsOn(subProject.tasks.withType(Sign::class.java))
onlyIf("Not snapshot") { !isSnapshot.get() }
onlyIf("New tag") { newTag.get() }
}
tasks.withType<PublishToMavenLocal>().configureEach {
onlyIf("In development") { isSnapshot.get() }
}
tasks.withType<Sign>().configureEach {
dependsOn(subProject.tasks.getByName("tag"))
onlyIf("Not snapshot") { !isSnapshot.get() }
onlyIf("New tag") { newTag.get() }
}
}
tasks.withType<TagTask>().configureEach {
onlyIf("Not snapshot") { !isSnapshot.get() }
}
tasks.withType<DokkaTask>().configureEach {
onlyIf("Not snapshot") { !isSnapshot.get() }
}
tasks {
assemble {
dependsOn(jar)
}
}
}
tasks.withType<TagTask>().configureEach {
val isSnapshot = provider {
version.toString().startsWith("0.0.0")
|| version.toString().endsWith("SNAPSHOT")
}
onlyIf("Not snapshot") { !isSnapshot.get() }
}
val jar by tasks.getting(Jar::class) {
enabled = false // nothing to build in root project
}
nexusPublishing {
sonatype {
stagingProfileId.set(properties["stagingProfileId"]?.toString())
}
}
}
operator fun String.times(x: Int): String {
return List(x) { this }.joinToString("")
}
internal enum class Color(ansiCode: Int) {
NONE(0),
BLACK(30),
RED(31),
GREEN(32),
YELLOW(33),
BLUE(34),
PURPLE(35),
CYAN(36),
WHITE(37);
private val ansiString: String = "\u001B[${ansiCode}m"
override fun toString(): String {
return ansiString
}
}