-
Notifications
You must be signed in to change notification settings - Fork 7
/
DokkatooKotlinAdapter.kt
193 lines (166 loc) · 7.08 KB
/
DokkatooKotlinAdapter.kt
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
package dev.adamko.dokkatoo.adapters
import com.android.build.gradle.api.ApplicationVariant
import com.android.build.gradle.api.LibraryVariant
import dev.adamko.dokkatoo.DokkatooBasePlugin
import dev.adamko.dokkatoo.DokkatooExtension
import dev.adamko.dokkatoo.dokka.parameters.DokkaSourceSetIdSpec.Companion.dokkaSourceSetIdSpec
import dev.adamko.dokkatoo.dokka.parameters.KotlinPlatform
import dev.adamko.dokkatoo.internal.DokkatooInternalApi
import dev.adamko.dokkatoo.internal.LocalProjectOnlyFilter
import dev.adamko.dokkatoo.internal.not
import javax.inject.Inject
import org.gradle.api.Plugin
import org.gradle.api.Project
import org.gradle.api.file.FileCollection
import org.gradle.api.logging.Logging
import org.gradle.api.model.ObjectFactory
import org.gradle.api.plugins.ExtensionContainer
import org.gradle.kotlin.dsl.*
import org.jetbrains.kotlin.gradle.dsl.KotlinMultiplatformExtension
import org.jetbrains.kotlin.gradle.dsl.KotlinProjectExtension
import org.jetbrains.kotlin.gradle.dsl.KotlinSingleTargetExtension
import org.jetbrains.kotlin.gradle.plugin.KotlinCompilation
import org.jetbrains.kotlin.gradle.plugin.KotlinPlatformType
import org.jetbrains.kotlin.gradle.plugin.mpp.KotlinJvmAndroidCompilation
/**
* The [DokkatooKotlinAdapter] plugin will automatically register Kotlin source sets as Dokka source sets. *
*
* **Must be applied *after* [dev.adamko.dokkatoo.DokkatooBasePlugin]**
*/
@DokkatooInternalApi
abstract class DokkatooKotlinAdapter @Inject constructor(
private val objects: ObjectFactory,
) : Plugin<Project> {
private val logger = Logging.getLogger(this::class.java)
override fun apply(project: Project) {
logger.info("applied DokkaKotlinAdapter to ${project.path}")
project.plugins.withType<DokkatooBasePlugin>().configureEach {
project.pluginManager.apply {
withPlugin("org.jetbrains.kotlin.android") { exec(project) }
withPlugin("org.jetbrains.kotlin.js") { exec(project) }
withPlugin("org.jetbrains.kotlin.jvm") { exec(project) }
withPlugin("org.jetbrains.kotlin.multiplatform") { exec(project) }
}
}
}
private fun exec(project: Project) {
val kotlinExtension = project.extensions.findKotlinExtension() ?: run {
logger.info("could not find Kotlin Extension")
return
}
logger.info("Configuring Dokka in Gradle Kotlin Project ${project.path}")
val dokkatooExtension = project.extensions.getByType<DokkatooExtension>()
val kotlinPlatformType = kotlinExtension.kotlinPlatformType()
val allKotlinCompilations: Collection<KotlinCompilation<*>> = when (kotlinExtension) {
is KotlinMultiplatformExtension -> kotlinExtension.targets.flatMap { it.compilations }
is KotlinSingleTargetExtension<*> -> kotlinExtension.target.compilations
else -> emptyList()
}
kotlinExtension.sourceSets.all kss@{
/** Get all compilations for this source set */
val kssCompilations: List<KotlinCompilation<*>> =
allKotlinCompilations.filter { compilation ->
this@kss == compilation.defaultSourceSet
|| this@kss in compilation.kotlinSourceSets
|| this@kss in compilation.allKotlinSourceSets
}
val kotlinSourceSetConfigurationNames: List<String> =
kssCompilations.flatMap { compilationSourceSet ->
sequence {
with(compilationSourceSet) {
yield(compileDependencyConfigurationName)
yield(implementationConfigurationName)
yield(runtimeOnlyConfigurationName)
yield(apiConfigurationName)
yield(compileOnlyConfigurationName)
yieldAll(relatedConfigurationNames)
}
}
}
/** Determine if a source set is 'main', and not test sources */
val isMainSourceSet: Boolean =
kssCompilations.isEmpty() || kssCompilations.any { it.isMainCompilation() }
// TODO: Needs to respect filters.
// We probably need to change from "sourceRoots" to support "sourceFiles"
// https://github.com/Kotlin/dokka/issues/1215
val extantKotlinSourceRoots = [email protected] { it.exists() }
val dependentSourceSetIds = [email protected] { dependedKss ->
objects.dokkaSourceSetIdSpec(
"${project.path}:${[email protected]}:${dependedKss.name}",
dependedKss.name,
)
}
logger.info("kotlin source set ${[email protected]} has source roots: ${extantKotlinSourceRoots.map { it.invariantSeparatorsPath }}")
dokkatooExtension.dokkatooSourceSets.create([email protected]) {
displayName.convention(
analysisPlatform.map { platform ->
name.substringBeforeLast(
delimiter = "Main",
missingDelimiterValue = platform.key,
)
}
)
suppress.convention(!isMainSourceSet)
sourceRoots.from(extantKotlinSourceRoots)
classpath.from(getKSSClasspath(project, kotlinSourceSetConfigurationNames))
analysisPlatform.convention(KotlinPlatform.fromString(kotlinPlatformType.name))
dependentSourceSets.addAll(dependentSourceSetIds)
}
}
}
/** Get the classpath used to build and run a Kotlin Source Set */
private fun getKSSClasspath(
project: Project,
kotlinSourceSetConfigurationNames: List<String>,
): FileCollection =
kotlinSourceSetConfigurationNames
.mapNotNull { kssConfName -> project.configurations.findByName(kssConfName) }
.filter { conf -> conf.isCanBeResolved }
.fold(objects.fileCollection()) { classpathCollector, conf ->
classpathCollector.from(
@Suppress("UnstableApiUsage")
conf
.incoming
.artifactView {
componentFilter(!LocalProjectOnlyFilter)
lenient(true)
}.artifacts
.resolvedArtifacts
.map { artifacts -> artifacts.map { it.file } }
)
}
companion object {
private fun KotlinProjectExtension.kotlinPlatformType(): KotlinPlatformType {
return when (this) {
is KotlinMultiplatformExtension -> {
targets
.map { it.platformType }
.distinct()
.singleOrNull() ?: KotlinPlatformType.common
}
is KotlinSingleTargetExtension<*> -> target.platformType
else -> KotlinPlatformType.common
}
}
private fun ExtensionContainer.findKotlinExtension(): KotlinProjectExtension? =
try {
findByType()
?: findByType<org.jetbrains.kotlin.gradle.dsl.KotlinJvmProjectExtension>()
} catch (e: Throwable) {
when (e) {
is TypeNotPresentException,
is ClassNotFoundException,
is NoClassDefFoundError -> null
else -> throw e
}
}
private fun KotlinCompilation<*>.isMainCompilation(): Boolean {
return when (this) {
is KotlinJvmAndroidCompilation ->
androidVariant is LibraryVariant || androidVariant is ApplicationVariant
else ->
name == "main"
}
}
}
}