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
|
package org.jetbrains.dokka.dokkatoo
import org.jetbrains.dokka.dokkatoo.distributions.DokkatooConfigurationAttributes
import org.jetbrains.dokka.dokkatoo.distributions.DokkatooConfigurationAttributes.Companion.DOKKATOO_BASE_ATTRIBUTE
import org.jetbrains.dokka.dokkatoo.distributions.DokkatooConfigurationAttributes.Companion.DOKKATOO_CATEGORY_ATTRIBUTE
import org.jetbrains.dokka.dokkatoo.distributions.DokkatooConfigurationAttributes.Companion.DOKKA_FORMAT_ATTRIBUTE
import org.jetbrains.dokka.dokkatoo.dokka.parameters.DokkaSourceSetSpec
import org.jetbrains.dokka.dokkatoo.dokka.parameters.KotlinPlatform
import org.jetbrains.dokka.dokkatoo.dokka.parameters.VisibilityModifier
import org.jetbrains.dokka.dokkatoo.internal.*
import org.jetbrains.dokka.dokkatoo.tasks.DokkatooGenerateTask
import org.jetbrains.dokka.dokkatoo.tasks.DokkatooPrepareModuleDescriptorTask
import org.jetbrains.dokka.dokkatoo.tasks.DokkatooTask
import java.io.File
import javax.inject.Inject
import kotlinx.serialization.ExperimentalSerializationApi
import kotlinx.serialization.json.Json
import org.gradle.api.NamedDomainObjectContainer
import org.gradle.api.Plugin
import org.gradle.api.Project
import org.gradle.api.artifacts.Configuration
import org.gradle.api.file.ProjectLayout
import org.gradle.api.file.RegularFileProperty
import org.gradle.api.model.ObjectFactory
import org.gradle.api.provider.Property
import org.gradle.api.provider.Provider
import org.gradle.api.provider.ProviderFactory
import org.gradle.api.tasks.TaskContainer
import org.gradle.kotlin.dsl.*
import org.gradle.language.base.plugins.LifecycleBasePlugin
/**
* The base plugin for Dokkatoo. Sets up Dokkatoo and configures default values, but does not
* add any specific config (specifically, it does not create Dokka Publications).
*/
abstract class DokkatooBasePlugin
@DokkatooInternalApi
@Inject
constructor(
private val providers: ProviderFactory,
private val layout: ProjectLayout,
private val objects: ObjectFactory,
) : Plugin<Project> {
override fun apply(target: Project) {
// apply the lifecycle-base plugin so the clean task is available
target.pluginManager.apply(LifecycleBasePlugin::class)
val dokkatooExtension = createExtension(target)
target.tasks.createDokkaLifecycleTasks()
val configurationAttributes = objects.newInstance<DokkatooConfigurationAttributes>()
target.dependencies.attributesSchema {
attribute(DOKKATOO_BASE_ATTRIBUTE)
attribute(DOKKATOO_CATEGORY_ATTRIBUTE)
attribute(DOKKA_FORMAT_ATTRIBUTE)
}
target.configurations.register(dependencyContainerNames.dokkatoo) {
description = "Fetch all Dokkatoo files from all configurations in other subprojects"
asConsumer()
isVisible = false
attributes {
attribute(DOKKATOO_BASE_ATTRIBUTE, configurationAttributes.dokkatooBaseUsage)
}
}
configureDokkaPublicationsDefaults(dokkatooExtension)
dokkatooExtension.dokkatooSourceSets.configureDefaults(
sourceSetScopeConvention = dokkatooExtension.sourceSetScopeDefault
)
target.tasks.withType<DokkatooGenerateTask>().configureEach {
cacheDirectory.convention(dokkatooExtension.dokkatooCacheDirectory)
workerDebugEnabled.convention(false)
workerLogFile.convention(temporaryDir.resolve("dokka-worker.log"))
workerJvmArgs.set(
listOf(
//"-XX:MaxMetaspaceSize=512m",
"-XX:+HeapDumpOnOutOfMemoryError",
"-XX:+AlwaysPreTouch", // https://github.com/gradle/gradle/issues/3093#issuecomment-387259298
//"-XX:StartFlightRecording=disk=true,name={path.drop(1).map { if (it.isLetterOrDigit()) it else '-' }.joinToString("")},dumponexit=true,duration=30s",
//"-XX:FlightRecorderOptions=repository=$baseDir/jfr,stackdepth=512",
)
)
dokkaConfigurationJsonFile.convention(temporaryDir.resolve("dokka-configuration.json"))
}
target.tasks.withType<DokkatooPrepareModuleDescriptorTask>().configureEach {
moduleName.convention(dokkatooExtension.moduleName)
includes.from(providers.provider { dokkatooExtension.dokkatooSourceSets.flatMap { it.includes } })
modulePath.convention(dokkatooExtension.modulePath)
}
target.tasks.withType<DokkatooGenerateTask>().configureEach {
publicationEnabled.convention(true)
onlyIf("publication must be enabled") { publicationEnabled.getOrElse(true) }
generator.dokkaSourceSets.addAllLater(
providers.provider {
// exclude suppressed source sets as early as possible, to avoid unnecessary dependency resolution
dokkatooExtension.dokkatooSourceSets.filterNot { it.suppress.get() }
}
)
generator.dokkaSourceSets.configureDefaults(
sourceSetScopeConvention = dokkatooExtension.sourceSetScopeDefault
)
}
dokkatooExtension.dokkatooSourceSets.configureDefaults(
sourceSetScopeConvention = dokkatooExtension.sourceSetScopeDefault
)
}
private fun createExtension(project: Project): DokkatooExtension {
val dokkatooExtension = project.extensions.create<DokkatooExtension>(EXTENSION_NAME).apply {
moduleName.convention(providers.provider { project.name })
moduleVersion.convention(providers.provider { project.version.toString() })
modulePath.convention(project.pathAsFilePath())
konanHome.convention(
providers
.provider {
// konanHome is set into in extraProperties:
// https://github.com/JetBrains/kotlin/blob/v1.9.0/libraries/tools/kotlin-gradle-plugin/src/common/kotlin/org/jetbrains/kotlin/gradle/targets/native/KotlinNativeTargetPreset.kt#L35-L38
project.extensions.extraProperties.get("konanHome") as? String?
}
.map { File(it) }
)
sourceSetScopeDefault.convention(project.path)
dokkatooPublicationDirectory.convention(layout.buildDirectory.dir("dokka"))
dokkatooModuleDirectory.convention(layout.buildDirectory.dir("dokka-module"))
dokkatooConfigurationsDirectory.convention(layout.buildDirectory.dir("dokka-config"))
}
dokkatooExtension.versions {
jetbrainsDokka.convention(DokkatooConstants.DOKKA_VERSION)
jetbrainsMarkdown.convention("0.3.1")
freemarker.convention("2.3.31")
kotlinxHtml.convention("0.8.0")
kotlinxCoroutines.convention("1.6.4")
}
return dokkatooExtension
}
/** Set defaults in all [DokkatooExtension.dokkatooPublications]s */
private fun configureDokkaPublicationsDefaults(
dokkatooExtension: DokkatooExtension,
) {
dokkatooExtension.dokkatooPublications.all {
enabled.convention(true)
cacheRoot.convention(dokkatooExtension.dokkatooCacheDirectory)
delayTemplateSubstitution.convention(false)
failOnWarning.convention(false)
finalizeCoroutines.convention(false)
moduleName.convention(dokkatooExtension.moduleName)
moduleVersion.convention(dokkatooExtension.moduleVersion)
offlineMode.convention(false)
outputDir.convention(dokkatooExtension.dokkatooPublicationDirectory)
suppressInheritedMembers.convention(false)
suppressObviousFunctions.convention(true)
}
}
/** Set conventions for all [DokkaSourceSetSpec] properties */
private fun NamedDomainObjectContainer<DokkaSourceSetSpec>.configureDefaults(
sourceSetScopeConvention: Property<String>,
) {
configureEach dss@{
analysisPlatform.convention(KotlinPlatform.DEFAULT)
displayName.convention(
analysisPlatform.map { platform ->
// Match existing Dokka naming conventions. (This should probably be simplified!)
when {
// Multiplatform source sets (e.g. commonMain, jvmMain, macosMain)
name.endsWith("Main") -> name.substringBeforeLast("Main")
// indeterminate source sets should be named by the Kotlin platform
else -> platform.displayName
}
}
)
documentedVisibilities.convention(setOf(VisibilityModifier.PUBLIC))
jdkVersion.convention(8)
enableKotlinStdLibDocumentationLink.convention(true)
enableJdkDocumentationLink.convention(true)
enableAndroidDocumentationLink.convention(
analysisPlatform.map { it == KotlinPlatform.AndroidJVM }
)
reportUndocumented.convention(false)
skipDeprecated.convention(false)
skipEmptyPackages.convention(true)
sourceSetScope.convention(sourceSetScopeConvention)
// Manually added sourceSets should not be suppressed by default. dokkatooSourceSets that are
// automatically added by DokkatooKotlinAdapter will have a sensible value for suppress.
suppress.convention(false)
suppressGeneratedFiles.convention(true)
sourceLinks.configureEach {
localDirectory.convention(layout.projectDirectory)
remoteLineSuffix.convention("#L")
}
perPackageOptions.configureEach {
matchingRegex.convention(".*")
suppress.convention(false)
skipDeprecated.convention(false)
reportUndocumented.convention(false)
}
externalDocumentationLinks {
configureEach {
enabled.convention(true)
packageListUrl.convention(url.map { it.appendPath("package-list") })
}
maybeCreate("jdk") {
enabled.convention(this@dss.enableJdkDocumentationLink)
url(this@dss.jdkVersion.map { jdkVersion ->
when {
jdkVersion < 11 -> "https://docs.oracle.com/javase/${jdkVersion}/docs/api/"
else -> "https://docs.oracle.com/en/java/javase/${jdkVersion}/docs/api/"
}
})
packageListUrl(this@dss.jdkVersion.map { jdkVersion ->
when {
jdkVersion < 11 -> "https://docs.oracle.com/javase/${jdkVersion}/docs/api/package-list"
else -> "https://docs.oracle.com/en/java/javase/${jdkVersion}/docs/api/element-list"
}
})
}
maybeCreate("kotlinStdlib") {
enabled.convention(this@dss.enableKotlinStdLibDocumentationLink)
url("https://kotlinlang.org/api/latest/jvm/stdlib/")
}
maybeCreate("androidSdk") {
enabled.convention(this@dss.enableAndroidDocumentationLink)
url("https://developer.android.com/reference/kotlin/")
}
maybeCreate("androidX") {
enabled.convention(this@dss.enableAndroidDocumentationLink)
url("https://developer.android.com/reference/kotlin/")
packageListUrl("https://developer.android.com/reference/kotlin/androidx/package-list")
}
}
}
}
private fun TaskContainer.createDokkaLifecycleTasks() {
register<DokkatooTask>(taskNames.generate) {
description = "Generates Dokkatoo publications for all formats"
dependsOn(withType<DokkatooGenerateTask>())
}
}
// workaround for https://github.com/gradle/gradle/issues/23708
private fun RegularFileProperty.convention(file: File): RegularFileProperty =
convention(objects.fileProperty().fileValue(file))
// workaround for https://github.com/gradle/gradle/issues/23708
private fun RegularFileProperty.convention(file: Provider<File>): RegularFileProperty =
convention(objects.fileProperty().fileProvider(file))
companion object {
const val EXTENSION_NAME = "dokkatoo"
/**
* The group of all Dokkatoo [Gradle tasks][org.gradle.api.Task].
*
* @see org.gradle.api.Task.getGroup
*/
const val TASK_GROUP = "dokkatoo"
/** The names of [Gradle tasks][org.gradle.api.Task] created by Dokkatoo */
val taskNames = TaskNames(null)
/** The names of [Configuration]s created by Dokkatoo */
val dependencyContainerNames = DependencyContainerNames(null)
internal val jsonMapper = Json {
prettyPrint = true
@OptIn(ExperimentalSerializationApi::class)
prettyPrintIndent = " "
}
}
@DokkatooInternalApi
abstract class HasFormatName {
abstract val formatName: String?
/** Appends [formatName] to the end of the string, camelcase style, if [formatName] is not null */
protected fun String.appendFormat(): String =
when (val name = formatName) {
null -> this
else -> this + name.uppercaseFirstChar()
}
}
/**
* Names of the Gradle [Configuration]s used by the [Dokkatoo Plugin][DokkatooBasePlugin].
*
* Beware the confusing terminology:
* - [Gradle Configurations][org.gradle.api.artifacts.Configuration] - share files between subprojects. Each has a name.
* - [DokkaConfiguration][org.jetbrains.dokka.DokkaConfiguration] - parameters for executing the Dokka Generator
*/
@DokkatooInternalApi
class DependencyContainerNames(override val formatName: String?) : HasFormatName() {
val dokkatoo = "dokkatoo".appendFormat()
/** Name of the [Configuration] that _consumes_ all [org.jetbrains.dokka.DokkaConfiguration.DokkaModuleDescription] files */
val dokkatooModuleFilesConsumer = "dokkatooModule".appendFormat()
/** Name of the [Configuration] that _provides_ all [org.jetbrains.dokka.DokkaConfiguration.DokkaModuleDescription] files to other projects */
val dokkatooModuleFilesProvider = "dokkatooModuleElements".appendFormat()
/**
* Classpath used to execute the Dokka Generator.
*
* Extends [dokkaPluginsClasspath], so Dokka plugins and their dependencies are included.
*/
val dokkaGeneratorClasspath = "dokkatooGeneratorClasspath".appendFormat()
/** Dokka Plugins (including transitive dependencies, so this can be passed to the Dokka Generator Worker classpath) */
val dokkaPluginsClasspath = "dokkatooPlugin".appendFormat()
/**
* Dokka Plugins (excluding transitive dependencies) will be used to create Dokka Generator Parameters
*
* Generally, this configuration should not be invoked manually. Instead, use [dokkaPluginsClasspath].
*/
val dokkaPluginsIntransitiveClasspath = "dokkatooPluginIntransitive".appendFormat()
}
@DokkatooInternalApi
class TaskNames(override val formatName: String?) : HasFormatName() {
val generate = "dokkatooGenerate".appendFormat()
val generatePublication = "dokkatooGeneratePublication".appendFormat()
val generateModule = "dokkatooGenerateModule".appendFormat()
val prepareModuleDescriptor = "prepareDokkatooModuleDescriptor".appendFormat()
}
}
|