<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <parent>
        <groupId>io.cucumber</groupId>
        <artifactId>cucumber-jvm-groovy</artifactId>
        <version>4.7.1</version>
    </parent>

    <artifactId>cucumber-groovy</artifactId>
    <packaging>jar</packaging>
    <name>Cucumber Groovy</name>

    <properties>
        <gmaven.runtime>1.8</gmaven.runtime>
        <jackson.version>2.9.9</jackson.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>io.cucumber</groupId>
            <artifactId>cucumber-core</artifactId>
        </dependency>
        <dependency>
            <groupId>org.codehaus.groovy</groupId>
            <artifactId>groovy-all</artifactId>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.datatype</groupId>
            <artifactId>jackson-datatype-jsr310</artifactId>
            <version>${jackson.version}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>${jackson.version}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>io.cucumber</groupId>
            <artifactId>cucumber-junit</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.mockito</groupId>
            <artifactId>mockito-core</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.codehaus.gmaven</groupId>
                <artifactId>gmaven-plugin</artifactId>
                <executions>
                    <execution>
                        <goals>
                            <goal>generateStubs</goal>
                            <goal>compile</goal>
                            <goal>generateTestStubs</goal>
                            <goal>testCompile</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-clean-plugin</artifactId>
                <configuration>
                    <filesets>
                        <fileset>
                            <directory>bin</directory>
                            <includes>
                                <include>*.jar</include>
                            </includes>
                        </fileset>
                    </filesets>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-shade-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-antrun-plugin</artifactId>
                <executions>
                    <execution>
                        <id>generate-i18n-sources</id>
                        <goals>
                            <goal>run</goal>
                        </goals>
                        <phase>generate-sources</phase>
                        <configuration>
                            <target>
                                <taskdef name="groovy" classname="org.codehaus.groovy.ant.Groovy" classpathref="maven.plugin.classpath" />

                                <groovy><![CDATA[
import groovy.text.SimpleTemplateEngine
import gherkin.GherkinDialect
import gherkin.GherkinDialectProvider

def engine = new SimpleTemplateEngine()
def templateSource = new File(project.baseDir, "src${File.separator}main${File.separator}code_generator${File.separator}I18n.groovy.txt").getText()

def unsupported = ["EM"] // The generated files for Emoij do not compile.
def dialectProvider = new GherkinDialectProvider()

GherkinDialectProvider.DIALECTS.keySet().each { language ->
  def dialect = dialectProvider.getDialect(language, null)
  def normalized_language = dialect.language.replaceAll("[\\s-]", "_").toUpperCase()
  if (!unsupported.contains(normalized_language)) {
    def binding = ["i18n":dialect, "normalized_language":normalized_language]
    template = engine.createTemplate(templateSource).make(binding)
    def file = new File(project.baseDir, "target${File.separator}generated-sources${File.separator}i18n${File.separator}java${File.separator}cucumber${File.separator}api${File.separator}groovy${File.separator}${normalized_language}.java")
    file.parentFile.mkdirs()
    file.write(template.toString(), "UTF-8")
  }
}
        ]]></groovy>

                            </target>
                        </configuration>
                    </execution>
                    <execution>
                        <id>copy-full-jar</id>
                        <phase>package</phase>
                        <configuration>
                            <target>
                                <copy file="${project.build.directory}/${project.build.finalName}-shaded.jar" tofile="${basedir}/bin/cucumber-groovy-shaded.jar" />
                            </target>
                        </configuration>
                        <goals>
                            <goal>run</goal>
                        </goals>
                    </execution>
                    <execution>
                        <id>groovy-cli-test</id>
                        <phase>integration-test</phase>
                        <goals>
                            <goal>run</goal>
                        </goals>
                        <configuration>
                            <target>
                                <!--
                                Also run the CLI test to ensure that stuff works via the CLI as well.
                                This isn't exactly the same as running `groovy bin/cucumber-jvm.groovy`
                                without cucumber-groovy-full.jar on the classpath, but that will work too.
                                -->

                                <echo message="Running groovy tests via the CLI..." />
                                <java classname="groovy.ui.GroovyMain" fork="true" failonerror="true" newenvironment="true" maxmemory="512m" classpathref="maven.test.classpath">
                                    <classpath>
                                        <pathelement location="${maven.dependency.org.codehaus.groovy.groovy-all.jar.path}" />
                                        <pathelement location="${maven.dependency.junit.junit.jar.path}" />
                                        <pathelement location="${basedir}/bin/cucumber-groovy-shaded.jar" />
                                        <pathelement location="target/test-classes" />
                                    </classpath>
                                    <arg value="bin/cucumber-jvm.groovy" />
                                    <arg value="--strict" />

                                    <!--
                                    target/test-classes/cucumber/runtime/groovy contains:

                                    * compiled groovy stepdefs (compiled_stepdefs$_run_closure1.class etc)
                                    * source groovy stepdefs (interpreted_stepdefs.groovy etc)
                                    * feature filkes (a_feature.feature etc)

                                    -->
                                    <arg value="--glue" />
                                    <arg value="classpath:cucumber/runtime/groovy" />

                                    <arg value="classpath:cucumber/runtime/groovy" />
                                </java>

                            </target>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>build-helper-maven-plugin</artifactId>
                <executions>
                    <execution>
                        <id>add-source</id>
                        <phase>generate-sources</phase>
                        <goals>
                            <goal>add-source</goal>
                        </goals>
                        <configuration>
                            <sources>
                                <source>${basedir}/target/generated-sources/i18n/java</source>
                            </sources>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <configuration>
                    <includes>
                        <include>**/*.java</include>
                    </includes>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>
