Skip to content

Commit

Permalink
Apply kotlin-dsl plugin (#2702)
Browse files Browse the repository at this point in the history
Update code to use idiomatic Gradle API
  • Loading branch information
aSemy committed Feb 21, 2023
1 parent f4a6a6a commit 0a09318
Show file tree
Hide file tree
Showing 14 changed files with 91 additions and 88 deletions.
2 changes: 1 addition & 1 deletion runners/gradle-plugin/build.gradle.kts
Expand Up @@ -2,7 +2,7 @@ import org.gradle.configurationcache.extensions.serviceOf
import org.jetbrains.*

plugins {
`java-gradle-plugin`
`kotlin-dsl`
id("com.gradle.plugin-publish") version "0.20.0"
}

Expand Down
Expand Up @@ -4,22 +4,22 @@ import org.gradle.api.NamedDomainObjectContainer
import org.gradle.api.internal.plugins.DslObject
import org.gradle.api.tasks.Internal
import org.gradle.api.tasks.Nested
import org.gradle.kotlin.dsl.container
import org.gradle.work.DisableCachingByDefault

@DisableCachingByDefault(because = "Abstract super-class, not to be instantiated directly")
abstract class AbstractDokkaLeafTask : AbstractDokkaTask() {

@get:Internal
val dokkaSourceSets: NamedDomainObjectContainer<GradleDokkaSourceSetBuilder> =
project.container(GradleDokkaSourceSetBuilder::class.java, gradleDokkaSourceSetBuilderFactory())
.also { container ->
DslObject(this).extensions.add("dokkaSourceSets", container)
project.kotlinOrNull?.sourceSets?.all { kotlinSourceSet ->
container.register(kotlinSourceSet.name) { dokkaSourceSet ->
dokkaSourceSet.configureWithKotlinSourceSet(kotlinSourceSet)
}
project.container(GradleDokkaSourceSetBuilder::class, gradleDokkaSourceSetBuilderFactory()).also { container ->
DslObject(this).extensions.add("dokkaSourceSets", container)
project.kotlinOrNull?.sourceSets?.all sourceSet@{
container.register(name) {
configureWithKotlinSourceSet(this@sourceSet)
}
}
}

/**
* Only contains source sets that are marked with `isDocumented`.
Expand All @@ -29,7 +29,7 @@ abstract class AbstractDokkaLeafTask : AbstractDokkaTask() {
@get:Nested
protected val unsuppressedSourceSets: List<GradleDokkaSourceSetBuilder>
get() = dokkaSourceSets
.toList()
.also(::checkSourceSetDependencies)
.filterNot { it.suppress.getSafe() }
.toList()
.also(::checkSourceSetDependencies)
.filterNot { it.suppress.getSafe() }
}
Expand Up @@ -4,25 +4,26 @@ import org.gradle.api.Project
import org.gradle.api.artifacts.Configuration
import org.gradle.api.artifacts.Dependency
import org.gradle.api.attributes.Usage
import org.gradle.kotlin.dsl.named

internal fun Project.maybeCreateDokkaDefaultPluginConfiguration(): Configuration {
return configurations.maybeCreate("dokkaPlugin") {
attributes.attribute(Usage.USAGE_ATTRIBUTE, project.objects.named(Usage::class.java, "java-runtime"))
attributes.attribute(Usage.USAGE_ATTRIBUTE, project.objects.named(Usage.JAVA_RUNTIME))
isCanBeConsumed = false
}
}

internal fun Project.maybeCreateDokkaDefaultRuntimeConfiguration(): Configuration {
return configurations.maybeCreate("dokkaRuntime") {
attributes.attribute(Usage.USAGE_ATTRIBUTE, project.objects.named(Usage::class.java, "java-runtime"))
attributes.attribute(Usage.USAGE_ATTRIBUTE, project.objects.named(Usage.JAVA_RUNTIME))
isCanBeConsumed = false
}
}

internal fun Project.maybeCreateDokkaPluginConfiguration(dokkaTaskName: String, additionalDependencies: Collection<Dependency> = emptySet()): Configuration {
return project.configurations.maybeCreate("${dokkaTaskName}Plugin") {
extendsFrom(maybeCreateDokkaDefaultPluginConfiguration())
attributes.attribute(Usage.USAGE_ATTRIBUTE, project.objects.named(Usage::class.java, "java-runtime"))
attributes.attribute(Usage.USAGE_ATTRIBUTE, project.objects.named(Usage.JAVA_RUNTIME))
isCanBeConsumed = false
dependencies.add(project.dokkaArtifacts.dokkaAnalysis) // compileOnly in base plugin
dependencies.add(project.dokkaArtifacts.dokkaBase)
Expand All @@ -33,10 +34,10 @@ internal fun Project.maybeCreateDokkaPluginConfiguration(dokkaTaskName: String,
internal fun Project.maybeCreateDokkaRuntimeConfiguration(dokkaTaskName: String): Configuration {
return project.configurations.maybeCreate("${dokkaTaskName}Runtime") {
extendsFrom(maybeCreateDokkaDefaultRuntimeConfiguration())
attributes.attribute(Usage.USAGE_ATTRIBUTE, project.objects.named(Usage::class.java, "java-runtime"))
attributes.attribute(Usage.USAGE_ATTRIBUTE, project.objects.named(Usage.JAVA_RUNTIME))
isCanBeConsumed = false
defaultDependencies { dependencies ->
dependencies.add(project.dokkaArtifacts.dokkaCore)
defaultDependencies {
add(project.dokkaArtifacts.dokkaCore)
}
}
}
Expand Up @@ -4,6 +4,8 @@ import org.gradle.api.NamedDomainObjectContainer
import org.gradle.api.Project
import org.gradle.api.UnknownDomainObjectException
import org.gradle.util.Path
import org.gradle.kotlin.dsl.findByType
import org.gradle.kotlin.dsl.getByType
import org.jetbrains.kotlin.gradle.dsl.KotlinProjectExtension
import org.jetbrains.kotlin.gradle.plugin.KotlinPlatformType
import org.jetbrains.kotlin.gradle.plugin.KotlinTarget
Expand All @@ -14,13 +16,13 @@ internal fun parsePath(path: String): Path = Path.path(path)

internal val Project.kotlinOrNull: KotlinProjectExtension?
get() = try {
project.extensions.findByType(KotlinProjectExtension::class.java)
project.extensions.findByType()
} catch (e: NoClassDefFoundError) {
null
}

internal val Project.kotlin: KotlinProjectExtension
get() = project.extensions.getByType(KotlinProjectExtension::class.java)
get() = project.extensions.getByType()

internal fun Project.isAndroidProject() = try {
project.extensions.getByName("android")
Expand Down
Expand Up @@ -19,8 +19,8 @@ class AbstractDokkaParentTaskTest {
private val subSubproject0 = ProjectBuilder.builder().withName("subSubproject0").withParent(subproject0).build()

init {
rootProject.subprojects { project ->
project.tasks.create<DokkaTask>("dokkaTask")
rootProject.subprojects {
tasks.create<DokkaTask>("dokkaTask")
}
}

Expand Down
Expand Up @@ -95,9 +95,9 @@ class ConfigureWithKotlinSourceSetGistTest {
mainSourceSet.kotlin.sourceDirectories.elements.get().map { it.asFile }.forEach { it.mkdirs() }

/* Make sure to remove dependencies that cannot be resolved during test */
project.configurations.configureEach { configuration ->
configuration.withDependencies { dependencies ->
dependencies.removeIf { dependency -> dependency !is FileCollectionDependency }
project.configurations.configureEach {
withDependencies {
removeIf { dependency -> dependency !is FileCollectionDependency }
}
}

Expand Down
Expand Up @@ -4,6 +4,10 @@ import org.gradle.kotlin.dsl.create
import org.gradle.kotlin.dsl.withType
import org.gradle.testfixtures.ProjectBuilder
import org.jetbrains.dokka.DokkaConfigurationImpl
import org.jetbrains.dokka.DokkaDefaults.cacheRoot
import org.jetbrains.dokka.DokkaDefaults.failOnWarning
import org.jetbrains.dokka.DokkaDefaults.moduleName
import org.jetbrains.dokka.DokkaDefaults.offlineMode
import org.jetbrains.dokka.DokkaException
import java.io.File
import kotlin.test.Test
Expand All @@ -19,25 +23,25 @@ class DokkaCollectorTaskTest {
val childProject = ProjectBuilder.builder().withParent(rootProject).build()
childProject.plugins.apply("org.jetbrains.kotlin.jvm")

rootProject.allprojects { project ->
project.plugins.apply("org.jetbrains.dokka")
project.tasks.withType<AbstractDokkaTask>().configureEach { task ->
task.plugins.withDependencies { dependencies -> dependencies.clear() }
rootProject.allprojects {
plugins.apply("org.jetbrains.dokka")
tasks.withType<AbstractDokkaTask>().configureEach {
plugins.withDependencies { clear() }
}
project.tasks.withType<DokkaTask>().configureEach { task ->
task.dokkaSourceSets.configureEach { sourceSet ->
sourceSet.classpath.setFrom(emptyList<Any>())
tasks.withType<DokkaTask>().configureEach {
dokkaSourceSets.configureEach {
classpath.setFrom(emptyList<Any>())
}
}
}

val collectorTasks = rootProject.tasks.withType<DokkaCollectorTask>()
collectorTasks.configureEach { task ->
task.moduleName.set("custom Module Name")
task.outputDirectory.set(File("customOutputDirectory"))
task.cacheRoot.set(File("customCacheRoot"))
task.failOnWarning.set(true)
task.offlineMode.set(true)
collectorTasks.configureEach {
moduleName.set("custom Module Name")
outputDirectory.set(File("customOutputDirectory"))
cacheRoot.set(File("customCacheRoot"))
failOnWarning.set(true)
offlineMode.set(true)
}

assertTrue(collectorTasks.isNotEmpty(), "Expected at least one collector task")
Expand Down Expand Up @@ -68,7 +72,7 @@ class DokkaCollectorTaskTest {
fun `with no child tasks throws DokkaException`() {
val project = ProjectBuilder.builder().build()
val collectorTask = project.tasks.create<DokkaCollectorTask>("collector")
project.configurations.all { configuration -> configuration.withDependencies { it.clear() } }
project.configurations.all { withDependencies { clear() } }
assertFailsWith<DokkaException> { collectorTask.generateDocumentation() }
}
}
Expand Up @@ -15,9 +15,7 @@ class DokkaConfigurationJsonTest {
val project = ProjectBuilder.builder().build()
project.plugins.apply("org.jetbrains.dokka")
val dokkaTask = project.tasks.withType<DokkaTask>().first()
dokkaTask.plugins.withDependencies { dependencies ->
dependencies.clear()
}
dokkaTask.plugins.withDependencies { clear() }
dokkaTask.apply {
this.failOnWarning.set(true)
this.offlineMode.set(true)
Expand All @@ -29,19 +27,19 @@ class DokkaConfigurationJsonTest {
this.pluginsConfiguration.add(
PluginConfigurationImpl("B", DokkaConfiguration.SerializationFormat.JSON, """ { "key" : "value2" } """)
)
this.dokkaSourceSets.create("main") { sourceSet ->
sourceSet.displayName.set("customSourceSetDisplayName")
sourceSet.reportUndocumented.set(true)
this.dokkaSourceSets.create("main") {
displayName.set("customSourceSetDisplayName")
reportUndocumented.set(true)

sourceSet.externalDocumentationLink { link ->
link.packageListUrl.set(URL("http://some.url"))
link.url.set(URL("http://some.other.url"))
externalDocumentationLink {
packageListUrl.set(URL("http://some.url"))
url.set(URL("http://some.other.url"))
}
sourceSet.perPackageOption { packageOption ->
packageOption.includeNonPublic.set(true)
packageOption.reportUndocumented.set(true)
packageOption.skipDeprecated.set(true)
packageOption.documentedVisibilities.set(setOf(DokkaConfiguration.Visibility.PRIVATE))
perPackageOption {
includeNonPublic.set(true)
reportUndocumented.set(true)
skipDeprecated.set(true)
documentedVisibilities.set(setOf(DokkaConfiguration.Visibility.PRIVATE))
}
}
}
Expand Down
Expand Up @@ -24,30 +24,28 @@ class DokkaConfigurationSerializableTest {
val project = ProjectBuilder.builder().build()
project.plugins.apply("org.jetbrains.dokka")
val dokkaTask = project.tasks.withType<DokkaTask>().first()
dokkaTask.plugins.withDependencies { dependencies ->
dependencies.clear()
}
dokkaTask.plugins.withDependencies { clear() }
dokkaTask.apply {
this.failOnWarning.set(true)
this.offlineMode.set(true)
this.outputDirectory.set(File("customOutputDir"))
this.cacheRoot.set(File("customCacheRoot"))
this.pluginsConfiguration.add(PluginConfigurationImpl("A", DokkaConfiguration.SerializationFormat.JSON, """ { "key" : "value1" } """))
this.pluginsConfiguration.add(PluginConfigurationImpl("B", DokkaConfiguration.SerializationFormat.JSON, """ { "key" : "value2" } """))
this.dokkaSourceSets.create("main") { sourceSet ->
sourceSet.displayName.set("customSourceSetDisplayName")
sourceSet.reportUndocumented.set(true)
this.dokkaSourceSets.create("main") {
displayName.set("customSourceSetDisplayName")
reportUndocumented.set(true)

sourceSet.externalDocumentationLink { link ->
link.packageListUrl.set(URL("http://some.url"))
link.url.set(URL("http://some.other.url"))
externalDocumentationLink {
packageListUrl.set(URL("http://some.url"))
url.set(URL("http://some.other.url"))
}

sourceSet.perPackageOption { packageOption ->
packageOption.includeNonPublic.set(true)
packageOption.reportUndocumented.set(true)
packageOption.skipDeprecated.set(true)
packageOption.documentedVisibilities.set(setOf(DokkaConfiguration.Visibility.PRIVATE))
perPackageOption {
includeNonPublic.set(true)
reportUndocumented.set(true)
skipDeprecated.set(true)
documentedVisibilities.set(setOf(DokkaConfiguration.Visibility.PRIVATE))
}
}
}
Expand Down
Expand Up @@ -27,9 +27,9 @@ class DokkaMultiModuleTaskTest {
}

init {
rootProject.allprojects { project ->
project.tasks.withType<AbstractDokkaTask>().configureEach { task ->
task.plugins.withDependencies { dependencies -> dependencies.clear() }
rootProject.allprojects {
tasks.withType<AbstractDokkaTask>().configureEach {
plugins.withDependencies { clear() }
}
}
}
Expand Down Expand Up @@ -57,7 +57,7 @@ class DokkaMultiModuleTaskTest {
dokkaSourceSets.create("main")
dokkaSourceSets.create("test")
dokkaSourceSets.configureEach {
it.includes.from(include1, include2)
includes.from(include1, include2)
}
}

Expand Down Expand Up @@ -151,7 +151,7 @@ class DokkaMultiModuleTaskTest {
fun `multimodule task with no child tasks throws DokkaException`() {
val project = ProjectBuilder.builder().build()
val multimodule = project.tasks.create<DokkaMultiModuleTask>("multimodule")
project.configurations.configureEach { it.withDependencies { it.clear() } }
project.configurations.configureEach { withDependencies { clear() } }
assertFailsWith<DokkaException> { multimodule.generateDocumentation() }
}

Expand All @@ -163,17 +163,17 @@ class DokkaMultiModuleTaskTest {

childDokkaTask.apply {
dokkaSourceSets.create("main") {
it.includes.from(childDokkaTaskInclude1, childDokkaTaskInclude2)
includes.from(childDokkaTaskInclude1, childDokkaTaskInclude2)
}
dokkaSourceSets.create("main2") {
it.includes.from(childDokkaTaskInclude3)
includes.from(childDokkaTaskInclude3)
}
}

val secondChildDokkaTaskInclude = childProject.file("include4")
val secondChildDokkaTask = childProject.tasks.create<DokkaTaskPartial>("secondChildDokkaTask") {
dokkaSourceSets.create("main") {
it.includes.from(secondChildDokkaTaskInclude)
includes.from(secondChildDokkaTaskInclude)
}
}
multiModuleTask.addChildTask(secondChildDokkaTask)
Expand Down
Expand Up @@ -11,12 +11,12 @@ class DokkaTaskTest {
fun `no suppressed source sets are present after in built configuration`() {
val project = ProjectBuilder.builder().build()
val task = project.tasks.create<DokkaTask>("dokkaTask")
project.configurations.all { configuration -> configuration.withDependencies { it.clear() } }
project.configurations.all { withDependencies { clear() } }

task.dokkaSourceSets.register("main")
task.dokkaSourceSets.register("jvm")
task.dokkaSourceSets.register("test") {
it.suppress.set(true)
suppress.set(true)
}

assertEquals(
Expand All @@ -30,7 +30,7 @@ class DokkaTaskTest {
fun `module version is not present if not specified`(){
val project = ProjectBuilder.builder().build()
val task = project.tasks.create<DokkaTask>("dokkaTask")
project.configurations.all { configuration -> configuration.withDependencies { it.clear() } }
project.configurations.all { withDependencies { clear() } }

task.dokkaSourceSets.register("main")
assertNull(task.buildDokkaConfiguration().moduleVersion)
Expand Down
Expand Up @@ -225,9 +225,9 @@ class GradleDokkaSourceSetBuilderTest {
})

sourceSet.sourceLink {
it.remoteLineSuffix.set("ls2")
it.localDirectory.set(project.file("p2"))
it.remoteUrl.set(URL("https://u2"))
remoteLineSuffix.set("ls2")
localDirectory.set(project.file("p2"))
remoteUrl.set(URL("https://u2"))
}

sourceSet.sourceLink(project.closureOf<GradleSourceLinkBuilder> {
Expand Down Expand Up @@ -269,7 +269,7 @@ class GradleDokkaSourceSetBuilderTest {
})

sourceSet.perPackageOption {
it.matchingRegex.set("p2.*")
matchingRegex.set("p2.*")
}

sourceSet.perPackageOption(project.closureOf<GradlePackageOptionsBuilder> {
Expand Down Expand Up @@ -311,7 +311,7 @@ class GradleDokkaSourceSetBuilderTest {
)

sourceSet.externalDocumentationLink {
it.url.set(URL("https://u2"))
url.set(URL("https://u2"))
}

sourceSet.externalDocumentationLink(project.closureOf<GradleExternalDocumentationLinkBuilder> {
Expand Down

0 comments on commit 0a09318

Please sign in to comment.