buildscript {
    repositories {
        maven {
            url "https://plugins.gradle.org/m2/"
        }
    }
    dependencies {
        //taken from https://github.com/tbroyer/gradle-errorprone-plugin
        classpath 'net.ltgt.gradle:gradle-errorprone-plugin:4.3.0'
    }
}

apply plugin: "net.ltgt.errorprone"

dependencies {
    //annotationProcessor "com.uber.nullaway:nullaway:0.6.4"

    //taken from https://github.com/google/error-prone/releases
    errorprone 'com.google.errorprone:error_prone_core:2.41.0'
    implementation 'com.google.errorprone:error_prone_annotations:2.15.0'
}

//this will hold any command-line auto-patch requests (via -PErrorProneAutoPatchList=[coma-separated-list]).
def errorProneAutoPatch = rootProject.findProperty('ErrorProneAutoPatchList')

afterEvaluate {
    tasks.withType(JavaCompile).configureEach {
        options.errorprone.errorproneArgs.add('-Xep:MixedMutabilityReturnType:OFF')
        options.errorprone.errorproneArgs.add('-Xep:UnnecessaryLambda:OFF')
        options.errorprone.errorproneArgs.add('-Xep:MutablePublicArray:OFF')
        options.errorprone.errorproneArgs.add('-Xep:UnnecessaryAnonymousClass:OFF')
        options.errorprone.errorproneArgs.add('-XepDisableWarningsInGeneratedCode')

        if (name.toLowerCase(Locale.ROOT).contains('test')) {
            options.errorprone.errorproneArgs.add('-Xep:UndefinedEquals:OFF')
        }

        //auto-patch
        if (errorProneAutoPatch != null) {
            options.errorprone {
                errorproneArgs.addAll(
                        "-XepPatchChecks:${errorProneAutoPatch}",
                        "-XepPatchLocation:IN_PLACE"
                )
            }
            options.compilerArgs << '-nowarn'
        } else {
            //fail on warnings (which are Error-Prone issues)
            options.compilerArgs << '-Werror'
        }
    }
}
