Kotlin-dsl-samples: Support convenience configuration of existing objects in NamedDomainObjectContainer

Created on 15 Dec 2016  路  10Comments  路  Source: gradle/kotlin-dsl-samples

See #35.

feature kotlin-dsl-api ease-of-use groovy-parity

Most helpful comment

As of #35, it is now possible to select and configure tasks with the following patterns:

Select single task by name

Use the collection indexer.

task["jar"].dependsOn("someOtherTask")

Configure multiple tasks by name

Invoke the task container (works with any DomainObjectContainer).

tasks {
    "jar" {
         dependsOn("someOtherTask")
    }

    "clean"(Delete::class) { // specify type information so `delete` is available
         delete("someOtherDir")
    }
}

Bring a task into scope for multiple references

Declare a delegated property using the container as a delegate (works with any DomainObjectCollection).

val jar: Jar by tasks
jar.apply {
    from(sourceSets["main"].allSource)
    from(sourceSets["test"].allSource)
}

tasks["build"].dependsOn(jar)

All 10 comments

Can Kotlin do

sourceSets {
  "main" {

  }
}

?

@oehme, yes and I was considering to use that syntax for #35 since it implies a cardinality of _many_ and it's similar to the Groovy version.

If it can do that, can it also do sourceSets."main"?

No, not that but it can do sourceSets["main"].

I think that's even better when you think of it as a container or map.

@oehme, I agree that for accessing an existing element it probably won't get better than that.

How do you envision the configuration of an existing source set, for example?

sourceSets["test鈥淽.compileClasspath = configurations["compileClasspath"] + sourceSets["main鈥淽.output

Must be designed together with #35.

Consider also the contrast between eager and lazy configuration methods such as getByName and withType.

As of #35, it is now possible to select and configure tasks with the following patterns:

Select single task by name

Use the collection indexer.

task["jar"].dependsOn("someOtherTask")

Configure multiple tasks by name

Invoke the task container (works with any DomainObjectContainer).

tasks {
    "jar" {
         dependsOn("someOtherTask")
    }

    "clean"(Delete::class) { // specify type information so `delete` is available
         delete("someOtherDir")
    }
}

Bring a task into scope for multiple references

Declare a delegated property using the container as a delegate (works with any DomainObjectCollection).

val jar: Jar by tasks
jar.apply {
    from(sourceSets["main"].allSource)
    from(sourceSets["test"].allSource)
}

tasks["build"].dependsOn(jar)
Was this page helpful?
0 / 5 - 0 ratings

Related issues

jaredsburrows picture jaredsburrows  路  3Comments

scompt picture scompt  路  4Comments

ilya-g picture ilya-g  路  4Comments

deeprim picture deeprim  路  4Comments

jmfayard picture jmfayard  路  3Comments