Skip to content

Commit

Permalink
replaced custom Gradle property functions with Gradle Kotlin DSL
Browse files Browse the repository at this point in the history
  • Loading branch information
aSemy committed Feb 22, 2023
1 parent 8c0344e commit 42c6cc4
Show file tree
Hide file tree
Showing 20 changed files with 167 additions and 181 deletions.
Expand Up @@ -23,7 +23,7 @@ fun interface DokkaMultiModuleFileLayout {
*/
object NoCopy : DokkaMultiModuleFileLayout {
override fun targetChildOutputDirectory(parent: DokkaMultiModuleTask, child: AbstractDokkaTask): File =
child.outputDirectory.getSafe()
child.outputDirectory.get()
}

/**
Expand All @@ -38,7 +38,7 @@ fun interface DokkaMultiModuleFileLayout {
val relativeProjectPath = parent.project.relativeProjectPath(child.project.path)
val relativeFilePath = relativeProjectPath.replace(":", File.separator)
check(!File(relativeFilePath).isAbsolute) { "Unexpected absolute path $relativeFilePath" }
return parent.outputDirectory.getSafe().resolve(relativeFilePath)
return parent.outputDirectory.get().resolve(relativeFilePath)
}
}
}
Expand All @@ -56,7 +56,7 @@ internal fun DokkaMultiModuleTask.copyChildOutputDirectories() {

internal fun DokkaMultiModuleTask.copyChildOutputDirectory(child: AbstractDokkaTask) {
val targetChildOutputDirectory = project.file(fileLayout.get().targetChildOutputDirectory(this, child))
val sourceChildOutputDirectory = child.outputDirectory.getSafe()
val sourceChildOutputDirectory = child.outputDirectory.get()

/* Pointing to the same directory -> No copy necessary */
if (sourceChildOutputDirectory.absoluteFile == targetChildOutputDirectory.absoluteFile) {
Expand All @@ -79,4 +79,3 @@ internal fun DokkaMultiModuleTask.copyChildOutputDirectory(child: AbstractDokkaT

sourceChildOutputDirectory.copyRecursively(targetChildOutputDirectory, overwrite = true)
}

@@ -1,26 +1,6 @@
package org.jetbrains.dokka.gradle

import org.gradle.api.model.ObjectFactory
import org.gradle.api.provider.Property
import org.gradle.api.provider.Provider
import org.gradle.kotlin.dsl.property
import org.jetbrains.dokka.utilities.cast
import kotlin.reflect.typeOf

internal inline fun <reified T : Any> ObjectFactory.safeProperty() = property<T?>()

internal inline fun <reified T : Any> Property<T?>.safeConvention(value: T): Property<T> {
return this.convention(value).cast()
}

internal inline fun <reified T : Any> Property<T?>.safeConvention(provider: Provider<T?>): Property<T> {
return this.convention(provider).cast()
}

@OptIn(ExperimentalStdlibApi::class)
internal inline fun <reified T> Provider<T>.getSafe(): T =
if (typeOf<T>().isMarkedNullable) orNull as T
else get()

internal fun Provider<String>.getValidVersionOrNull() = orNull?.takeIf { it != "unspecified" }

Expand Up @@ -9,28 +9,28 @@ internal fun GradleDokkaSourceSetBuilder.toDokkaSourceSetImpl(): DokkaSourceSetI
displayName = displayNameOrDefault(),
sourceSetID = sourceSetID,
sourceRoots = sourceRoots.toSet(),
dependentSourceSets = dependentSourceSets.getSafe().toSet(),
dependentSourceSets = dependentSourceSets.get().toSet(),
samples = samples.toSet(),
includes = includes.toSet(),
includeNonPublic = includeNonPublic.getSafe(),
documentedVisibilities = documentedVisibilities.getSafe(),
reportUndocumented = reportUndocumented.getSafe(),
skipEmptyPackages = skipEmptyPackages.getSafe(),
skipDeprecated = skipDeprecated.getSafe(),
jdkVersion = jdkVersion.getSafe(),
sourceLinks = sourceLinks.getSafe().build().toSet(),
perPackageOptions = perPackageOptions.getSafe().build(),
includeNonPublic = includeNonPublic.get(),
documentedVisibilities = documentedVisibilities.get(),
reportUndocumented = reportUndocumented.get(),
skipEmptyPackages = skipEmptyPackages.get(),
skipDeprecated = skipDeprecated.get(),
jdkVersion = jdkVersion.get(),
sourceLinks = sourceLinks.get().build().toSet(),
perPackageOptions = perPackageOptions.get().build(),
externalDocumentationLinks = externalDocumentationLinksWithDefaults(),
languageVersion = languageVersion.getSafe(),
apiVersion = apiVersion.getSafe(),
noStdlibLink = noStdlibLink.getSafe(),
noJdkLink = noJdkLink.getSafe(),
languageVersion = languageVersion.orNull,
apiVersion = apiVersion.orNull,
noStdlibLink = noStdlibLink.get(),
noJdkLink = noJdkLink.get(),
suppressedFiles = suppressedFilesWithDefaults(),
analysisPlatform = platform.getSafe()
analysisPlatform = platform.get()
)

private fun GradleDokkaSourceSetBuilder.displayNameOrDefault(): String {
displayName.getSafe()?.let { return it }
displayName.orNull?.let { return it }
if (name.endsWith("Main") && name != "Main") {
return name.removeSuffix("Main")
}
Expand All @@ -39,17 +39,17 @@ private fun GradleDokkaSourceSetBuilder.displayNameOrDefault(): String {
}

private fun GradleDokkaSourceSetBuilder.externalDocumentationLinksWithDefaults(): Set<ExternalDocumentationLinkImpl> {
return externalDocumentationLinks.getSafe().build()
return externalDocumentationLinks.get().build()
.run {
if (noJdkLink.getSafe()) this
else this + ExternalDocumentationLink.jdk(jdkVersion.getSafe())
if (noJdkLink.get()) this
else this + ExternalDocumentationLink.jdk(jdkVersion.get())
}
.run {
if (noStdlibLink.getSafe()) this
if (noStdlibLink.get()) this
else this + ExternalDocumentationLink.kotlinStdlib()
}
.run {
if (noAndroidSdkLink.getSafe() || !project.isAndroidProject()) this
if (noAndroidSdkLink.get() || !project.isAndroidProject()) this
else this +
ExternalDocumentationLink.androidSdk() +
ExternalDocumentationLink.androidX()
Expand All @@ -58,7 +58,7 @@ private fun GradleDokkaSourceSetBuilder.externalDocumentationLinksWithDefaults()
}

private fun GradleDokkaSourceSetBuilder.suppressedFilesWithDefaults(): Set<File> {
val suppressedGeneratedFiles = if (suppressGeneratedFiles.getSafe()) {
val suppressedGeneratedFiles = if (suppressGeneratedFiles.get()) {
val generatedRoot = project.buildDir.resolve("generated").absoluteFile
sourceRoots
.filter { it.startsWith(generatedRoot) }
Expand Down
Expand Up @@ -8,6 +8,7 @@ import org.gradle.api.provider.Property
import org.gradle.api.provider.SetProperty
import org.gradle.api.tasks.*
import org.gradle.kotlin.dsl.listProperty
import org.gradle.kotlin.dsl.property
import org.gradle.kotlin.dsl.setProperty
import org.jetbrains.dokka.*
import java.io.File
Expand Down Expand Up @@ -51,8 +52,8 @@ open class GradleDokkaSourceSetBuilder(
* Default is `false`.
*/
@Input
val suppress: Property<Boolean> = project.objects.safeProperty<Boolean>()
.safeConvention(false)
val suppress: Property<Boolean> = project.objects.property<Boolean>()
.convention(false)

/**
* Display name used to refer to the source set.
Expand All @@ -64,7 +65,7 @@ open class GradleDokkaSourceSetBuilder(
*/
@Input
@Optional
val displayName: Property<String?> = project.objects.safeProperty()
val displayName: Property<String?> = project.objects.property()

/**
* List of Markdown files that contain
Expand Down Expand Up @@ -108,8 +109,9 @@ open class GradleDokkaSourceSetBuilder(
* Default is [DokkaConfiguration.Visibility.PUBLIC].
*/
@Input
val documentedVisibilities: SetProperty<DokkaConfiguration.Visibility> = project.objects.setProperty<DokkaConfiguration.Visibility>()
.convention(DokkaDefaults.documentedVisibilities)
val documentedVisibilities: SetProperty<DokkaConfiguration.Visibility> =
project.objects.setProperty<DokkaConfiguration.Visibility>()
.convention(DokkaDefaults.documentedVisibilities)

/**
* Specifies source sets that current source set depends on.
Expand Down Expand Up @@ -169,8 +171,8 @@ open class GradleDokkaSourceSetBuilder(
* Default is `false`.
*/
@Input
val reportUndocumented: Property<Boolean> = project.objects.safeProperty<Boolean>()
.safeConvention(DokkaDefaults.reportUndocumented)
val reportUndocumented: Property<Boolean> = project.objects.property<Boolean>()
.convention(DokkaDefaults.reportUndocumented)

/**
* Specifies the location of the project source code on the Web. If provided, Dokka generates
Expand Down Expand Up @@ -209,8 +211,8 @@ open class GradleDokkaSourceSetBuilder(
*/
@Input
@Optional
val platform: Property<Platform> = project.objects.safeProperty<Platform>()
.safeConvention(Platform.DEFAULT)
val platform: Property<Platform> = project.objects.property<Platform>()
.convention(Platform.DEFAULT)

/**
* Whether to skip packages that contain no visible declarations after
Expand All @@ -222,8 +224,8 @@ open class GradleDokkaSourceSetBuilder(
* Default is `true`.
*/
@Input
val skipEmptyPackages: Property<Boolean> = project.objects.safeProperty<Boolean>()
.safeConvention(DokkaDefaults.skipEmptyPackages)
val skipEmptyPackages: Property<Boolean> = project.objects.property<Boolean>()
.convention(DokkaDefaults.skipEmptyPackages)

/**
* Whether to document declarations annotated with [Deprecated].
Expand All @@ -233,8 +235,8 @@ open class GradleDokkaSourceSetBuilder(
* Default is `false`.
*/
@Input
val skipDeprecated: Property<Boolean> = project.objects.safeProperty<Boolean>()
.safeConvention(DokkaDefaults.skipDeprecated)
val skipDeprecated: Property<Boolean> = project.objects.property<Boolean>()
.convention(DokkaDefaults.skipDeprecated)

/**
* Directories or individual files that should be suppressed, meaning declarations from them
Expand All @@ -256,8 +258,8 @@ open class GradleDokkaSourceSetBuilder(
* Default is `true`.
*/
@Input
val suppressGeneratedFiles: Property<Boolean> = project.objects.safeProperty<Boolean>()
.safeConvention(DokkaDefaults.suppressGeneratedFiles)
val suppressGeneratedFiles: Property<Boolean> = project.objects.property<Boolean>()
.convention(DokkaDefaults.suppressGeneratedFiles)

/**
* Whether to generate external documentation links that lead to API reference
Expand All @@ -266,8 +268,8 @@ open class GradleDokkaSourceSetBuilder(
* Default is `false`, meaning links will be generated.
*/
@Input
val noStdlibLink: Property<Boolean> = project.objects.safeProperty<Boolean>()
.safeConvention(DokkaDefaults.noStdlibLink)
val noStdlibLink: Property<Boolean> = project.objects.property<Boolean>()
.convention(DokkaDefaults.noStdlibLink)

/**
* Whether to generate external documentation links to JDK's Javadocs
Expand All @@ -278,8 +280,8 @@ open class GradleDokkaSourceSetBuilder(
* Default is `false`, meaning links will be generated.
*/
@Input
val noJdkLink: Property<Boolean> = project.objects.safeProperty<Boolean>()
.safeConvention(DokkaDefaults.noJdkLink)
val noJdkLink: Property<Boolean> = project.objects.property<Boolean>()
.convention(DokkaDefaults.noJdkLink)

/**
* Whether to generate external documentation links for Android SDK API reference
Expand All @@ -290,8 +292,8 @@ open class GradleDokkaSourceSetBuilder(
* Default is `false`, meaning links will be generated.
*/
@Input
val noAndroidSdkLink: Property<Boolean> = project.objects.safeProperty<Boolean>()
.safeConvention(DokkaDefaults.noAndroidSdkLink)
val noAndroidSdkLink: Property<Boolean> = project.objects.property<Boolean>()
.convention(DokkaDefaults.noAndroidSdkLink)

/**
* [Kotlin language version](https://kotlinlang.org/docs/compatibility-modes.html)
Expand All @@ -302,7 +304,7 @@ open class GradleDokkaSourceSetBuilder(
*/
@Input
@Optional
val languageVersion: Property<String?> = project.objects.safeProperty()
val languageVersion: Property<String?> = project.objects.property()

/**
* [Kotlin API version](https://kotlinlang.org/docs/compatibility-modes.html)
Expand All @@ -313,7 +315,7 @@ open class GradleDokkaSourceSetBuilder(
*/
@Input
@Optional
val apiVersion: Property<String?> = project.objects.safeProperty()
val apiVersion: Property<String?> = project.objects.property()

/**
* JDK version to use when generating external documentation links for Java types.
Expand All @@ -325,15 +327,15 @@ open class GradleDokkaSourceSetBuilder(
* Default is JDK 8.
*/
@Input
val jdkVersion: Property<Int> = project.objects.safeProperty<Int>()
.safeConvention(DokkaDefaults.jdkVersion)
val jdkVersion: Property<Int> = project.objects.property<Int>()
.convention(DokkaDefaults.jdkVersion)

/**
* Deprecated. Use [documentedVisibilities] instead.
*/
@Input
val includeNonPublic: Property<Boolean> = project.objects.safeProperty<Boolean>()
.safeConvention(DokkaDefaults.includeNonPublic)
val includeNonPublic: Property<Boolean> = project.objects.property<Boolean>()
.convention(DokkaDefaults.includeNonPublic)

fun DokkaSourceSetID(sourceSetName: String): DokkaSourceSetID = sourceSetIdFactory.create(sourceSetName)

Expand Down
Expand Up @@ -5,6 +5,7 @@ import org.gradle.api.provider.Property
import org.gradle.api.tasks.Input
import org.gradle.api.tasks.Internal
import org.gradle.api.tasks.Optional
import org.gradle.kotlin.dsl.property
import org.jetbrains.dokka.DokkaConfigurationBuilder
import org.jetbrains.dokka.ExternalDocumentationLink
import org.jetbrains.dokka.ExternalDocumentationLinkImpl
Expand Down Expand Up @@ -52,7 +53,7 @@ class GradleExternalDocumentationLinkBuilder(
* ```
*/
@Input
val url: Property<URL?> = project.objects.safeProperty()
val url: Property<URL> = project.objects.property()

/**
* Specifies the exact location of a `package-list` instead of relying on Dokka
Expand All @@ -66,10 +67,10 @@ class GradleExternalDocumentationLinkBuilder(
*/
@Optional
@Input
val packageListUrl: Property<URL?> = project.objects.safeProperty()
val packageListUrl: Property<URL> = project.objects.property()

override fun build(): ExternalDocumentationLinkImpl = ExternalDocumentationLink(
url = checkNotNull(url.getSafe()) { "url not specified " },
packageListUrl = packageListUrl.getSafe()
url = checkNotNull(url.get()) { "url not specified " },
packageListUrl = packageListUrl.orNull,
)
}

0 comments on commit 42c6cc4

Please sign in to comment.