-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBuildExtensions.kt
More file actions
356 lines (324 loc) · 11.9 KB
/
BuildExtensions.kt
File metadata and controls
356 lines (324 loc) · 11.9 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
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
/*
* Copyright 2025, TeamDev. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Redistribution and use in source and/or binary forms, with or without
* modification, must retain the above copyright notice and the following
* disclaimer.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
@file:Suppress("UnusedReceiverParameter", "unused", "TopLevelPropertyNaming", "ObjectPropertyName")
import io.spine.dependency.build.ErrorProne
import io.spine.dependency.build.GradleDoctor
import io.spine.dependency.build.Ksp
import io.spine.dependency.build.PluginPublishPlugin
import io.spine.dependency.lib.Protobuf
import io.spine.dependency.local.Compiler
import io.spine.dependency.local.CoreJvmCompiler
import io.spine.dependency.local.McJava
import io.spine.dependency.local.ProtoTap
import io.spine.dependency.test.Kotest
import io.spine.dependency.test.Kover
import io.spine.gradle.repo.standardToSpineSdk
import org.gradle.api.Project
import org.gradle.api.Task
import org.gradle.api.file.DuplicatesStrategy
import org.gradle.api.tasks.JavaExec
import org.gradle.jvm.tasks.Jar
import org.gradle.kotlin.dsl.ScriptHandlerScope
import org.gradle.plugin.use.PluginDependenciesSpec
import org.gradle.plugin.use.PluginDependencySpec
/**
* Provides shortcuts to reference our dependency objects.
*
* Dependency objects cannot be used under `plugins` section because `io` is a value
* declared in auto-generated `org.gradle.kotlin.dsl.PluginAccessors.kt` file.
* It conflicts with our own declarations.
*
* In such cases, a shortcut to apply a plugin can be created:
*
* ```
* val PluginDependenciesSpec.`gradle-doctor`: PluginDependencySpec
* get() = id(GradleDoctor.pluginId).version(GradleDoctor.version)
* ```
*
* But for some plugins, it is impossible to apply them directly to a project.
* For example, when a plugin is not published to Gradle Portal, it can only be
* applied with the buildscript's classpath. Thus, it is necessary to leave some freedom
* upon how to apply them. In such cases, just a shortcut to a dependency object
* can be declared without applying the plugin in-place.
*/
private const val ABOUT_DEPENDENCY_EXTENSIONS = ""
/**
* Applies [standard][io.spine.gradle.repo.standardToSpineSdk] repositories to this `buildscript`.
*/
fun ScriptHandlerScope.standardSpineSdkRepositories() {
repositories.standardToSpineSdk()
}
/**
* Shortcut to [Protobuf] dependency object for using under `buildScript`.
*/
val ScriptHandlerScope.protobuf: Protobuf
get() = Protobuf
/**
* Shortcut to [CoreJvmCompiler] dependency object for using under `buildScript`.
*/
val ScriptHandlerScope.coreJvmCompiler: CoreJvmCompiler
get() = CoreJvmCompiler
/**
* Shortcut to [McJava] dependency object for using under `buildScript`.
*/
val ScriptHandlerScope.mcJava: McJava
get() = McJava
/**
* Shortcut to [CoreJvmCompiler] dependency object.
*
* This plugin is not published to Gradle Portal and cannot be applied directly to a project.
* Firstly, it should be put to buildscript's classpath and then applied by ID only.
*/
val PluginDependenciesSpec.coreJvmCompiler: CoreJvmCompiler
get() = CoreJvmCompiler
/**
* Shortcut to [Compiler] dependency object for using under `buildscript`.
*/
val ScriptHandlerScope.spineCompiler: Compiler
get() = Compiler
/**
* Shortcut to [Compiler] dependency object.
*
* This plugin is published at Gradle Plugin Portal.
* But when used in a pair with [mcJava], it cannot be applied directly to a project.
* It is so, because [mcJava] uses [spineCompiler] as its dependency.
* And the buildscript's classpath ends up with both of them.
*/
val PluginDependenciesSpec.spineCompiler: Compiler
get() = Compiler
/**
* Provides shortcuts for applying plugins from our dependency objects.
*
* Dependency objects cannot be used under `plugins` section because `io` is a value
* declared in auto-generated `org.gradle.kotlin.dsl.PluginAccessors.kt` file.
* It conflicts with our own declarations.
*
* Declaring of top-level shortcuts eliminates the need to apply plugins
* using a fully qualified name of dependency objects.
*
* It is still possible to apply a plugin with a custom version, if needed.
* Just declare a version again on the returned [PluginDependencySpec].
*
* For example:
*
* ```
* plugins {
* protobuf version("0.8.19-custom")
* }
* ```
*/
private const val ABOUT_PLUGIN_ACCESSORS = ""
val PluginDependenciesSpec.errorprone: PluginDependencySpec
get() = id(ErrorProne.GradlePlugin.id)
val PluginDependenciesSpec.protobuf: PluginDependencySpec
get() = id(Protobuf.GradlePlugin.id)
val PluginDependenciesSpec.prototap: PluginDependencySpec
get() = id(ProtoTap.gradlePluginId).version(ProtoTap.version)
val PluginDependenciesSpec.`gradle-doctor`: PluginDependencySpec
get() = id(GradleDoctor.pluginId).version(GradleDoctor.version)
val PluginDependenciesSpec.kotest: PluginDependencySpec
get() = Kotest.let {
return id(it.gradlePluginId).version(it.version)
}
val PluginDependenciesSpec.kover: PluginDependencySpec
get() = id(Kover.id).version(Kover.version)
val PluginDependenciesSpec.ksp: PluginDependencySpec
get() = id(Ksp.id).version(Ksp.dogfoodingVersion)
val PluginDependenciesSpec.`plugin-publish`: PluginDependencySpec
get() = id(PluginPublishPlugin.id).version(PluginPublishPlugin.version)
/**
* Configures the dependencies between third-party Gradle tasks
* and those defined via the Spine Compiler and its plugins.
*
* It is required to avoid warnings in build logs, detecting the undeclared
* usage of Spine-specific task output by other tasks,
* e.g., the output of `launchSpineCompiler` is used by `compileKotlin`.
*/
@Suppress("unused")
fun Project.configureTaskDependencies() {
/**
* Creates a dependency between the Gradle task of *this* name
* onto the task with `taskName`.
*
* If either of tasks does not exist in the enclosing `Project`,
* this method does nothing.
*
* This extension is kept local to `configureTaskDependencies` extension
* to prevent its direct usage from outside.
*/
fun String.dependOn(taskName: String) {
val whoDepends = this
val dependOntoTask: Task? = tasks.findByName(taskName)
dependOntoTask?.let {
tasks.findByName(whoDepends)?.dependsOn(it)
}
}
afterEvaluate {
val generateProto = "generateProto"
val createVersionFile = "createVersionFile"
val compileKotlin = "compileKotlin"
compileKotlin.run {
dependOn(generateProto)
}
val compileTestKotlin = "compileTestKotlin"
val sourcesJar = "sourcesJar"
val kspKotlin = "kspKotlin"
sourcesJar.run {
dependOn(generateProto)
dependOn(kspKotlin)
dependOn(createVersionFile)
dependOn("prepareProtocConfigVersions")
}
val dokkaGenerate = "dokkaGenerate"
dokkaGenerate.run {
dependOn(generateProto)
dependOn(kspKotlin)
}
val dokkaGeneratePublicationJavadoc = "dokkaGeneratePublicationJavadoc"
dokkaGeneratePublicationJavadoc.dependOn(kspKotlin)
"publishPluginJar".dependOn(createVersionFile)
compileKotlin.dependOn(kspKotlin)
compileTestKotlin.dependOn("kspTestKotlin")
"compileTestFixturesKotlin".dependOn("kspTestFixturesKotlin")
"javadocJar".dependOn(dokkaGeneratePublicationJavadoc)
"htmlDocsJar".dependOn(dokkaGenerate)
"kspTestKotlin".dependOn("launchTestSpineCompiler")
}
}
/**
* Obtains all modules names of which do not have `"-tests"` as the suffix.
*
* By convention, such modules are for integration tests and should be treated differently.
*/
val Project.productionModules: Iterable<Project>
get() = rootProject.subprojects.filterNot { subproject ->
subproject.name.run {
contains("-tests")
|| contains("test-fixtures")
|| contains("integration-tests")
}
}
/**
* Obtains the names of the [productionModules].
*
* The extension could be useful for excluding modules from standard publishing:
* ```kotlin
* spinePublishing {
* val customModule = "my-custom-module"
* modules = productionModuleNames.toSet().minus(customModule)
* modulesWithCustomPublishing = setOf(customModule)
* //...
* }
* ```
*/
val Project.productionModuleNames: List<String>
get() = productionModules.map { it.name }
/**
* Sets the remote debug option for this [JavaExec] task.
*
* The port number is [5566][BuildSettings.REMOTE_DEBUG_PORT].
*
* @param enabled If `true` the task will be suspended.
*/
fun JavaExec.remoteDebug(enabled: Boolean = true) {
debugOptions {
this@debugOptions.enabled.set(enabled)
port.set(BuildSettings.REMOTE_DEBUG_PORT)
server.set(true)
suspend.set(true)
}
}
/**
* Sets the remote debug option for the task of [JavaExec] type with the given name.
*
* The port number is [5566][BuildSettings.REMOTE_DEBUG_PORT].
*
* @param enabled If `true` the task will be suspended.
* @throws IllegalStateException if the task with the given name is not found, or,
* if the taks is not of [JavaExec] type.
*/
fun Project.setRemoteDebug(taskName: String, enabled: Boolean = true) {
val task = tasks.findByName(taskName)
check(task != null) {
"Could not find a task named `$taskName` in the project `$name`."
}
check(task is JavaExec) {
"The task `$taskName` is not of type `JavaExec`."
}
task.remoteDebug(enabled)
}
/**
* Sets remote debug options for the `launchSpineCompiler` task.
*
* @param enabled if `true` the task will be suspended.
*
* @see remoteDebug
*/
fun Project.spineCompilerRemoteDebug(enabled: Boolean = true) =
setRemoteDebug("launchSpineCompiler", enabled)
/**
* Sets remote debug options for the `launchTestSpineCompiler` task.
*
* @param enabled if `true` the task will be suspended.
*
* @see remoteDebug
*/
fun Project.testSpineCompilerRemoteDebug(enabled: Boolean = true) =
setRemoteDebug("launchTestSpineCompiler", enabled)
/**
* Sets remote debug options for the `launchTestFixturesSpineCompiler` task.
*
* @param enabled if `true` the task will be suspended.
*
* @see remoteDebug
*/
fun Project.testFixturesSpineCompilerRemoteDebug(enabled: Boolean = true) =
setRemoteDebug("launchTestFixturesSpineCompiler", enabled)
/**
* Parts of names of configurations to be excluded by
* `artifactMeta/excludeConfigurations/containing` in the modules
* where `io.spine.atifact-meta` plugin is applied.
*/
val buildToolConfigurations: Array<String> = arrayOf(
"detekt",
"jacoco",
"pmd",
"checkstyle",
"checkerframework",
"ksp",
"dokka",
)
/**
* Make the `sourcesJar` task accept duplicated input which seems to occur
* somewhere inside Protobuf Gradle Plugin.
*/
fun Project.allowDuplicationInSourcesJar() {
tasks.withType(Jar::class.java).configureEach {
if (name == "sourcesJar") {
duplicatesStrategy = DuplicatesStrategy.INCLUDE
}
}
}