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

Fix usabililty of Configuration accessors in the configurations {} block #1129

Merged
merged 5 commits into from
Sep 24, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -801,6 +801,30 @@ class ProjectSchemaAccessorsIntegrationTest : AbstractPluginIntegrationTest() {
containsString("Type of `myConvention` receiver is MyConvention"))
}

@Test
fun `accessors to existing configurations`() {

withBuildScript("""
plugins {
java
}

configurations {
runtimeOnly {
extendsFrom(implementation.get())
}
}

configurations.implementation {
resolutionStrategy {
failOnVersionConflict()
}
}
""")

build("help")
}

private
fun withFolders(folders: FoldersDslExpression) =
projectRoot.withFolders(folders)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@

package org.gradle.kotlin.dsl.provider.plugins

import org.gradle.api.NamedDomainObjectCollectionSchema
import org.gradle.api.NamedDomainObjectContainer
import org.gradle.api.Project
import org.gradle.api.artifacts.Configuration
import org.gradle.api.plugins.ExtensionAware
import org.gradle.api.plugins.ExtensionsSchema
import org.gradle.api.reflect.HasPublicType
Expand All @@ -36,23 +39,26 @@ class DefaultProjectSchemaProvider : ProjectSchemaProvider {
ProjectSchema(
targetSchema.extensions,
targetSchema.conventions,
targetSchema.containerElements,
accessibleConfigurationsOf(project))
}
}


private
data class ExtensionConventionSchema(
data class TargetTypedSchema(
val extensions: List<ProjectSchemaEntry<TypeOf<*>>>,
val conventions: List<ProjectSchemaEntry<TypeOf<*>>>
val conventions: List<ProjectSchemaEntry<TypeOf<*>>>,
val containerElements: List<ProjectSchemaEntry<TypeOf<*>>>
)


private
fun targetSchemaFor(target: Any, targetType: TypeOf<*>): ExtensionConventionSchema {
fun targetSchemaFor(target: Any, targetType: TypeOf<*>): TargetTypedSchema {

val extensions = mutableListOf<ProjectSchemaEntry<TypeOf<*>>>()
val conventions = mutableListOf<ProjectSchemaEntry<TypeOf<*>>>()
val containerElements = mutableListOf<ProjectSchemaEntry<TypeOf<*>>>()

fun collectSchemaOf(target: Any, targetType: TypeOf<*>) {
if (target is ExtensionAware) {
Expand All @@ -68,6 +74,10 @@ fun targetSchemaFor(target: Any, targetType: TypeOf<*>): ExtensionConventionSche
conventions.add(ProjectSchemaEntry(targetType, name, type))
collectSchemaOf(target.convention.plugins[name]!!, type)
}
accessibleContainerSchema(target.configurations.collectionSchema).forEach { schema ->
containerElements.add(ProjectSchemaEntry(typeOfConfigurationContainer, schema.name, schema.publicType))
}
// WARN eagerly realize all source sets
sourceSetsOf(target)?.forEach { sourceSet ->
collectSchemaOf(sourceSet, typeOfSourceSet)
}
Expand All @@ -76,7 +86,7 @@ fun targetSchemaFor(target: Any, targetType: TypeOf<*>): ExtensionConventionSche

collectSchemaOf(target, targetType)

return ExtensionConventionSchema(extensions.distinct(), conventions.distinct())
return TargetTypedSchema(extensions.distinct(), conventions.distinct(), containerElements.distinct())
}


Expand All @@ -90,6 +100,11 @@ fun accessibleConventionsSchema(plugins: Map<String, Any>) =
plugins.filterKeys(::isPublic).mapValues { inferPublicTypeOfConvention(it.value) }


private
fun accessibleContainerSchema(collectionSchema: NamedDomainObjectCollectionSchema) =
collectionSchema.elements.filter { isPublic(it.name) }


private
fun sourceSetsOf(project: Project) =
project.extensions.findByName("sourceSets") as? SourceSetContainer
Expand Down Expand Up @@ -125,6 +140,10 @@ private
val typeOfProject = typeOf<Project>()


private
val typeOfConfigurationContainer = typeOf<NamedDomainObjectContainer<Configuration>>()
Copy link
Member

Choose a reason for hiding this comment

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

❌ Since we're getting those elements specifically from project.configurations, wouldn't it be better to limit the extensions to ConfigurationContainer only?

Copy link
Member Author

Choose a reason for hiding this comment

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

We can't because configurations {} receiver is NamedDomainObjectContainerScope<Configuration>.



private
val typeOfSourceSet = typeOf<SourceSet>()

Expand Down
Loading