-
Notifications
You must be signed in to change notification settings - Fork 46
/
build.gradle.kts
450 lines (411 loc) · 16.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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
/*
* Copyright (C) 2010-2023, Danilo Pianini and contributors
* listed, for each module, in the respective subproject's build.gradle.kts file.
*
* This file is part of Alchemist, and is distributed under the terms of the
* GNU General Public License, with a linking exception,
* as described in the file LICENSE in the Alchemist distribution's top directory.
*/
import Libs.alchemist
import Libs.incarnation
import Util.allVerificationTasks
import Util.currentCommitHash
import Util.fetchJavadocIOForDependency
import Util.id
import Util.isInCI
import Util.isMac
import Util.isMultiplatform
import Util.isWindows
import com.github.spotbugs.snom.SpotBugsTask
import org.danilopianini.gradle.mavencentral.JavadocJar
import org.gradle.api.tasks.testing.logging.TestExceptionFormat
import org.jetbrains.dokka.gradle.AbstractDokkaLeafTask
import org.jetbrains.dokka.gradle.AbstractDokkaParentTask
import org.jetbrains.dokka.gradle.AbstractDokkaTask
import org.jetbrains.dokka.gradle.DokkaCollectorTask
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
import java.time.Duration
plugins {
distribution
alias(libs.plugins.dokka)
alias(libs.plugins.gitSemVer)
alias(libs.plugins.java.qa)
alias(libs.plugins.kotlin.multiplatform) apply false
alias(libs.plugins.kotlin.jvm)
alias(libs.plugins.kotlin.qa)
alias(libs.plugins.multiJvmTesting)
alias(libs.plugins.publishOnCentral)
alias(libs.plugins.taskTree)
alias(libs.plugins.hugo)
}
val minJavaVersion: String by properties
allprojects {
with(rootProject.libs.plugins) {
if (project.isMultiplatform) {
apply(plugin = kotlin.multiplatform.id)
} else {
apply(plugin = kotlin.jvm.id)
}
apply(plugin = dokka.id)
apply(plugin = gitSemVer.id)
apply(plugin = java.qa.id)
apply(plugin = multiJvmTesting.id)
apply(plugin = kotlin.qa.id)
apply(plugin = publishOnCentral.id)
apply(plugin = taskTree.id)
}
apply(plugin = "distribution")
multiJvm {
jvmVersionForCompilation.set(minJavaVersion.toInt())
maximumSupportedJvmVersion.set(latestJava)
if (isInCI && (isWindows || isMac)) {
/*
* Reduce time in CI by running on fewer JVMs on slower or more limited instances.
*/
testByDefaultWith(latestJava)
}
}
repositories {
google()
mavenCentral()
// for tornadofx 2.0.0 snapshot release
maven {
url = uri("https://oss.sonatype.org/content/repositories/snapshots")
content {
includeGroup("no.tornado")
}
}
}
dependencies {
with(rootProject.libs) {
compileOnly(spotbugs.annotations)
implementation(resourceloader)
implementation(slf4j)
implementation(kotlin("stdlib-jdk8"))
implementation(kotlin("reflect"))
testCompileOnly(spotbugs.annotations)
// Test implementation: JUnit 5 + Kotest + Mockito + Mockito-Kt + Alchemist testing tooling
testImplementation(bundles.testing.compile)
testImplementation(alchemist("test"))
// Test runtime: Junit engine
testRuntimeOnly(bundles.testing.runtimeOnly)
// executable jar packaging
}
if ("incarnation" in project.name) {
runtimeOnly(rootProject)
}
}
// JVM PROJECTS CONFIGURATIONS
if (!project.isMultiplatform) {
tasks.withType<AbstractDokkaLeafTask>().configureEach {
timeout.set(Duration.ofMinutes(5))
dokkaSourceSets.configureEach {
jdkVersion.set(multiJvm.jvmVersionForCompilation)
listOf("kotlin", "java")
.flatMap { listOf("main/$it", "commonMain/$it", "jsMain/$it", "jvmMain/$it") }
.map { "src/$it" }
.associateWith { File(projectDir, it) }
.filterValues { it.exists() }
.forEach { (path, file) ->
sourceLink {
localDirectory.set(file)
val project = if (project == rootProject) "" else project.name
val url = "https://github.com/AlchemistSimulator/Alchemist/${
currentCommitHash?.let { "tree/$it" } ?: "blob/master"
}/$project/$path"
remoteUrl.set(uri(url).toURL())
remoteLineSuffix.set("#L")
}
}
configurations.run { sequenceOf(api, implementation) }
.flatMap { it.get().dependencies }
.forEach { dependency ->
val javadocIOURLs = fetchJavadocIOForDependency(dependency)
if (javadocIOURLs != null) {
val (javadoc, packageList) = javadocIOURLs
externalDocumentationLink {
url.set(javadoc.toURL())
packageListUrl.set(packageList.toURL())
}
}
}
}
failOnWarning.set(true)
}
}
// MULTIPLATFORM PROJECTS CONFIGURATIONS
if (project.isMultiplatform) {
tasks.dokkaJavadoc {
enabled = false
}
tasks.withType<JavadocJar>().configureEach {
val dokka = tasks.dokkaHtml.get()
dependsOn(dokka)
from(dokka.outputDirectory)
}
publishing {
publications {
publications.withType<MavenPublication>().configureEach {
if ("OSSRH" !in name) {
artifact(tasks.javadocJar)
}
}
}
}
/*
* This is a workaround for the following Gradle error,
* and should be removed as soon as possible.
*
* * What went wrong:
* Execution failed for task ':dokkaHtmlCollector'.
* > Could not determine the dependencies of null.
* > Current thread does not hold the state lock for project ':alchemist-web-renderer'
*/
val dokkaHtmlCollector by rootProject.tasks.named("dokkaHtmlCollector")
dokkaHtmlCollector.dependsOn(tasks.dokkaHtml)
/*
* On multiplatform projects, publish-on-central does not seem to bind compileCommonKotlin correctly.
*/
tasks.withType<PublishToMavenRepository>().configureEach {
dependsOn(tasks.classes)
}
}
// COMPILE
tasks.withType<JavaCompile>().configureEach {
options.encoding = "UTF-8"
}
tasks.withType<KotlinCompile>().configureEach {
compilerOptions {
freeCompilerArgs.add("-Xjvm-default=all") // Enable default methods in Kt interfaces
// Context receivers temporarily disabled, as they are unsupported in Kotlin script
// "-Xcontext-receivers", // Enable context receivers
}
}
// TEST AND COVERAGE
tasks.withType<Test>().configureEach {
testLogging {
events("passed", "skipped", "failed", "standardError")
exceptionFormat = TestExceptionFormat.FULL
}
useJUnitPlatform()
maxHeapSize = "1g"
}
// CODE QUALITY
javaQA {
checkstyle {
additionalConfiguration.set(
"""
<module name="RegexpSingleline">
<property name="severity" value="error" />
<property name="format" value="Math\s*\.\s*random\s*\(\s*\)" />
<property name="fileExtensions" value="java,xtend,scala,kt" />
<property name="message"
value="Don't use Math.random() inside Alchemist. Breaks stuff." />
</module>
<module name="RegexpSingleline">
<property name="severity" value="error" />
<property name="format" value="class\s*\.\s*forName\s*\(" />
<property name="fileExtensions" value="java,xtend,scala,kt" />
<property name="message"
value="Use the library to load classes and resources. Breaks grid otherwise." />
</module>
<module name="RegexpSingleline">
<property name="severity" value="error" />
<property name="format" value="class\s*\.\s*getResource" />
<property name="fileExtensions" value="java,xtend,scala,kt" />
<property name="message"
value="Use the library to load classes and resources. Breaks grid otherwise." />
</module>
<module name="RegexpSingleline">
<property name="severity" value="error" />
<property name="format" value="class\s*\.\s*getClassLoader" />
<property name="fileExtensions" value="java,xtend,scala,kt" />
<property name="message"
value="Use the library to load classes and resources. Breaks grid otherwise." />
</module>
<module name="RegexpSingleline">
<property name="severity" value="warning" />
<property name="format" value="@author" />
<property name="fileExtensions" value="java,xtend,scala,kt" />
<property name="message"
value="Do not use @author. Changes and authors are tracked by the content manager." />
</module>
""".trimIndent(),
)
additionalSuppressions.set(
"""
<suppress files=".*[\\/]expressions[\\/]parser[\\/].*" checks=".*"/>
<suppress files=".*[\\/]biochemistrydsl[\\/].*" checks=".*"/>
""".trimIndent(),
)
}
}
tasks.allVerificationTasks.configureEach {
exclude { "generated" in it.file.absolutePath }
}
tasks.withType<SpotBugsTask>().configureEach {
reports {
create("html") { enabled = true }
}
}
// PUBLISHING
tasks.withType<Javadoc>().configureEach {
// Disable Javadoc, use Dokka.
enabled = false
}
/*
* Work around:
* Task ':...:dokkaJavadoc' uses this output of task ':...:jar' without declaring an explicit or implicit dependency.
* This can lead to incorrect results being produced, depending on what order the tasks are executed.
*/
tasks.withType<AbstractDokkaTask>().configureEach {
dependsOn(tasks.jar)
}
if (isInCI) {
signing {
val signingKey: String? by project
val signingPassword: String? by project
useInMemoryPgpKeys(signingKey, signingPassword)
}
}
group = "it.unibo.alchemist"
val repoSlug = "AlchemistSimulator/Alchemist"
publishOnCentral {
projectDescription.set(extra["projectDescription"].toString())
projectLongName.set(extra["projectLongName"].toString())
projectUrl.set("https://github.com/$repoSlug")
licenseName.set("GPL 3.0 with linking exception")
licenseUrl.set(projectUrl.map { "$it/blob/master/LICENSE.md" })
scmConnection.set("git:git@github.com:$repoSlug.git")
repository("https://maven.pkg.github.com/${repoSlug.lowercase()}") {
user.set("DanySK")
password.set(System.getenv("GITHUB_TOKEN"))
}
}
publishing.publications.withType<MavenPublication>().configureEach {
pom {
developers {
developer {
name.set("Danilo Pianini")
email.set("danilo.pianini@unibo.it")
url.set("https://www.danilopianini.org")
roles.set(mutableSetOf("architect", "developer"))
}
}
}
}
// Disable distribution tasks that just clutter the build
listOf(tasks.distZip, tasks.distTar).forEach {
it.configure { enabled = false }
}
}
/*
* Root project additional configuration
*/
evaluationDependsOnChildren()
dependencies {
// Depend on subprojects whose presence is necessary to run
listOf("api", "engine", "loading").forEach { api(alchemist(it)) } // Execution requirements
with(libs.apache.commons) {
implementation(io)
implementation(lang3)
}
implementation(libs.kotlin.cli)
implementation(libs.guava)
implementation(libs.logback)
testRuntimeOnly(incarnation("protelis"))
testRuntimeOnly(incarnation("sapere"))
testRuntimeOnly(incarnation("biochemistry"))
testRuntimeOnly(alchemist("cognitive-agents"))
testRuntimeOnly(alchemist("physics"))
}
tasks.named("kotlinStoreYarnLock").configure {
dependsOn("kotlinUpgradeYarnLock")
}
// WEBSITE
val websiteDir = project.layout.buildDirectory.map { it.dir("website").asFile }.get()
hugo {
version = Regex("gohugoio/hugo@v([\\.\\-\\+\\w]+)")
.find(file("deps-utils/action.yml").readText())!!.groups[1]!!.value
}
tasks {
hugoBuild {
outputDirectory = websiteDir
}
// Exclude the UI and Multiplatform packages from the collector documentation.
withType<DokkaCollectorTask>().configureEach {
/*
* Although the method is deprecated, no valid alternative has been implemented yet.
* Disabling individual partial tasks has been proven ineffective.
*/
removeChildTasks(
allprojects.filter { it.isMultiplatform } + listOf(
alchemist("swingui"),
),
)
}
/**
* Use the alchemist logo in the documentation.
*/
val alchemistLogo = file("site/static/images/logo.svg")
for (docTaskProvider in listOf<Provider<out AbstractDokkaParentTask>>(dokkaHtmlCollector, dokkaHtmlMultiModule)) {
val docTask = docTaskProvider.get()
val copyLogo = register<Copy>("copyLogoFor${ docTask.name.replaceFirstChar { it.titlecase() } }") {
from(alchemistLogo)
into(docTask.outputDirectory.map { File(it.asFile, "images") })
rename("logo.svg", "logo-icon.svg")
}
docTask.finalizedBy(copyLogo)
hugoBuild.configure { mustRunAfter(copyLogo) }
}
val performWebsiteStringReplacements by registering {
val index = File(websiteDir, "index.html")
mustRunAfter(hugoBuild)
if (!index.exists()) {
logger.lifecycle("${index.absolutePath} does not exist")
dependsOn(hugoBuild)
}
doLast {
require(index.exists()) {
"file ${index.absolutePath} existed during configuration, but it has been deleted."
}
val websiteReplacements = file("site/replacements").readLines()
.map { it.split("->") }
.map { it[0] to it[1] }
val replacements: List<Pair<String, String>> =
websiteReplacements + ("!development preview!" to project.version.toString())
index.parentFile.walkTopDown()
.filter { it.isFile && it.extension.matches(Regex("html?", RegexOption.IGNORE_CASE)) }
.forEach { page ->
val initialContents = page.readText()
var text = initialContents
for ((toreplace, replacement) in replacements) {
text = text.replace(toreplace, replacement)
}
if (initialContents != text) {
page.writeText(text)
}
}
}
}
mapOf("javadoc" to dokkaJavadocCollector, "kdoc" to dokkaHtmlMultiModule, "plainkdoc" to dokkaHtmlCollector)
.forEach { (folder, taskContainer) ->
hugoBuild.configure { dependsOn(taskContainer) }
val copyTask = register<Copy>("copy${folder.replaceFirstChar { it.titlecase() }}IntoWebsite") {
from(taskContainer.map { it.outputDirectory })
into(File(websiteDir, "reference/$folder"))
finalizedBy(performWebsiteStringReplacements)
}
hugoBuild.configure { finalizedBy(copyTask) }
}
/*
* Work around:
*
* Task ':dokka...Collector' uses this output of task ':<subproject>:jar'
* without declaring an explicit or implicit dependency.
* This can lead to incorrect results being produced, depending on what order the tasks are executed.
*/
withType<AbstractDokkaParentTask>().configureEach {
dependsOn(subprojects.map { it.tasks.jar })
}
}