/*
 *  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.
 */

apply plugin: 'antlr'

sourceSets {
    main {
        antlr {
            srcDirs = ['src/antlr']
        }
    }
}

def srcBase = "subprojects/parser-antlr4/src"
def srcMain = "$srcBase/main"
def srcTest = "$srcBase/test"

generateGrammarSource {
    maxHeapSize = '1g'
    outputs.cacheIf { true }
    outputDirectory = file("$generatedDirectory/antlr4")
    sourceSets.main.java.srcDirs += outputDirectory

    final PARSER_PACKAGE_NAME = 'org.apache.groovy.parser.antlr4'
    arguments += ["-visitor", "-no-listener", "-package", PARSER_PACKAGE_NAME]

    doLast {
        def parserFilePattern = 'Groovy*'
        def outputPath = generateGrammarSource.outputDirectory.canonicalPath
        def parserPackagePath = "${outputPath}/${PARSER_PACKAGE_NAME.replace('.', '/')}"
        file(parserPackagePath).mkdirs()
        copy {
            from outputPath
            into parserPackagePath
            include parserFilePattern
        }
        delete fileTree(outputPath) {
            include parserFilePattern
        }
    }
}

configurations {
    implementation {
        extendsFrom = extendsFrom.findAll { it != configurations.antlr2 }
    }
}

dependencies {
    antlr "com.tunnelvisionlabs:antlr4:$antlr4Version"
    implementation "com.tunnelvisionlabs:antlr4-runtime:$antlr4Version"
    testImplementation project(':groovy-test')
}

sourceSets.main.java.srcDirs += file("$srcMain/java")
sourceSets.main.groovy.srcDirs += file("$srcMain/groovy")
sourceSets.main.resources.srcDirs += file("$srcMain/resources")
sourceSets.test.java.srcDirs += file("$srcTest/java")
sourceSets.test.groovy.srcDirs += file("$srcTest/groovy")
sourceSets.test.resources.srcDirs += file("$srcTest/resources")

tasks.withType(Test) {
    jvmArgs /*"-Dgroovy.attach.groovydoc=true", "-Dgroovy.attach.runtime.groovydoc=true",*/ "-Dgroovy.antlr4.cache.threshold=100"
}
