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

Support for Gradle Provider properties #344

Closed
eskatos opened this issue Apr 26, 2017 · 0 comments
Closed

Support for Gradle Provider properties #344

eskatos opened this issue Apr 26, 2017 · 0 comments

Comments

@eskatos
Copy link
Member

eskatos commented Apr 26, 2017

Gradle 4.0 will introduce a supported public mean to bind lazily evaluated properties. See the release notes and user guide relevant sections.

The master branch contains a port to Kotlin build scripts of the documentation sample.

The sample will evolve.
Here's a capture of how it can be used with the master branch of gradle-script-kotlin at the time of writing without adding any Kotlin extensions:

apply {
    plugin(GreetingPlugin::class.java)
}

configure<GreetingPluginExtension> {
    message = "Hi from Gradle"
    outputFiles = files("$buildDir/a.txt", "$buildDir/b.txt")
}

open class GreetingPlugin : Plugin<Project> {
    override fun apply(project: Project) {
        // Add the 'greeting' extension object
        val greeting: GreetingPluginExtension = project.extensions.create(
            "greeting",
            GreetingPluginExtension::class.java,
            project)
        // Add a task that uses the configuration
        project.tasks {
            "hello"(Greeting::class) {
                provideMessage(greeting.messageProvider)
                outputFiles = greeting.outputFiles
            }
        }
    }
}

open class GreetingPluginExtension(val project: Project) {
    private
    val messageState = project.property(String::class.java)

    private
    val outputFileCollection = project.files()

    var message
        get() = messageState.get()
        set(value) = messageState.set(value)

    val messageProvider
        get() = messageState

    var outputFiles
        get() = outputFileCollection
        set(value) = outputFileCollection.setFrom(value)
}

open class Greeting : DefaultTask() {
    private
    val messageState = project.property(String::class.java)

    private
    val outputFileCollection = project.files()

    @get:Input
    var message
        get() = messageState.get()
        set(value) = messageState.set(value)

    @get:OutputFiles
    var outputFiles
        get() = outputFileCollection
        set(value) = outputFileCollection.setFrom(value)

    fun provideMessage(message: Provider<String>) = messageState.set(message)

    @TaskAction
    fun printMessage() {
        logger.info("Writing message '$message' to files ${outputFiles.files}")
        outputFiles.forEach { it.writeText(message) }
    }
}

This issue is about providing a more Kotlin-idiomatic way of using that new Gradle feature.

@eskatos eskatos added this to the 1.0.0 milestone Apr 26, 2017
@bamboo bamboo modified the milestones: 0.9.0, 1.0.0 May 5, 2017
@bamboo bamboo self-assigned this May 5, 2017
@bamboo bamboo changed the title Support for Gradle lazy evaluated properties Support for Gradle Provider properties May 8, 2017
bamboo added a commit that referenced this issue May 8, 2017
And `ConfigurableFileCollection` as well.

See #344
@bamboo bamboo closed this as completed in 7007c98 May 9, 2017
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants