<?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.4</version>
    </parent>

    <artifactId>quickbuf-runtime</artifactId>

    <properties>
        <javaModuleName>us.hebi.quickbuf.runtime</javaModuleName>
        <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>
        <generatedSourcesDir>${basedir}/target/generated-sources/annotations/</generatedSourcesDir>
        <copyToDir>${generatedSourcesDir}/${apiDirectory}</copyToDir>
    </properties>

    <dependencies>
        <dependency>
            <groupId>com.google.protobuf</groupId>
            <artifactId>protobuf-java</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>com.google.protobuf</groupId>
            <artifactId>protobuf-java-util</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>

            <!-- create a test jar that we can reuse for tests in other modules -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>3.3.0</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>test-jar</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>

            <!-- Don't compile sources that we replace with a pre-compiled class file -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.11.0</version>
                <configuration>
                    <excludes>
                        <exclude>**/us/hebi/quickbuf/JdkMethods.java</exclude>
                    </excludes>
                </configuration>
            </plugin>

            <plugin>
                <artifactId>maven-antrun-plugin</artifactId>
                <version>3.1.0</version>
                <executions>

                    <!-- Replace Java9+ sources with pre-compiled class files -->
                    <execution>
                        <id>add-java9-classes</id>
                        <phase>generate-resources</phase> <!-- after 'compile' -->
                        <goals>
                            <goal>run</goal>
                        </goals>
                        <configuration>
                            <target>
                                <copy file="src/java9/JdkMethods.class" tofile="${project.build.outputDirectory}/us/hebi/quickbuf/JdkMethods.class" overwrite="true" />
                            </target>
                        </configuration>
                    </execution>

                    <!-- Copy RepeatedFloat implementation to other types. Use float because it's unlikely to collide -->
                    <execution>
                        <id>generate-repeated-primitives</id>
                        <phase>generate-sources</phase>
                        <goals>
                            <goal>run</goal>
                        </goals>
                        <configuration>
                            <target>
                                <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" overwrite="true"/>
                                <replace file="${copyToDir}/RepeatedByte.java" token="Float" value="Byte"/>
                                <replace file="${copyToDir}/RepeatedByte.java" token="float" value="byte"/>

                                <!-- Add a few extra methods for simplifying JSON writes -->
                                <replace file="${copyToDir}/RepeatedByte.java" token="} // RepeatedByte" value=""/>
                                <concat destfile="${copyToDir}/RepeatedByte.java" append="true">
                                    <fileset file="src/test/resources/concat/RepeatedByte.txt" />
                                </concat>

                            </target>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>build-helper-maven-plugin</artifactId>
                <version>3.4.0</version>
                <executions>
                    <execution>
                        <phase>generate-sources</phase>
                        <goals>
                            <goal>add-source</goal>
                        </goals>
                        <configuration>
                            <sources>
                                <source>${generatedSourcesDir}</source>
                            </sources>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

            <!-- Generate test sources via protobuf-plugin -->
            <plugin>
                <groupId>com.github.os72</groupId>
                <artifactId>protoc-jar-maven-plugin</artifactId>
                <version>${protoc.jar.version}</version>
                <configuration>
                    <!-- don't cache because it breaks multi-executions -->
                    <optimizeCodegen>false</optimizeCodegen>
                </configuration>
                <executions>
                    <execution>
                        <id>generate-eager-protos</id>
                        <phase>generate-test-sources</phase>
                        <goals>
                            <goal>run</goal>
                        </goals>
                        <configuration>
                            <protocVersion>${protobuf.version}</protocVersion>

                            <inputDirectories>
                                <include>${proto.dir}/</include>
                            </inputDirectories>

                            <outputTargets>

                                <!-- Generate sources (override package so we don't have name collisions with gen-java) -->
                                <outputTarget>
                                    <!-- Linux finds absolute file, Windows finds it via executing PATH -->
                                    <pluginPath>${protoc.quickbufPlugin}</pluginPath>
                                    <type>quickbuf</type>
                                    <outputOptions>
                                        replace_package=(protobuf)=quickbuf,indent=4,store_unknown_fields=true,enforce_has_checks=false,input_order=quickbuf,allocation=eager,gen_descriptors=true
                                    </outputOptions>
                                    <addSources>test</addSources>
                                </outputTarget>

                                <!-- Generate Java sources for sanity checks -->
                                <outputTarget>
                                    <type>java</type>
                                    <addSources>test</addSources>
                                </outputTarget>

                            </outputTargets>

                        </configuration>
                    </execution>
                    <execution>
                        <id>generate-lazy-protos</id>
                        <phase>generate-test-sources</phase>
                        <goals>
                            <goal>run</goal>
                        </goals>
                        <configuration>
                            <protocVersion>${protobuf.version}</protocVersion>

                            <inputDirectories>
                                <include>${proto.dir}/../lazy/</include>
                            </inputDirectories>

                            <outputTargets>
                                <!-- Generate sources with lazy option -->
                                <outputTarget>
                                    <pluginPath>${protoc.quickbufPlugin}</pluginPath>
                                    <type>quickbuf</type>
                                    <outputOptions>
                                        replace_package=(protobuf)=quickbuf,indent=4,store_unknown_fields=true,enforce_has_checks=true,allocation=lazy,extensions=embedded,gen_descriptors=true
                                    </outputOptions>
                                    <addSources>test</addSources>
                                </outputTarget>
                            </outputTargets>

                        </configuration>
                    </execution>
                </executions>
            </plugin>

        </plugins>
    </build>

    <profiles>
        <!-- Increases the language level for recent jdks to allow compiling. Release with jdk 8 -->
        <profile>
            <id>jdk17compat</id>
            <activation>
                <jdk>(11,)</jdk>
            </activation>
            <properties>
                <maven.compiler.source>8</maven.compiler.source>
                <maven.compiler.target>8</maven.compiler.target>
            </properties>
        </profile>
    </profiles>

</project>