New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for Gradle `Provider` properties #344

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

Comments

Projects
None yet
2 participants
@eskatos
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 from Support for Gradle lazy evaluated properties to Support for Gradle `Provider` properties May 8, 2017

bamboo added a commit that referenced this issue May 8, 2017

Add support for `PropertyState` delegated properties
And `ConfigurableFileCollection` as well.

See #344

@bamboo bamboo closed this in 7007c98 May 9, 2017

bamboo added a commit that referenced this issue May 9, 2017

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment