<?xml version="1.0" encoding="UTF-8"?>
<!--

    Copyright 2017 The GreyCat Authors.  All rights reserved.
    <p>
    Licensed 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
    <p>
    http://www.apache.org/licenses/LICENSE-2.0
    <p>
    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.

-->
<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>
    <artifactId>greycat</artifactId>
    <name>greycat</name>
    <version>6</version>

    <parent>
        <artifactId>greycat-parent</artifactId>
        <groupId>com.datathings</groupId>
        <version>6</version>
        <relativePath>..</relativePath>
    </parent>

    <properties>
        <js.name>greycat</js.name>
        <header.path>${basedir}/../HEADER</header.path>
    </properties>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.19.1</version>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>2.4</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>test-jar</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>com.datathings</groupId>
                <artifactId>j2ts-mavenplugin</artifactId>
                <version>${java2typescript.plugin.version}</version>
                <executions>
                    <execution>
                        <id>compile-core</id>
                        <phase>compile</phase>
                        <goals>
                            <goal>java2ts</goal>
                        </goals>
                        <configuration>
                            <name>greycat</name>
                            <source>${basedir}/../greycat/src/main/java</source>
                            <target>${basedir}/target/generated-sources</target>
                            <headers>
                                <header>import {java,Long} from 'j2ts-jre';</header>
                            </headers>
                        </configuration>
                    </execution>
                    <execution>
                        <id>compile-test</id>
                        <phase>test-compile</phase>
                        <goals>
                            <goal>java2ts</goal>
                        </goals>
                        <configuration>
                            <name>test</name>
                            <headers>
                                <header>import {java,Long} from 'j2ts-jre';</header>
                                <header>import {org} from 'j2ts-junit';</header>
                                <header>import * as greycat from '../../target/classes-npm';</header>
                            </headers>
                            <testHeaders>
                                <testHeader>var greycatTest = require('../test-classes-npm');</testHeader>
                            </testHeaders>
                            <source>${basedir}/../greycat/src/test/java</source>
                            <target>${basedir}/target/generated-test-sources</target>
                            <withJUnit>true</withJUnit>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

            <plugin>
                <groupId>org.codehaus.gmaven</groupId>
                <artifactId>groovy-maven-plugin</artifactId>
                <version>2.0</version>
                <executions>
                    <execution>
                        <id>main.ts.compile</id>
                        <phase>compile</phase>
                        <goals>
                            <goal>execute</goal>
                        </goals>
                        <configuration>
                            <source>
                                def ant = new AntBuilder()
                                ant.copy(file: "${basedir}/src/main/ts/tsconfig.json", todir: "${basedir}/target/classes-npm")
                                ant.copy(file: "${basedir}/src/main/ts/package.json", todir: "${basedir}/target/classes-npm")
                                def project_version = "${project.version}".replaceAll("-SNAPSHOT", "")
                                while (project_version.split('\\.').length != 3) {
                                    project_version += '.0'
                                }
                                def j2ts_version = "${java2typescript.plugin.version}".replaceAll("-SNAPSHOT", "")
                                while (j2ts_version.split('\\.').length != 3) {
                                    j2ts_version += '.0'
                                }
                                ant.replace(file: "${basedir}/target/classes-npm/package.json", token: "GREYCAT_VERSION", value: project_version)
                                ant.replace(file: "${basedir}/target/classes-npm/package.json", token: "J2TS_VERSION", value: j2ts_version)
                                ant.copy(file: "${basedir}/target/generated-sources/greycat.ts", todir: "${basedir}/target/classes-npm")
                                ant.concat(append: true, destfile: "${basedir}/target/classes-npm/greycat.ts", "export = greycat;")
                                def npmInstallProcess = "npm install".execute(null, new File("${basedir}/target/classes-npm"))
                                npmInstallProcess.waitForProcessOutput(System.out, System.err)
                                if (npmInstallProcess.exitValue() != 0) {
                                    exit
                                }
                                ant.echo("TypeScript Compiler")
                                def tscProcess = "node ${basedir}/target/classes-npm/node_modules/typescript/lib/tsc.js".execute(null, new File("${basedir}/target/classes-npm"))
                                tscProcess.waitForProcessOutput(System.out, System.err)
                                if (tscProcess.exitValue() != 0) {
                                    exit
                                }
                                ant.delete(file: "${basedir}/target/classes-npm/greycat.ts")
                            </source>
                        </configuration>
                    </execution>
                    <execution>
                        <id>test.ts.compile</id>
                        <phase>test-compile</phase>
                        <goals>
                            <goal>execute</goal>
                        </goals>
                        <configuration>
                            <source>
                                def ant = new AntBuilder()
                                ant.copy(file: "${basedir}/src/test/ts/tsconfig.json", todir: "${basedir}/target/test-classes-npm")
                                ant.copy(file: "${basedir}/src/test/ts/package.json", todir: "${basedir}/target/test-classes-npm")
                                def project_version = "${project.version}".replaceAll("-SNAPSHOT", "")
                                while (project_version.split('\\.').length != 3) {
                                    project_version += '.0'
                                }
                                def j2ts_version = "${java2typescript.plugin.version}".replaceAll("-SNAPSHOT", "")
                                while (j2ts_version.split('\\.').length != 3) {
                                    j2ts_version += '.0'
                                }
                                ant.replace(file: "${basedir}/target/test-classes-npm/package.json", token: "GREYCAT_VERSION", value: project_version)
                                ant.replace(file: "${basedir}/target/test-classes-npm/package.json", token: "J2TS_VERSION", value: j2ts_version)
                                ant.copy(file: "${basedir}/target/generated-test-sources/test.ts", todir: "${basedir}/target/test-classes-npm")
                                ant.concat(append: "true", destfile: "${basedir}/target/test-classes-npm/test.ts", "export = greycatTest;")
                                def npmInstallProcess = "npm install".execute(null, new File("${basedir}/target/test-classes-npm"))
                                npmInstallProcess.waitForProcessOutput(System.out, System.err)
                                if (npmInstallProcess.exitValue() != 0) {
                                    exit
                                }
                                def tscProcess = "node ${basedir}/target/test-classes-npm/node_modules/typescript/lib/tsc.js".execute(null, new File("${basedir}/target/test-classes-npm"))
                                tscProcess.waitForProcessOutput(System.out, System.err)
                                if (tscProcess.exitValue() != 0) {
                                    exit
                                }
                                ant.concat(append: "false", destfile: "${basedir}/target/test-classes-npm/jasmine.js", "var Jasmine = require('jasmine');var jasmine = new Jasmine();jasmine.execute(['testsRunner.js']);")
                                ant.copy(file: "${basedir}/target/generated-test-sources/testsRunner.js", todir: "${basedir}/target/test-classes-npm")
                            </source>
                        </configuration>
                    </execution>
                    <execution>
                        <id>test.ts.run</id>
                        <phase>test</phase>
                        <goals>
                            <goal>execute</goal>
                        </goals>
                        <configuration>
                            <source>
                                def jasmineProcess = "node jasmine.js".execute(null, new File("${basedir}/target/test-classes-npm"))
                                jasmineProcess.waitForProcessOutput(System.out, System.err)
                                if (jasmineProcess.exitValue() != 0) {
                                    exit
                                }
                            </source>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

</project>
