<?xml version="1.0" encoding="UTF-8"?>
<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>
        <artifactId>quickbuf-parent</artifactId>
        <groupId>us.hebi.quickbuf</groupId>
        <version>1.0-alpha8</version>
    </parent>

    <artifactId>quickbuf-runtime</artifactId>

    <properties>
        <maven.compiler.source>6</maven.compiler.source>
        <maven.compiler.target>6</maven.compiler.target>
        <apiDirectory>us/hebi/quickbuf</apiDirectory>
        <copyFromDir>${basedir}/src/main/java/${apiDirectory}</copyFromDir>
        <copyToDir>${basedir}/target/generated-sources/${apiDirectory}</copyToDir>
        <javaModuleName>us.hebi.quickbuf.runtime</javaModuleName>
    </properties>

    <dependencies>
        <dependency>
            <groupId>com.google.protobuf</groupId>
            <artifactId>protobuf-java</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>

            <!-- Copy RepeatedFloat implementation to other types. Use float because it's unlikely to collide -->
            <plugin>
                <artifactId>maven-antrun-plugin</artifactId>
                <executions>
                    <execution>
                        <id>generate-repeated-primitives</id>
                        <phase>generate-sources</phase>
                        <goals>
                            <goal>run</goal>
                        </goals>
                        <configuration>
                            <tasks>
                                <mkdir dir="${copyToDir}"/>

                                <copy file="${copyFromDir}/RepeatedFloat.java" tofile="${copyToDir}/RepeatedDouble.java" />
                                <replace file="${copyToDir}/RepeatedDouble.java" token="Float" value="Double"/>
                                <replace file="${copyToDir}/RepeatedDouble.java" token="float" value="double"/>

                                <copy file="${copyFromDir}/RepeatedFloat.java" tofile="${copyToDir}/RepeatedLong.java" />
                                <replace file="${copyToDir}/RepeatedLong.java" token="Float" value="Long"/>
                                <replace file="${copyToDir}/RepeatedLong.java" token="float" value="long"/>

                                <copy file="${copyFromDir}/RepeatedFloat.java" tofile="${copyToDir}/RepeatedInt.java" />
                                <replace file="${copyToDir}/RepeatedInt.java" token="RepeatedFloat" value="RepeatedInt"/>
                                <replace file="${copyToDir}/RepeatedInt.java" token="Float" value="Integer"/>
                                <replace file="${copyToDir}/RepeatedInt.java" token="float" value="int"/>

                                <copy file="${copyFromDir}/RepeatedFloat.java" tofile="${copyToDir}/RepeatedBoolean.java" />
                                <replace file="${copyToDir}/RepeatedBoolean.java" token="Float" value="Boolean"/>
                                <replace file="${copyToDir}/RepeatedBoolean.java" token="float" value="boolean"/>

                                <!-- the 'bytes' type is really the same as a repeated byte in disguise -->
                                <copy file="${copyFromDir}/RepeatedFloat.java" tofile="${copyToDir}/RepeatedByte.java" />
                                <replace file="${copyToDir}/RepeatedByte.java" token="Float" value="Byte"/>
                                <replace file="${copyToDir}/RepeatedByte.java" token="float" value="byte"/>

                            </tasks>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>build-helper-maven-plugin</artifactId>
                <executions>
                    <execution>
                        <phase>generate-sources</phase>
                        <goals>
                            <goal>add-source</goal>
                        </goals>
                        <configuration>
                            <sources>
                                <source>${copyToDir}</source>
                            </sources>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

            <!-- Generate sources via plugin -->
            <plugin>
                <groupId>com.github.os72</groupId>
                <artifactId>protoc-jar-maven-plugin</artifactId>
                <version>3.8.0</version>
                <executions>
                    <execution>
                        <phase>generate-test-sources</phase>
                        <goals>
                            <goal>run</goal>
                        </goals>
                        <configuration>
                            <protocVersion>${protoc.version}</protocVersion>

                            <inputDirectories>
                                <include>${proto.dir}/</include>
                            </inputDirectories>

                            <outputTargets>

                                <!-- Generate sources (override package so we don't have name collisions with gen-java) -->
                                <outputTarget>
                                    <pluginPath>${project.parent.basedir}
                                    </pluginPath> <!-- for some reason full file paths get omitted, so we rely on forwarding scripts that are in the path :( -->
                                    <type>quickbuf</type>
                                    <outputOptions>replace_package=(protos.test.protobuf)=protos.test.quickbuf,indent=4,store_unknown_fields=true,json_use_proto_name=false,enforce_has_checks=false
                                    </outputOptions>
                                    <addSources>test</addSources>
                                </outputTarget>

                                <!-- Generate Java sources for sanity checks -->
                                <outputTarget>
                                    <type>java</type>
                                    <addSources>test</addSources>
                                </outputTarget>

                            </outputTargets>

                        </configuration>
                    </execution>
                </executions>
            </plugin>

            <!-- Plugins required for publishing to Maven Central -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-source-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-javadoc-plugin</artifactId>
            </plugin>

        </plugins>
    </build>

</project>