All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.google.protobuf.gradle.ProtobufConfiguratorExts.kt Maven / Gradle / Ivy

Go to download

Gradle build plugin to handle Protocol Buffers automated code generation and compilation

There is a newer version: 0.9.4
Show newest version
package com.google.protobuf.gradle

import org.gradle.api.NamedDomainObjectContainer
import org.gradle.api.Project
import org.gradle.api.plugins.ExtensionAware
import org.gradle.api.tasks.SourceSet
import org.gradle.kotlin.dsl.*


fun Project.protobuf(action: ProtobufConfigurator.()->Unit) {
    project.convention.getPlugin(ProtobufConvention::class.java).protobuf.apply(action)
}

fun SourceSet.proto(action: ProtobufSourceDirectorySet.() -> Unit) {
    (this as? ExtensionAware)
        ?.extensions
        ?.getByType(ProtobufSourceDirectorySet::class.java)
        ?.apply(action)
}

fun ProtobufConfigurator.protoc(closure: ExecutableLocator.() -> Unit) {
    protoc(closureOf(closure))
}

fun ProtobufConfigurator.plugins(closure: NamedDomainObjectContainerScope.() -> Unit) {
    plugins(closureOf> {
        this(closure)
    })
}

fun  NamedDomainObjectContainerScope.id(id: String, closure: (T.() -> Unit)? = null) {
    closure?.let { create(id, closureOf(it)) }
        ?: create(id)
}

fun  NamedDomainObjectContainerScope.remove(id: String) {
    remove(this[id])
}

fun ProtobufConfigurator.generateProtoTasks(closure: ProtobufConfigurator.GenerateProtoTaskCollection.()->Unit) {
    generateProtoTasks(closureOf(closure))
}

fun GenerateProtoTask.builtins(closure: NamedDomainObjectContainerScope.()->Unit) {
    builtins(closureOf> {
        this(closure)
    })
}

fun GenerateProtoTask.plugins(closure: NamedDomainObjectContainerScope.()-> Unit) {
    plugins(closureOf> {
        this(closure)
    })
}

/**
 * The method generatorProtoTasks applies the supplied closure to the
 * instance of [ProtobufConfigurator.GenerateProtoTaskCollection].
 *
 * Since [ProtobufConfigurator.JavaGenerateProtoTaskCollection] and [ProtobufConfigurator.AndroidGenerateProtoTaskCollection]
 * each have unique methods, and only one instance in allocated per project, we need a way to statically resolve the
 * available methods. This is a necessity since Kotlin does not have any dynamic method resolution capabilities.
 */

fun ProtobufConfigurator.GenerateProtoTaskCollection.ofSourceSet(sourceSet: String): Collection =
    if (this is ProtobufConfigurator.JavaGenerateProtoTaskCollection)
        this.ofSourceSet(sourceSet) else emptyList()

fun ProtobufConfigurator.GenerateProtoTaskCollection.ofFlavor(flavor: String): Collection =
    if (this is ProtobufConfigurator.AndroidGenerateProtoTaskCollection)
        this.ofFlavor(flavor) else emptyList()

fun ProtobufConfigurator.GenerateProtoTaskCollection.ofBuildType(buildType: String): Collection =
    if (this is ProtobufConfigurator.AndroidGenerateProtoTaskCollection)
        this.ofBuildType(buildType) else emptyList()

fun ProtobufConfigurator.GenerateProtoTaskCollection.ofVariant(variant: String): Collection =
    if (this is ProtobufConfigurator.AndroidGenerateProtoTaskCollection)
        this.ofVariant(variant) else emptyList()

fun ProtobufConfigurator.GenerateProtoTaskCollection.ofNonTest(): Collection =
    if (this is ProtobufConfigurator.AndroidGenerateProtoTaskCollection)
        this.ofNonTest() else emptyList()

fun ProtobufConfigurator.GenerateProtoTaskCollection.ofTest(): Collection =
    if (this is ProtobufConfigurator.AndroidGenerateProtoTaskCollection)
        this.ofTest() else emptyList()




© 2015 - 2024 Weber Informatics LLC | Privacy Policy