<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>info.cukes</groupId>
        <artifactId>cucumber-jvm</artifactId>
        <relativePath>../pom.xml</relativePath>
        <version>1.2.4</version>
    </parent>

    <artifactId>cucumber-jruby</artifactId>
    <packaging>jar</packaging>
    <name>Cucumber-JVM: JRuby</name>

    <dependencies>
        <dependency>
            <groupId>info.cukes</groupId>
            <artifactId>cucumber-core</artifactId>
        </dependency>
        <dependency>
            <groupId>info.cukes</groupId>
            <artifactId>cucumber-jvm-deps</artifactId>
        </dependency>
        <dependency>
            <groupId>info.cukes</groupId>
            <artifactId>gherkin</artifactId>
        </dependency>
        <dependency>
            <groupId>org.jruby</groupId>
            <artifactId>jruby-complete</artifactId>
            <scope>provided</scope>
        </dependency>

        <dependency>
            <groupId>info.cukes</groupId>
            <artifactId>cucumber-junit</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>net.sourceforge.cobertura</groupId>
            <artifactId>cobertura</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-clean-plugin</artifactId>
                <configuration>
                    <filesets>
                        <fileset>
                            <directory>lib</directory>
                            <includes>
                                <include>**/*.*</include>
                            </includes>
                        </fileset>
                        <fileset>
                            <directory>.</directory>
                            <includes>
                                <include>*.gem</include>
                            </includes>
                        </fileset>
                    </filesets>
                </configuration>
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <configuration>
                    <environmentVariables>
                        <!-- https://github.com/cucumber/cucumber-jvm/issues/718 -->
                        <RUBY_VERSION>1.9.3</RUBY_VERSION>
                    </environmentVariables>
                </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[
def template = """\
module Cucumber
  module Runtime
    module JRuby
      module Dsl
<% i18n.codeKeywords.each { kw -> %>
        def \${java.text.Normalizer.normalize(kw, java.text.Normalizer.Form.NFC)}(regexp, &proc)
          register_stepdef(regexp, proc)
        end
<% } %>
      end
    end
  end
end
"""

gherkin.I18n.all.each { i18n ->
    def engine = new groovy.text.SimpleTemplateEngine()
    def binding = ["i18n":i18n]
    def source = engine.createTemplate(template).make(binding).toString()

    def file = new File(project.baseDir, "target${File.separator}generated-resources${File.separator}i18n${File.separator}cucumber${File.separator}api${File.separator}jruby${File.separator}${i18n.underscoredIsoCode}.rb")
    file.parentFile.mkdirs()
    file.write(source, "UTF-8")
}

        ]]></groovy>

                            </target>
                        </configuration>
                    </execution>
                    <execution>
                        <id>package-gem</id>
                        <phase>package</phase>
                        <configuration>
                            <target>
                                <!-- add jars to lib -->
                                <copy file="${project.build.directory}/${project.build.finalName}-shaded.jar" tofile="${basedir}/lib/cucumber-jruby-shaded.jar" />

                                <!-- build the gem file using jruby -->
                                <echo message="Building Gem" />
                                <java jar="${maven.dependency.org.jruby.jruby-complete.jar.path}" failonerror="true" fork="true">
                                    <!-- clobber PATH to prevent `gem` on the PATH from being picked up -->
                                    <env key="PATH" value="" />
                                    <sysproperty key="cucumber-jvm.version" value="${project.version}" />
                                    <arg value="-S" />
                                    <arg value="gem" />
                                    <arg value="build" />
                                    <arg value="cucumber-jvm.gemspec" />
                                </java>
                            </target>
                        </configuration>
                        <goals>
                            <goal>run</goal>
                        </goals>
                    </execution>
                    <execution>
                        <id>jruby-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 -->
                                <echo message="Running cucumber via the CLI..." />
                                <java jar="${maven.dependency.org.jruby.jruby-complete.jar.path}" fork="true" failonerror="true" newenvironment="true" maxmemory="512m">
                                    <env key="PATH" value="" />
                                    <arg value="-S" />
                                    <arg value="bin/cucumber-jvm" />
                                    <arg value="--glue" />
                                    <arg value="src/test/resources" />
                                    <arg value="src/test/resources" />
                                </java>
                            </target>
                        </configuration>
                    </execution>
                    <execution>
                        <id>deploy-gem</id>
                        <phase>deploy</phase>
                        <configuration>
                            <target unless="no.gem.deploy">
                                <!-- install the jruby-openssl gem first! -->
                                <echo message="installing jruby-openssl..." />
                                <java jar="${maven.dependency.org.jruby.jruby-complete.jar.path}" failonerror="true" fork="true">
                                    <env key="PATH" value="" />
                                    <arg value="-S" />
                                    <arg value="gem" />
                                    <arg value="install" />
                                    <arg value="jruby-openssl" />
                                    <arg value="--no-rdoc" />
                                    <arg value="--no-ri" />
                                    <arg value="-i" />
                                    <arg value="${basedir}/target/gems" />
                                </java>

                                <!-- publish the gem to rubygems.org -->
                                <echo message="Publishing Gem" />
                                <java jar="${maven.dependency.org.jruby.jruby-complete.jar.path}" failonerror="true" fork="true">
                                    <env key="PATH" value="" />
                                    <env key="GEM_PATH" value="${basedir}/target/gems" />
                                    <arg value="-S" />
                                    <arg value="gem" />
                                    <arg value="push" />
                                    <arg value="cucumber-jvm-${project.parent.version}-java.gem" />
                                </java>
                            </target>
                        </configuration>
                        <goals>
                            <goal>run</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>

            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>build-helper-maven-plugin</artifactId>
                <executions>
                    <execution>
                        <id>add-resource</id>
                        <phase>generate-resources</phase>
                        <goals>
                            <goal>add-resource</goal>
                        </goals>
                        <configuration>
                            <resources>
                                <resource>
                                    <directory>${basedir}/target/generated-resources/i18n</directory>
                                </resource>
                            </resources>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>
