Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

potential fix for compatibility with KGP 1.9 #3081

Merged
merged 5 commits into from
Aug 8, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,34 +6,49 @@ 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
import org.jetbrains.kotlin.gradle.tasks.KotlinCompileTool

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 }
.reduce(FileCollection::plus)
} else {
// Dokka suppresses source sets that do no have compilations
// since such configuration is invalid, it reports a warning or an error
sourceSet.withAllDependentSourceSets()
.toList()
.map { it.kotlin.sourceDirectories }
.reduce { acc, fileCollection -> acc + fileCollection }
.reduce(FileCollection::plus)
}
}

private fun KotlinCompilation.compileClasspathOf(project: Project): FileCollection {
val kgpVersion = project.getKgpVersion()

// if KGP version < 1.9 or org.jetbrains.dokka.classpath.useOldResolution=true
// we will use old (pre 1.9) resolution of classpath
if (kgpVersion == null ||
kgpVersion < KotlinGradlePluginVersion(1, 9, 0) ||
project.classpathProperty("useOldResolution", default = false)
) {
return oldCompileClasspathOf(project)
}

return newCompileClasspathOf(project)
Comment on lines +35 to +38
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

as a side note (and, I guess, as acknowledgement that it's a conscious decision for anyone looking at it): names with old and new get repetitive quick, it's easy to fall into "old vs old old" / "really old vs old", but I couldn't think of a better alternative to suggest, and hopefully we'll get rid of this code altogether by Kotlin 2.0, so it will definitely do for now 👍

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah, I agree, first idea at my side for naming was like pre19* and post19* or just use pre19 for old function and no prefix for new, but it also started to look a bit odd for me

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah, I had the same idea and also found it weird looking :)

}

private fun KotlinCompilation.newCompileClasspathOf(project: Project): FileCollection {
val compilationClasspath = (compileTaskProvider.get() as? KotlinCompileTool)?.libraries ?: project.files()
return compilationClasspath + platformDependencyFiles(project)
}

private fun KotlinCompilation.oldCompileClasspathOf(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(@Suppress("DEPRECATION") this.defaultSourceSet.implementationMetadataConfigurationName) // KT-58640
?: project.files()

return this.compileDependencyFiles + platformDependencyFiles + this.classpathOf(project)
return this.compileDependencyFiles + platformDependencyFiles(project) + this.classpathOf(project)
}

private fun KotlinCompilation.classpathOf(project: Project): FileCollection {
Expand All @@ -43,7 +58,7 @@ private fun KotlinCompilation.classpathOf(project: Project): FileCollection {
val shouldKeepBackwardsCompatibility = (kgpVersion != null && kgpVersion < KotlinGradlePluginVersion(1, 7, 0))
return if (shouldKeepBackwardsCompatibility) {
// removed since 1.9.0, left for compatibility with < Kotlin 1.7
val classpathGetter= kotlinCompile::class.members
val classpathGetter = kotlinCompile::class.members
.first { it.name == "getClasspath" }
classpathGetter.call(kotlinCompile) as FileCollection
} else {
Expand All @@ -60,3 +75,16 @@ private fun KotlinCompilation.getKotlinCompileTask(kgpVersion: KotlinGradlePlugi
this.compileTaskProvider.get() as? KotlinCompile // introduced in 1.8.0
}
}

private fun KotlinCompilation.platformDependencyFiles(project: Project): FileCollection {
val excludePlatformDependencyFiles = project.classpathProperty("excludePlatformDependencyFiles", default = false)

if (excludePlatformDependencyFiles) return project.files()
return (this as? AbstractKotlinNativeCompilation)
?.target?.project?.configurations
?.findByName(@Suppress("DEPRECATION") this.defaultSourceSet.implementationMetadataConfigurationName) // KT-58640
?: project.files()
}

private fun Project.classpathProperty(name: String, default: Boolean): Boolean =
(findProperty("org.jetbrains.dokka.classpath.$name") as? String)?.toBoolean() ?: default