-
Notifications
You must be signed in to change notification settings - Fork 392
/
kotlinClasspathUtils.kt
59 lines (50 loc) · 2.57 KB
/
kotlinClasspathUtils.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
package org.jetbrains.dokka.gradle.kotlin
import org.gradle.api.Project
import org.gradle.api.file.FileCollection
import org.jetbrains.dokka.gradle.isAndroidTarget
import org.jetbrains.kotlin.gradle.plugin.KotlinSourceSet
import org.jetbrains.kotlin.gradle.plugin.mpp.AbstractKotlinNativeCompilation
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
internal fun Project.classpathOf(sourceSet: KotlinSourceSet): FileCollection {
val compilations = compilationsOf(sourceSet)
return if (compilations.isNotEmpty()) {
compilations
.map { compilation -> compilation.compileClasspathOf(project = this) }
.reduce { acc, fileCollection -> acc + fileCollection }
} else {
sourceSet.withAllDependentSourceSets()
.toList()
.map { it.kotlin.sourceDirectories }
.reduce { acc, fileCollection -> acc + fileCollection }
}
}
private fun KotlinCompilation.compileClasspathOf(project: Project): FileCollection {
if (this.target.isAndroidTarget()) { // Workaround for https://youtrack.jetbrains.com/issue/KT-33893
return this.classpathOf(project)
}
val platformDependencyFiles: FileCollection = (this as? AbstractKotlinNativeCompilation)
?.target?.project?.configurations
?.findByName(this.defaultSourceSet.implementationMetadataConfigurationName)
?: project.files()
return this.compileDependencyFiles + platformDependencyFiles + this.classpathOf(project)
}
private fun KotlinCompilation.classpathOf(project: Project): FileCollection {
val kgpVersion = project.getKgpVersion()
val kotlinCompile = this.getKotlinCompileTask(kgpVersion) ?: return project.files()
val shouldKeepBackwardsCompatibility = (kgpVersion != null && kgpVersion < KotlinGradlePluginVersion(1, 7, 0))
return if (shouldKeepBackwardsCompatibility) {
@Suppress("DEPRECATION_ERROR")
kotlinCompile.classpath // deprecated with error since 1.8.0, left for compatibility with < Kotlin 1.7
} else {
kotlinCompile.libraries // introduced in 1.7.0
}
}
private fun KotlinCompilation.getKotlinCompileTask(kgpVersion: KotlinGradlePluginVersion? = null): KotlinCompile? {
val shouldKeepBackwardsCompatibility = (kgpVersion != null && kgpVersion < KotlinGradlePluginVersion(1, 8, 0))
return if (shouldKeepBackwardsCompatibility) {
@Suppress("DEPRECATION") // for `compileKotlinTask` property, deprecated with warning since 1.8.0
this.compileKotlinTask as? KotlinCompile
} else {
this.compileTaskProvider.get() as? KotlinCompile // introduced in 1.8.0
}
}