<?xml version="1.0"?>
<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/maven-v4_0_0.xsd">
    <parent>
        <groupId>org.neo4j</groupId>
        <artifactId>cypher-v25-parser-parent</artifactId>
        <version>5.26.10</version>
        <relativePath>../pom.xml</relativePath>
    </parent>

    <modelVersion>4.0.0</modelVersion>
    <artifactId>cypher-v25-antlr-parser</artifactId>
    <packaging>jar</packaging>
    <name>Cypher 25 ANTLR based parser</name>

    <description>Cypher 25 ANTLR based parser</description>

    <properties>
        <moduleName>org.neo4j.cypher.internal.parser.antlr4</moduleName>
        <spotless.check.skip>true</spotless.check.skip>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.antlr</groupId>
            <artifactId>antlr4-runtime</artifactId>
        </dependency>
        <dependency>
            <groupId>org.neo4j</groupId>
            <artifactId>cypher-antlr-common</artifactId>
            <version>${project.version}</version>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter</artifactId>
            <version>${junit.version}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.assertj</groupId>
            <artifactId>assertj-core</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.antlr</groupId>
                <artifactId>antlr4-maven-plugin</artifactId>
                <version>${antlr.version}</version>
                <executions>
                    <execution>
                        <id>antlr</id>
                        <phase>generate-sources</phase>
                        <goals>
                            <goal>antlr4</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <options>
                        <contextSuperClass>org.neo4j.cypher.internal.parser.AstRuleCtx</contextSuperClass>
                    </options>
                    <listener>false</listener> <!-- We have our own optimised listener -->
                    <visitor>false</visitor>
                    <outputDirectory>${basedir}/src/main/java/</outputDirectory>
                    <treatWarningsAsErrors>true</treatWarningsAsErrors>
                </configuration>
            </plugin>
            <plugin>
                <groupId>com.mycila</groupId>
                <artifactId>license-maven-plugin</artifactId>
                <executions>
                    <execution>
                        <id>check-licenses</id>
                        <goals>
                            <goal>format</goal>
                        </goals>
                        <phase>process-sources</phase>
                        <configuration>
                            <skip>false</skip> <!-- Never skip license:format here since these files are generated -->
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>com.diffplug.spotless</groupId>
                <artifactId>spotless-maven-plugin</artifactId>
                <configuration>
                    <java>
                        <excludes>
                            <!-- These files causes stack overflow -->
                            <exclude>src/main/java/org/neo4j/cypher/internal/parser/v25/*.java</exclude>
                        </excludes>
                    </java>
                </configuration>
            </plugin>
            <plugin>
                <!-- disable scala-maven-plugin, because we have no scala -->
                <groupId>net.alchim31.maven</groupId>
                <artifactId>scala-maven-plugin</artifactId>
                <executions>
                    <execution>
                        <id>scala-compile</id>
                        <phase>none</phase>
                    </execution>
                </executions>
                <configuration>
                    <skip>true</skip>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-enforcer-plugin</artifactId>
                <executions>
                    <execution>
                        <id>default</id>
                        <goals>
                            <goal>enforce</goal>
                        </goals>
                        <configuration>
                            <rules>
                                <!-- Antlr generates start imports so ignore it -->
                                <RestrictImports>
                                    <excludedSourceRoot>${basedir}/src/main/java/</excludedSourceRoot>
                                </RestrictImports>
                            </rules>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-assembly-plugin</artifactId>
                <configuration>
                    <encoding>UTF-8</encoding>
                    <appendAssemblyId>true</appendAssemblyId>
                </configuration>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <id>grammar</id>
                        <goals>
                            <goal>single</goal>
                        </goals>
                        <configuration>
                            <descriptors>
                                <descriptor>src/main/assembly/grammar.xml</descriptor>
                            </descriptors>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

    <profiles>
        <!-- Profile to ignore check for changes to generated parser. -->
        <profile>
            <id>check-generated-antlr</id>
            <activation>
                <property>
                    <name>!generate.antlr</name>
                </property>
            </activation>
            <build>
                <plugins>
                    <plugin>
                        <inherited>false</inherited>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-antrun-plugin</artifactId>
                        <executions>
                            <execution>
                                <!-- Copy the old generated parser files to target, see next step. -->
                                <id>copy-generated-antlr</id>
                                <phase>initialize</phase>
                                <goals>
                                    <goal>run</goal>
                                </goals>
                                <configuration>
                                    <target>
                                        <delete dir="${basedir}/target/old-generated-antlr"/>
                                        <copy todir="${basedir}/target/old-generated-antlr">
                                            <fileset dir="${basedir}/src/main/java" includes="**/*.java"/>
                                        </copy>
                                    </target>
                                </configuration>
                            </execution>
                            <execution>
                                <!-- Fail build if generated antlr parser files have changed.
                                     To prevent changes to grammar or antlr version without re-generating and checking in the generated files.
                                     Needed because we check in the generated parser in VCS. -->
                                <id>check-generated-antlr</id>
                                <phase>verify</phase>
                                <goals>
                                    <goal>run</goal>
                                </goals>
                                <configuration>
                                    <target>
                                        <property name="parser-old-path" value="${basedir}/target/old-generated-antlr/org/neo4j/cypher/internal/parser/v25/Cypher25Parser.java"/>
                                        <property name="parser-new-path" value="${basedir}/src/main/java/org/neo4j/cypher/internal/parser/v25/Cypher25Parser.java"/>
                                        <property name="lexer-old-path" value="${basedir}/target/old-generated-antlr/org/neo4j/cypher/internal/parser/v25/Cypher25Lexer.java"/>
                                        <property name="lexer-new-path" value="${basedir}/src/main/java/org/neo4j/cypher/internal/parser/v25/Cypher25Lexer.java"/>

                                        <!-- Uncomment to debug failures

                                        <exec executable="diff">
                                            <arg value="-s" />
                                            <arg value="${parser-old-path}" />
                                            <arg value="${parser-new-path}" />
                                            <redirector output="parser-diff.txt"/>
                                        </exec>
                                        <loadfile property="parser-diff" srcFile="parser-diff.txt" />
                                        <echo>diff -s ${parser-old-path} ${parser-new-path}</echo>
                                        <echo>${parser-diff}</echo>

                                        <exec executable="diff">
                                            <arg value="-s" />
                                            <arg value="${lexer-old-path}" />
                                            <arg value="${lexer-new-path}" />
                                            <redirector output="lexer-diff.txt"/>
                                        </exec>
                                        <loadfile property="lexer-diff" srcFile="lexer-diff.txt" />
                                        <echo>diff -s ${lexer-old-path} ${lexer-new-path}</echo>
                                        <echo>${lexer-diff}</echo>
                                        -->

                                        <fail message="Generated parser files have changed, please re-generate the parser (build with -Dgenerate.antlr) and commit changes to git.">
                                            <condition>
                                                <not>
                                                    <and>
                                                        <filesmatch textfile="true" file1="${parser-old-path}" file2="${parser-new-path}" />
                                                        <filesmatch textfile="true" file1="${lexer-old-path}" file2="${lexer-new-path}" />
                                                    </and>
                                                </not>
                                            </condition>
                                        </fail>
                                    </target>
                                </configuration>
                            </execution>
                        </executions>
                    </plugin>
                </plugins>
            </build>
        </profile>
    </profiles>
</project>