thrifty/thrifty-gradle-plugin
Ben Bader 8371641ab1
Bump Kotlin, Gradle, fix deprecations (#539)
2023-09-09 20:48:02 -06:00
..
src Bump Kotlin, Gradle, fix deprecations (#539) 2023-09-09 20:48:02 -06:00
README.md A few small refactors and cleanups on thrifty-gradle-plugin (#526) 2023-02-13 10:28:34 -07:00
build.gradle Bump Kotlin, Gradle, fix deprecations (#539) 2023-09-09 20:48:02 -06:00
gradle.properties Fix Maven publication (#364) 2020-06-09 10:46:43 -07:00

README.md

Gradle Plugin

Thrifty's Gradle plugin allows one to incorporate .thrift source files into a project and have Thrifty generate Kotlin or Java code from them as part of regular builds.

Incorporate it into your build like so:

plugins {
    id 'org.jetbrains.kotlin.jvm'
    id 'com.microsoft.thrifty'
}

// usual gradle stuff

// Configuration is optional as there are generally sane defaults
thrifty {
    // By default, thrifty will use all thrift files in 'src/main/thrift'.
    // You can override this in the following ways:

    // Specify a directory containing thrift files.
    // Note that if you do, 'src/main/thrift' is ignored.
    // By default, all .thrift files in this directory and its subdirectores
    // will be compiled.
    sourceDir 'path/to/thrift/files', 'path/to/other/thrift/files'

    // Specify several directories containing thrift files, as above.
    // Note the plural name 'sourceDirs'.
    sourceDirs 'path/to/thrift/files', 'path/to/other/thrift/files'

    // Specify a directory containing thrift files along with glob patterns
    // identifying the exact files to include or exclude.
    sourceDir('path/to/thrift/files') {
        // Include and exclude patterns as per:
        // https://docs.gradle.org/current/javadoc/org/gradle/api/tasks/util/PatternFilterable.html
        include '**/*.thrift'
        exclude '**/*.analytics.thrift'
    }

    // Augment the thrift include path with one or more directories:
    includePath 'path/to/include/dir', 'path/to/other/dir'

    // Ordinarily, generated sources go to 'build/generated/sources/thrifty', and this is
    // probably fine for 99.9% of use cases.  If you need the generated sources elsewhere,
    // you can specify a different location:
    outputDir 'some/other/location'

    // By default, Kotlin sources will be generated.  You can provide a few options to the
    // Kotlin code generator (all options shown with their default values):
    kotlin {
        // Thrift service implementations are generated by default.  Set 'false'
        // here to prevent them from being generated.
        emitServiceClients true

        // By default, struct fields are named exactly as written in the .thrift IDL.
        // Other choices are 'java' and 'pascal' - 'someField' and 'SomeField', respectively.
        nameStyle 'default'

        // Standard Kotlin collections are used by default.  If you want something custom, provide
        // that here.
        // For example, you might choose 'android.util.ArrayMap' for maps.
        listType 'kotlin.collections.ArrayList'
        setType 'kotlin.collections.LinkedHashSet'
        mapType 'kotlin.collections.LinkedHashMap'

        // When true, generated structs will implement the Parcelable interface.  This is optional,
        // and results in larger code.
        parcelable = false

        // The above options are also applicable to the 'java' block; Kotlin-specific options
        // follow.

        // When true, generated structs will be include 'Builder' inner classes.  This is
        // for legacy compatibiltity only, and leads to larger code.
        structBuilders = false

        // The Kotlin code generator supports several different service-client API styles.
        // Vaid values are 'default' (the default), 'coroutine', and 'none'.
        serviceClientStyle 'default'
    }


    // On the other hand, if you want Java sources, then _don't_ add the kotlin block.  Add at
    // least an empty 'java' block - all options shown here may be left out entirely.
    java {
        // Most of the options shown in the kotlin block above also apply here.

        // Specifies which kind of nullability annotations to add to generated code, if any.
        //
        // valid values are 'none' (the default), 'android-support', and 'androidx'.
        nullabilityAnnotationKind 'none'
    }
}

// If you want to put a TypeProcessor on the classpath, you can do it like so:
dependencies {
    thriftyTypeProcessor "com.example:example-type-processor:1.2.3"
}

Implementation Notes

Unlike the rest of Thrifty, this plugin is written in Java. This is because Gradle bundles its own version of Kotlin, and provides no guarantee that it will be compatible with any other Kotlin code.

According to Gradle's own advice, the only safe way to use Kotlin in Gradle plugins, long-term, is to encapsulate all of it behind a classpath-isolated invocation of their Worker API. This is what we do.