/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements. See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership. The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License. You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

// Following Gradle best practices to keep build logic organized

// Generated code locations for Unit tests
ext.genSrc = file("$buildDir/gen-java")
ext.genBeanSrc = file("$buildDir/gen-javabean")
ext.genReuseSrc = file("$buildDir/gen-javareuse")
ext.genFullCamelSrc = file("$buildDir/gen-fullcamel")
ext.genOptionTypeJdk8Src = file("$buildDir/gen-option-type-jdk8")
ext.genUnsafeSrc = file("$buildDir/gen-unsafe")

// Add the generated code directories to the test source set
sourceSets {
    test.java.srcDirs genSrc, genBeanSrc, genReuseSrc, genFullCamelSrc, genUnsafeSrc, genOptionTypeJdk8Src
}

// ----------------------------------------------------------------------------
// Code generation for Unit Testing

// A callable closure to make this easier
ext.thriftCompile = { Task task, String thriftFileName, String generator = 'java', File outputDir = genSrc ->
    def thriftFile = file("$thriftRoot/test/$thriftFileName")
    if (!thriftFile.exists()) {
        thriftFile = file("$projectDir/src/test/resources/$thriftFileName")
        assert thriftFile.exists(), "can't find $thriftFile"
    }

    task.inputs.file thriftFile
    task.outputs.dir outputDir

    task.doLast {
        outputDir.mkdirs()
        def result = exec {
            executable file(thriftCompiler)
            args '--gen', generator
            args '-out', outputDir
            args thriftFile
            standardOutput = task.outputBuffer
            errorOutput = task.outputBuffer
            ignoreExitValue = true
        }
        if (result.exitValue != 0) {
            // Only show the Thrift compiler output on failures, cuts down on noise!
            println task.outputBuffer.toString()
            result.rethrowFailure()
        }
    }
}

task generate(group: 'Build') {
    description = 'Generate all unit test Thrift sources'
    compileTestJava.dependsOn it
}

task generateJava(group: 'Build') {
    description = 'Generate the thrift gen-java source'
    generate.dependsOn it

    ext.outputBuffer = new ByteArrayOutputStream()

    thriftCompile(it, 'ThriftTest.thrift')
    thriftCompile(it, 'JavaTypes.thrift')
    thriftCompile(it, 'DebugProtoTest.thrift')
    thriftCompile(it, 'DoubleConstantsTest.thrift')
    thriftCompile(it, 'OptionalRequiredTest.thrift')
    thriftCompile(it, 'ManyOptionals.thrift')
    thriftCompile(it, 'JavaDeepCopyTest.thrift')
    thriftCompile(it, 'EnumContainersTest.thrift')
    thriftCompile(it, 'JavaBinaryDefault.thrift')
    thriftCompile(it, 'VoidMethExceptionsTest.thrift')
    thriftCompile(it, 'AnnotationTest.thrift')
    thriftCompile(it, 'partial/thrift_test_schema.thrift')
}

task generateOptionalTypeJava(group: 'Build') {
    description = 'Generate the thrift gen-option-type-jdk8 source'
    generate.dependsOn it

    ext.outputBuffer = new ByteArrayOutputStream()

    thriftCompile(it, 'JavaOptionTypeJdk8Test.thrift', 'java:option_type=jdk8', genOptionTypeJdk8Src)
}

task generateBeanJava(group: 'Build') {
    description = 'Generate the thrift gen-javabean source'
    generate.dependsOn it

    ext.outputBuffer = new ByteArrayOutputStream()

    thriftCompile(it, 'JavaBeansTest.thrift', 'java:beans,nocamel,future_iface', genBeanSrc)
}

task generateReuseJava(group: 'Build') {
    description = 'Generate the thrift gen-javareuse source'
    generate.dependsOn it

    ext.outputBuffer = new ByteArrayOutputStream()

    thriftCompile(it, 'FullCamelTest.thrift', 'java:fullcamel,future_iface', genFullCamelSrc)
}

task generateFullCamelJava(group: 'Build') {
    description = 'Generate the thrift gen-fullcamel source'
    generate.dependsOn it

    ext.outputBuffer = new ByteArrayOutputStream()

    thriftCompile(it, 'ReuseObjects.thrift', 'java:reuse_objects', genReuseSrc)
}

task generateUnsafeBinariesJava(group: 'Build') {
    description = 'Generate the thrift gen-unsafebinaries source'
    generate.dependsOn it

    ext.outputBuffer = new ByteArrayOutputStream()

    thriftCompile(it, 'UnsafeTypes.thrift', 'java:unsafe_binaries', genUnsafeSrc)
}

task generateWithAnnotationMetadata(group: 'Build') {
    description = 'Generate with annotation enabled and add to the default source'
    generate.dependsOn it

    ext.outputBuffer = new ByteArrayOutputStream()

    thriftCompile(it, 'JavaAnnotationTest.thrift', 'java:annotations_as_metadata', genSrc)
}
