This repository has been archived by the owner on Aug 19, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 434
Fix a bunch of issues in the containers APIs #1116
Merged
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
b6178c1
Prove named(name, action) works on monomorphic named containers
eskatos 7afa5b8
Fix wrong receiver of container registering delegated property
eskatos cf67955
Add more coverage over polymorphic domain object containers
eskatos 36b4c46
Add missing getByName(type, kclass, action)
eskatos 4bb3ea2
Add compilation checks for containers api return types
eskatos 6d37305
Add missing overloads returning TaskProvider<T> for TaskContainer
eskatos 7f634f0
Add assertions to TaskContainerEvalTest
eskatos 119dcf6
Add compilation checks for containers configuration receiver type
eskatos efd5c63
Add missing provideDelegate returning TaskProvider<T> for TaskContainer
eskatos c7ff3f6
Move TaskProvider<T> returning named() extensions up to TaskCollectio…
eskatos bb7eeb0
Refine TaskContainerEvalTest exercising generated Gradle api extensions
eskatos 5e46e66
Refine NamedContainersEvalTest
eskatos 7bc27bf
Refine GradleApiExtensionsFixture
eskatos 0864315
Rename tests
eskatos 72ebcff
Address review feedback
eskatos File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,6 +16,7 @@ | |
package org.gradle.kotlin.dsl | ||
|
||
import org.gradle.api.Task | ||
import org.gradle.api.tasks.TaskCollection | ||
import org.gradle.api.tasks.TaskContainer | ||
import org.gradle.api.tasks.TaskProvider | ||
|
||
|
@@ -87,6 +88,17 @@ operator fun <U : Task> ExistingDomainObjectDelegateProviderWithType<out TaskCon | |
) | ||
|
||
|
||
/** | ||
* Provides a [TaskProvider] delegate for the task of the given type named after the property after configuring it with the given action. | ||
*/ | ||
operator fun <U : Task> ExistingDomainObjectDelegateProviderWithTypeAndAction<out TaskContainer, U>.provideDelegate( | ||
receiver: Any?, | ||
property: KProperty<*> | ||
) = ExistingDomainObjectDelegate( | ||
delegateProvider.named(property.name, type).apply { configure(action) } | ||
) | ||
|
||
|
||
/** | ||
* Registers a task and provides a delegate with the resulting [TaskProvider]. | ||
*/ | ||
|
@@ -175,29 +187,29 @@ class TaskContainerScope(val container: TaskContainer) : TaskContainer by contai | |
/** | ||
* Locates a task by name and type, without triggering its creation or configuration, failing if there is no such task. | ||
* | ||
* @see [TaskContainer.named] | ||
* @see [TaskCollection.named] | ||
*/ | ||
@Suppress("extension_shadowed_by_member") | ||
inline fun <reified T : Task> TaskContainer.named(name: String): TaskProvider<T> = | ||
inline fun <reified T : Task> TaskCollection<out Task>.named(name: String): TaskProvider<T> = | ||
named(name, T::class) | ||
|
||
|
||
/** | ||
* Locates a task by name and type, without triggering its creation or configuration, failing if there is no such task. | ||
* | ||
* @see [TaskContainer.named] | ||
* @see [TaskCollection.named] | ||
*/ | ||
fun <T : Task> TaskContainer.named(name: String, type: KClass<T>): TaskProvider<T> = | ||
fun <T : Task> TaskCollection<out Task>.named(name: String, type: KClass<T>): TaskProvider<T> = | ||
named(name, type) {} | ||
|
||
|
||
/** | ||
* Configures a task by name and type, without triggering its creation or configuration, failing if there is no such task. | ||
* | ||
* @see [TaskContainer.named] | ||
* @see [TaskCollection.named] | ||
* @see [TaskProvider.configure] | ||
*/ | ||
fun <T : Task> TaskContainer.named(name: String, type: KClass<T>, configuration: T.() -> Unit): TaskProvider<T> = | ||
fun <T : Task> TaskCollection<out Task>.named(name: String, type: KClass<T>, configuration: T.() -> Unit): TaskProvider<T> = | ||
uncheckedCast(named(name).also { provider -> | ||
provider.configure { obj -> | ||
configuration( | ||
|
@@ -208,6 +220,37 @@ fun <T : Task> TaskContainer.named(name: String, type: KClass<T>, configuration: | |
}) | ||
|
||
|
||
/** | ||
* Configures a task by name and type, without triggering its creation or configuration, failing if there is no such task. | ||
* | ||
* @see [TaskCollection.named] | ||
* @see [TaskProvider.configure] | ||
*/ | ||
inline fun <reified T : Task> TaskCollection<out Task>.named(name: String, noinline configuration: T.() -> Unit): TaskProvider<T> = | ||
named<T>(name).apply { | ||
configure(configuration) | ||
} | ||
|
||
|
||
/** | ||
* Defines a new object, which will be created when it is required. | ||
* | ||
* @see [TaskContainer.register] | ||
*/ | ||
@Suppress("extension_shadowed_by_member") | ||
inline fun <reified T : Task> TaskContainer.register(name: String): TaskProvider<T> = | ||
register(name, T::class.java) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Seeing a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, look at other files changed in this PR There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
|
||
|
||
/** | ||
* Defines and configure a new object, which will be created when it is required. | ||
* | ||
* @see [TaskContainer.register] | ||
*/ | ||
inline fun <reified T : Task> TaskContainer.register(name: String, noinline configuration: T.() -> Unit): TaskProvider<T> = | ||
register(name, T::class.java, configuration) | ||
|
||
|
||
/** | ||
* Creates a [Task] with the given [name] and type, passing the given arguments to the [javax.inject.Inject]-annotated constructor, | ||
* and adds it to this project tasks container. | ||
|
215 changes: 215 additions & 0 deletions
215
subprojects/provider/src/test/kotlin/org/gradle/kotlin/dsl/NamedContainersDslTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,215 @@ | ||
/* | ||
* Copyright 2018 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.gradle.kotlin.dsl | ||
|
||
import org.gradle.api.Project | ||
|
||
import org.gradle.kotlin.dsl.fixtures.AbstractDslTest | ||
import org.gradle.kotlin.dsl.fixtures.newProjectBuilderProjectWith | ||
|
||
import org.hamcrest.CoreMatchers.hasItem | ||
import org.hamcrest.CoreMatchers.hasItems | ||
import org.hamcrest.MatcherAssert.assertThat | ||
|
||
import org.junit.Test | ||
|
||
|
||
class NamedContainersDslTest : AbstractDslTest() { | ||
|
||
@Test | ||
fun `monomorphic named domain object container api`() { | ||
|
||
testConfigurationContainerVia("api", """ | ||
|
||
val foo: Configuration = configurations.getByName("foo") | ||
val bar: Configuration = configurations.getByName("bar") { | ||
extendsFrom(foo) | ||
} | ||
|
||
val bazar: Configuration = configurations.create("bazar") | ||
val cathedral: Configuration = configurations.create("cathedral") { | ||
extendsFrom(bazar) | ||
} | ||
|
||
val cabin: NamedDomainObjectProvider<Configuration> = configurations.named("cabin") | ||
val castle: NamedDomainObjectProvider<Configuration> = configurations.named("castle") { | ||
extendsFrom(cabin.get()) | ||
} | ||
|
||
val valley: NamedDomainObjectProvider<Configuration> = configurations.register("valley") | ||
val hill: NamedDomainObjectProvider<Configuration> = configurations.register("hill") { | ||
extendsFrom(valley.get()) | ||
} | ||
""") | ||
} | ||
|
||
@Test | ||
fun `monomorphic named domain object container scope api`() { | ||
|
||
testConfigurationContainerVia("scope-api", """ | ||
configurations { | ||
|
||
val foo: Configuration = getByName("foo") | ||
val bar: Configuration = getByName("bar") { | ||
extendsFrom(foo) | ||
} | ||
|
||
val bazar: Configuration = create("bazar") | ||
val cathedral: Configuration = create("cathedral") { | ||
extendsFrom(bazar) | ||
} | ||
|
||
val cabin: NamedDomainObjectProvider<Configuration> = named("cabin") | ||
val castle: NamedDomainObjectProvider<Configuration> = named("castle") { | ||
extendsFrom(cabin.get()) | ||
} | ||
|
||
val valley: NamedDomainObjectProvider<Configuration> = register("valley") | ||
val hill: NamedDomainObjectProvider<Configuration> = register("hill") { | ||
extendsFrom(valley.get()) | ||
} | ||
} | ||
""") | ||
} | ||
|
||
@Test | ||
fun `monomorphic named domain object container delegated properties`() { | ||
|
||
testConfigurationContainerVia("delegated-properties", """ | ||
|
||
val foo: Configuration by configurations.getting | ||
val bar: Configuration by configurations.getting { | ||
extendsFrom(foo) | ||
} | ||
|
||
val bazar: Configuration by configurations.creating | ||
val cathedral: Configuration by configurations.creating { | ||
extendsFrom(bazar) | ||
} | ||
|
||
val cabin: NamedDomainObjectProvider<Configuration> by configurations.existing | ||
val castle: NamedDomainObjectProvider<Configuration> by configurations.existing { | ||
extendsFrom(cabin.get()) | ||
} | ||
|
||
val valley: NamedDomainObjectProvider<Configuration> by configurations.registering | ||
val hill: NamedDomainObjectProvider<Configuration> by configurations.registering { | ||
extendsFrom(valley.get()) | ||
} | ||
""") | ||
} | ||
|
||
@Test | ||
fun `monomorphic named domain object container scope delegated properties`() { | ||
|
||
testConfigurationContainerVia("scope-delegated-properties", """ | ||
configurations { | ||
|
||
val foo: Configuration by getting | ||
val bar: Configuration by getting { | ||
extendsFrom(foo) | ||
} | ||
|
||
val bazar: Configuration by creating | ||
val cathedral: Configuration by creating { | ||
extendsFrom(bazar) | ||
} | ||
|
||
val cabin: NamedDomainObjectProvider<Configuration> by existing | ||
val castle: NamedDomainObjectProvider<Configuration> by existing { | ||
extendsFrom(cabin.get()) | ||
} | ||
|
||
val valley: NamedDomainObjectProvider<Configuration> by registering | ||
val hill: NamedDomainObjectProvider<Configuration> by registering { | ||
extendsFrom(valley.get()) | ||
} | ||
} | ||
""") | ||
} | ||
|
||
@Test | ||
fun `monomorphic named domain object container scope string invoke`() { | ||
|
||
testConfigurationContainerVia("scope-string-invoke", """ | ||
configurations { | ||
|
||
val foo: NamedDomainObjectProvider<Configuration> = "foo"() | ||
val bar: NamedDomainObjectProvider<Configuration> = "bar" { | ||
extendsFrom(foo.get()) | ||
} | ||
|
||
val cabin: NamedDomainObjectProvider<Configuration> = "cabin"() | ||
val castle: NamedDomainObjectProvider<Configuration> = "castle" { | ||
extendsFrom(cabin.get()) | ||
} | ||
} | ||
""") { | ||
configurations { | ||
val bazar by creating | ||
create("cathedral") { | ||
it.extendsFrom(bazar) | ||
} | ||
val valley by registering | ||
register("hill") { | ||
it.extendsFrom(valley.get()) | ||
} | ||
} | ||
apply(plugin = "java") | ||
} | ||
} | ||
|
||
private | ||
fun testConfigurationContainerVia(name: String, script: String, configuration: Project.() -> Unit = {}) { | ||
newProjectBuilderProjectWith(newFolder(name)).run { | ||
|
||
preExistingConfigurations.forEach { name -> | ||
configurations.register(name) | ||
} | ||
|
||
configuration() | ||
|
||
eval(script) | ||
|
||
assertThat( | ||
configurations.names.sorted(), | ||
hasItems( | ||
*expectedConfigurationsExtendsFrom.flatMap { | ||
listOf(it.first, it.second) | ||
}.sorted().toTypedArray() | ||
) | ||
) | ||
expectedConfigurationsExtendsFrom.forEach { (first, second) -> | ||
assertThat(configurations[first].extendsFrom, hasItem(configurations[second])) | ||
} | ||
} | ||
} | ||
|
||
companion object { | ||
|
||
val preExistingConfigurations = listOf( | ||
"foo", "bar", "cabin", "castle" | ||
) | ||
|
||
val expectedConfigurationsExtendsFrom = listOf( | ||
"bar" to "foo", | ||
"cathedral" to "bazar", | ||
"castle" to "cabin", | ||
"hill" to "valley" | ||
) | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👀 That is quite a class name. 👀