<?xml version="1.0" encoding="UTF-8"?>
<!--
  ~ Copyright 2019 Red Hat, Inc. and/or its affiliates.
  ~
  ~ 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
  ~
  ~     http://www.apache.org/licenses/LICENSE-2.0
  ~
  ~ 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>

  <parent>
    <groupId>org.optaweb.vehiclerouting</groupId>
    <artifactId>optaweb-vehicle-routing</artifactId>
    <version>7.34.0-SNAPSHOT</version>
  </parent>

  <artifactId>optaweb-vehicle-routing-frontend</artifactId>
  <packaging>war</packaging>

  <name>OptaWeb Vehicle Routing Frontend</name>

  <properties>
    <sonar.sources>src</sonar.sources>
    <sonar.test.inclusions>**/*test.ts,**/*test.tsx</sonar.test.inclusions>
    <sonar.javascript.lcov.reportPaths>coverage/lcov.info</sonar.javascript.lcov.reportPaths>

    <!-- Frontend properties -->
    <node.version>v11.6.0</node.version>
    <npm.version>6.9.0</npm.version>
  </properties>

  <build>
    <finalName>optaweb-vehicle-routing-frontend</finalName>
    <plugins>
      <plugin>
        <groupId>com.github.eirslett</groupId>
        <artifactId>frontend-maven-plugin</artifactId>
        <executions>
          <execution>
            <id>install node and npm</id>
            <phase>initialize</phase>
            <goals>
              <goal>install-node-and-npm</goal>
            </goals>
            <configuration>
              <nodeVersion>${node.version}</nodeVersion>
              <npmVersion>${npm.version}</npmVersion>
            </configuration>
          </execution>
          <execution>
            <id>npm install lock-treatment-tool and run-node</id>
            <phase>initialize</phase>
            <goals>
              <goal>npm</goal>
            </goals>
            <configuration>
              <arguments>install lock-treatment-tool --global-style --no-package-lock --no-save</arguments>
            </configuration>
          </execution>
          <execution>
            <id>lock-treatment-tool execution</id>
            <phase>compile</phase>
            <goals>
              <goal>npm</goal>
            </goals>
            <configuration>
              <arguments>run locktt -- </arguments>
            </configuration>
          </execution>
          <execution>
            <id>npm install</id>
            <!--
              Using compile phase so that `npm install` doesn't run during sonar analysis,
              which is bound to generate-resources in kie-parent.
            -->
            <phase>compile</phase>
            <goals>
              <goal>npm</goal>
            </goals>
            <configuration>
              <arguments>install</arguments>
              <environmentVariables>
                <!--
                  Do not download Cypress binary during `npm install`. It's large (0.5G) and it's only needed locally
                  when running `npm run cypress:run`. When building the project with Maven (e.g. on Jenkins), Cypress
                  tests are executed in a Docker container so the binary downloaded by the npm package is not used.
                 -->
                <CYPRESS_INSTALL_BINARY>0</CYPRESS_INSTALL_BINARY>
              </environmentVariables>
            </configuration>
          </execution>
          <execution>
            <id>npm run typecheck</id>
            <phase>compile</phase>
            <goals>
              <goal>npm</goal>
            </goals>
            <configuration>
              <arguments>run typecheck</arguments>
            </configuration>
          </execution>
          <execution>
            <id>npm run lint</id>
            <phase>compile</phase>
            <goals>
              <goal>npm</goal>
            </goals>
            <configuration>
              <arguments>run lint</arguments>
            </configuration>
          </execution>
          <execution>
            <id>npm test</id>
            <phase>test</phase>
            <goals>
              <goal>npm</goal>
            </goals>
            <configuration>
              <arguments>run coverage</arguments>
            </configuration>
          </execution>
        </executions>
      </plugin>
      <plugin>
        <artifactId>maven-resources-plugin</artifactId>
        <executions>
          <execution>
            <id>copy-resources</id>
            <phase>prepare-package</phase>
            <goals>
              <goal>copy-resources</goal>
            </goals>
            <configuration>
              <outputDirectory>${project.build.directory}/${project.build.finalName}</outputDirectory>
              <resources>
                <resource>
                  <directory>build</directory>
                </resource>
              </resources>
            </configuration>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>
  <profiles>
    <profile>
      <!-- Do npm run build by default but allow to skip it (mvn -P-npmBuild) to speed up local builds. -->
      <id>npmBuild</id>
      <!-- Active unless deactivated (https://stackoverflow.com/a/57141425/1691152). -->
      <activation>
        <file>
          <exists>.</exists>
        </file>
      </activation>
      <build>
        <plugins>
          <plugin>
            <groupId>com.github.eirslett</groupId>
            <artifactId>frontend-maven-plugin</artifactId>
            <executions>
              <execution>
                <id>npm run build</id>
                <phase>prepare-package</phase>
                <goals>
                  <goal>npm</goal>
                </goals>
                <configuration>
                  <arguments>run build</arguments>
                </configuration>
              </execution>
            </executions>
          </plugin>
        </plugins>
      </build>
    </profile>
    <profile>
      <id>productized</id>
      <activation>
        <property>
          <name>productized</name>
        </property>
      </activation>
      <build>
        <plugins>
          <plugin>
            <groupId>com.github.eirslett</groupId>
            <artifactId>frontend-maven-plugin</artifactId>
            <executions>
              <execution>
                <id>locktt final cleanup</id>
                <phase>prepare-package</phase>
                <goals>
                  <goal>npm</goal>
                </goals>
                <configuration>
                  <arguments>run locktt</arguments>
                </configuration>
              </execution>
            </executions>
          </plugin>
        </plugins>
      </build>
    </profile>
  </profiles>
</project>
