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

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

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

    <dependencies>
        <dependency>
            <groupId>io.undertow</groupId>
            <artifactId>undertow-core</artifactId>
            <version>${undertow.version}</version>
        </dependency>
        <dependency>
            <groupId>com.datathings</groupId>
            <artifactId>greycat</artifactId>
            <version>${project.parent.version}</version>
        </dependency>
        <dependency>
            <groupId>com.datathings</groupId>
            <artifactId>greycat</artifactId>
            <version>${project.version}</version>
            <type>test-jar</type>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <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/greycat.ws.ts", todir: "${basedir}/target/classes-npm")
                                ant.copy(file: "${basedir}/src/main/ts/package.json", todir: "${basedir}/target/classes-npm")
                                ant.copy(file: "${basedir}/src/main/ts/tsconfig.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}/src/main/ts/readme.md", todir: "${basedir}/target/classes-npm")
                                def linkGreycatProcess = "npm install ${basedir}/../../greycat/target/classes-npm".execute(null, new File("${basedir}/target/classes-npm"))
                                linkGreycatProcess.waitForProcessOutput(System.out, System.err)
                                if (linkGreycatProcess.exitValue() != 0) { exit }
                                def npmInstallProcess = "npm install".execute(null, new File("${basedir}/target/classes-npm"))
                                npmInstallProcess.waitForProcessOutput(System.out, System.err)
                                if (npmInstallProcess.exitValue() != 0) { exit }
                                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.ws.ts")
                            </source>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

</project>
