Antlr4 - generate grammar source for more language in gradle
Asked Answered
O

0

7

In my project I have to generate the grammar sources for more than one language (Java, Javascript and Python) using gradle.

I'm using the antlr plugin, so I have the following rows in my build.gradle file:

apply plugin: 'antlr'

generateGrammarSource {
    def languageFlag = project.hasProperty('Language') ?project.property('Language') : 'Python2'

    arguments = ['-Dlanguage=' + languageFlag]

    def pythonOutputDirectory = "python/engine_lib/kpi_attributes"
    switch (languageFlag) {
        case "Java":    
            outputDirectory = file("../../../../XSpotterGUI/sviluppo/src/com/xech/xspotter4/grammars/kpiattributes")
            arguments += ['-package', 'com.xech.xspotter4.grammars.kpiattributes']
            break
        case "JavaScript":  
            outputDirectory = file("../../../../XSpotterGUI/sviluppo/WebContent/xspotter4/js/xech/grammars/kpiattributes")
            break
        case "Python2":
            outputDirectory = file(pythonOutputDirectory)
            break
    }
    description = 'Generates Java sources from Antlr4 grammars.'
    maxHeapSize = "64m"
    sourceSets.main.antlr.srcDirs = ['.']

    includes = ['KpiAttributes.g4']

    doLast {
        if (languageFlag.equals("Python2")) {
            File file = new File("$pythonOutputDirectory/__init__.py")
            file.write ""
        }    
    }
}

I omitted the rows regarding repositories, dependencies and so on.

In this way I'm able to call gradle three times:

./gradlew generateGrammarSource -PLanguage=Java
./gradlew generateGrammarSource -PLanguage=Python2
./gradlew generateGrammarSource -PLanguage=JavaScript

But I have not been able to create a task 'generateAllGrammarSources' in order to call gradlew only ONE time and generate all sources

Outbound answered 28/5, 2018 at 14:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.