Skip to content
This repository has been archived by the owner on Aug 19, 2020. It is now read-only.

Commit

Permalink
Generate compilable accessors for container elements of default packa…
Browse files Browse the repository at this point in the history
…ge type

Relates to #1158
  • Loading branch information
bamboo committed Oct 3, 2018
1 parent 385ee42 commit a8b14d1
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -93,25 +93,19 @@ class ProjectSchemaAccessorsIntegrationTest : AbstractPluginIntegrationTest() {
@Test
fun `can access extension of default package type`() {

withFolders {

"buildSrc" {

withPrecompiledPlugins()
withBuildSrc {

"src/main/kotlin" {
"src/main/kotlin" {

withFile("Extension.kt", """
class Extension(private val name: String) : org.gradle.api.Named {
override fun getName() = name
}
""")
withFile("Extension.kt", """
class Extension(private val name: String) : org.gradle.api.Named {
override fun getName() = name
}
""")

withFile("plugin.gradle.kts", """
extensions.add("extension", Extension("foo"))
extensions.add("beans", container(Extension::class))
""")
}
withFile("plugin.gradle.kts", """
extensions.add("extension", Extension("foo"))
""")
}
}

Expand All @@ -123,41 +117,83 @@ class ProjectSchemaAccessorsIntegrationTest : AbstractPluginIntegrationTest() {
println("extension: " + typeOf(extension))
extension { println("extension{} " + typeOf(this)) }
println("beans: " + typeOf(beans))
beans { println("beans{} " + typeOf(beans)) }
""")

assertThat(
build("help", "-q").output,
containsMultiLineString("""
extension: Extension
extension{} Extension
beans: org.gradle.api.NamedDomainObjectContainer<Extension>
beans{} org.gradle.api.NamedDomainObjectContainer<Extension>
""")
)
}

@Test
fun `can access task of default package type`() {
fun `can access container element of default package type`() {

withFolders {
withBuildSrc {

"buildSrc" {
"src/main/kotlin" {

withPrecompiledPlugins()
withFile("Bean.kt", """
open class Bean(private val name: String) : org.gradle.api.Named {
override fun getName() = name
}
""")

"src/main/kotlin" {
withFile("plugin.gradle.kts", """
withFile("CustomTask.kt", """
open class CustomTask : org.gradle.api.DefaultTask()
""")
import org.gradle.api.internal.*
import org.gradle.internal.reflect.*
withFile("plugin.gradle.kts", """
tasks.register<CustomTask>("customTask")
""")
}
val beans = DefaultPolymorphicDomainObjectContainer(
Any::class.java,
DirectInstantiator.INSTANCE
) { (it as Bean).name }
// TODO: schema doesn't expose the registered element as Bean
beans.register("bean", Bean::class)
extensions.add("beans", beans)
""")
}
}

withBuildScript("""
plugins { id("plugin") }
inline fun <reified T> typeOf(value: T) = typeOf<T>()
println("beans: " + typeOf(beans))
println("bean: " + typeOf(beans.bean))
beans { println("beans{} " + typeOf(beans)) }
""")

// TODO: `bean` should be exposed as `Bean` in the collection schema
assertThat(
build("help", "-q").output,
containsMultiLineString("""
beans: org.gradle.api.NamedDomainObjectContainer<java.lang.Object>
bean: org.gradle.api.NamedDomainObjectProvider<java.lang.Object>
beans{} org.gradle.api.NamedDomainObjectContainer<java.lang.Object>
""")
)
}

@Test
fun `can access task of default package type`() {

withBuildSrc {
"src/main/kotlin" {

withFile("CustomTask.kt", """
open class CustomTask : org.gradle.api.DefaultTask()
""")

withFile("plugin.gradle.kts", """
tasks.register<CustomTask>("customTask")
""")
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -247,16 +247,23 @@ fun sourceFilesWithAccessorsFor(projectSchema: ProjectSchema<TypeAccessibility>,
}


private
internal
fun importsRequiredBy(schemaSubset: ProjectSchema<TypeAccessibility>): List<String> =
defaultPackageTypesIn(
schemaSubset
.run { extensions.flatMap { listOf(it.target, it.type) } + tasks.map { it.type } }
candidateTypesForImportIn(schemaSubset)
.filterIsInstance<TypeAccessibility.Accessible>()
.map { it.type }
)


private
fun candidateTypesForImportIn(projectSchema: ProjectSchema<TypeAccessibility>) = projectSchema.run {
(extensions.flatMap { listOf(it.target, it.type) }
+ tasks.map { it.type }
+ containerElements.map { it.type })
}


internal
fun defaultPackageTypesIn(typeStrings: List<String>): List<String> =
typeStrings
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

package org.gradle.kotlin.dsl.accessors

import org.gradle.kotlin.dsl.accessors.TypeAccessibility.Accessible

import org.hamcrest.CoreMatchers.equalTo
import org.hamcrest.MatcherAssert.assertThat

Expand All @@ -32,4 +34,29 @@ class DefaultPackageTypesTest {
equalTo(listOf("Extension"))
)
}

@Test
fun `#importsRequiredBy takes container elements into account`() {

assertThat(
importsRequiredBy(
ProjectSchema(
containerElements = listOf(
ProjectSchemaEntry(
Accessible("Container"),
"element",
Accessible("DefaultPackageType")
)
),
extensions = emptyList(),
conventions = emptyList(),
tasks = emptyList(),
configurations = emptyList()
)
),
equalTo(
listOf("DefaultPackageType")
)
)
}
}

0 comments on commit a8b14d1

Please sign in to comment.