<?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/maven-v4_0_0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<parent>
		<groupId>com.mastercard.test.flow</groupId>
		<artifactId>report</artifactId>
		<version>1.1.4</version>
	</parent>
	<artifactId>report-ng</artifactId>
	<packaging>jar</packaging>
	<description>Report webapp</description>

	<properties>
		<src.res.dir>${basedir}/dist/report</src.res.dir>
		<src.index.file>${src.res.dir}/index.html</src.index.file>
		<dst.res.dir>${basedir}/target/classes/com/mastercard/test/flow/report</dst.res.dir>
	</properties>

	<dependencies>
		<dependency>
			<!-- there are no tests, but this keeps builds happy
			     when you run the reactor with, e.g. -DexcludedGroups=gui -->
			<groupId>org.junit.jupiter</groupId>
			<artifactId>junit-jupiter</artifactId>
		</dependency>
	</dependencies>

	<build>
		<plugins>

			<plugin>
				<!-- Step 2: Fail the build with a useful message if the compiled content isn't there -->
				<artifactId>maven-enforcer-plugin</artifactId>
				<version>3.4.1</version>
				<executions>
					<execution>
						<id>enforce-content</id>
						<goals>
							<goal>enforce</goal>
						</goals>
						<phase>process-resources</phase>
						<configuration>
							<rules>
								<requireFilesExist>
									<message>Compiled application artifacts are missing!
Build with
 * '-Dnode=system' if you already have node installed
 * '-Dnode=local' if you don't have node installed, but you'd like a local installation to be created
 * '-Dnode=none' if you don't want to build the webapp at all (test coverage will be reduced)</message>
									<files>
										<file>${src.index.file}</file>
									</files>
								</requireFilesExist>
							</rules>
						</configuration>
					</execution>
				</executions>
			</plugin>

			<plugin>
				<!-- Step 3: copy the compiled content into the resources dir -->
				<artifactId>maven-resources-plugin</artifactId>
				<version>3.3.1</version>
				<executions>
					<execution>
						<id>copy-content-to-artifact</id>
						<goals>
							<goal>copy-resources</goal>
						</goals>
						<phase>process-resources</phase>
						<configuration>
							<outputDirectory>${dst.res.dir}</outputDirectory>
							<resources>
								<resource>
									<directory>${src.res.dir}</directory>
								</resource>
							</resources>
						</configuration>
					</execution>
				</executions>
			</plugin>

			<plugin>
				<!-- Step 4: generate a manifest file that lists the content files -->
				<artifactId>maven-antrun-plugin</artifactId>
				<version>3.1.0</version>
				<executions>
					<execution>
						<id>create-manifest</id>
						<goals>
							<goal>run</goal>
						</goals>
						<phase>process-resources</phase>
						<configuration>
							<target>
								<!-- By default angular puts `type="module"` attributes on the script elements.
								     These break viewing a report on the filesystem. We really like that use-case,
								     so we're going to remove those attributes here -->
								<replace file="${dst.res.dir}/index.html" value="">
									<replacetoken><![CDATA[type="module"]]></replacetoken>
								</replace>
								<fileset dir="${dst.res.dir}" excludes="manifest.txt" id="fileset" />
								<pathconvert pathsep="${line.separator}" property="filelist" refid="fileset" targetos="unix">
									<map from="${dst.res.dir}/" to="" />
								</pathconvert>
								<echo file="${dst.res.dir}/manifest.txt">${filelist}</echo>
								<fixcrlf eol="unix" file="${dst.res.dir}/manifest.txt" />
							</target>
						</configuration>
					</execution>
				</executions>
			</plugin>
		</plugins>
	</build>

	<profiles>

		<!-- The following profiles generate the compiled webapp artifacts -->
		<!-- Once the compiled artifacts exist, you can trigger recompilation by explictly activating
		the profile you *do* want, e.g.:  -->
		<!-- if you *do* have node installed: `mvn -Psystem-node clean package -->
		<!-- if you *don't* have node installed: `mvn -Plocal-node clean package -->

		<profile>
			<id>node-local</id>
			<!-- Use this profile if you *don't* have node and npm installed on your 
				system. Maven will create a local installation (in your home directory) of 
				the tools it needs -->
			<activation>
				<property>
					<name>node</name>
					<value>local</value>
				</property>
				<file>
					<missing>${src.index.file}</missing>
				</file>
			</activation>
			<build>
				<plugins>
					<plugin>
						<!-- Step 1: compile the angular projects into the dist directory -->
						<groupId>com.github.eirslett</groupId>
						<artifactId>frontend-maven-plugin</artifactId>
						<configuration>
							<nodeVersion>v14.15.1</nodeVersion>
							<installDirectory>target</installDirectory>
						</configuration>
						<executions>
							<execution>
								<id>install node and npm</id>
								<goals>
									<goal>install-node-and-npm</goal>
								</goals>
								<phase>initialize</phase>
							</execution>
							<execution>
								<id>npm install</id>
								<goals>
									<goal>npm</goal>
								</goals>
								<phase>initialize</phase>
								<configuration>
									<arguments>install</arguments>
								</configuration>
							</execution>
							<execution>
								<id>npm run build</id>
								<goals>
									<goal>npm</goal>
								</goals>
								<phase>generate-resources</phase>
								<configuration>
									<arguments>run build</arguments>
								</configuration>
							</execution>
						</executions>
					</plugin>
				</plugins>
			</build>
		</profile>

		<profile>
			<id>node-local-test</id>
			<activation>
				<property>
					<name>node</name>
					<value>local</value>
				</property>
			</activation>
			<build>
				<plugins>
					<plugin>
						<!-- Step 1: compile the angular projects into the dist directory -->
						<groupId>com.github.eirslett</groupId>
						<artifactId>frontend-maven-plugin</artifactId>
						<configuration>
							<nodeVersion>v14.15.1</nodeVersion>
							<installDirectory>target</installDirectory>
						</configuration>
						<executions>
							<execution>
								<id>npm run test</id>
								<goals>
									<goal>npm</goal>
								</goals>
								<phase>test</phase>
								<configuration>
									<arguments>run test</arguments>
								</configuration>
							</execution>
						</executions>
					</plugin>
				</plugins>
			</build>
		</profile>

		<profile>
			<id>node-system</id>
			<!-- Use this profile if you *do* have node and npm installed, and you'd 
				prefer to use those -->
			<activation>
				<property>
					<name>node</name>
					<value>system</value>
				</property>
				<file>
					<missing>${src.index.file}</missing>
				</file>
			</activation>
			<build>
				<plugins>
					<plugin>
						<!-- Step 1: compile the angular projects into the dist directory -->
						<groupId>org.codehaus.mojo</groupId>
						<artifactId>exec-maven-plugin</artifactId>
						<executions>
							<execution>
								<id>npm install</id>
								<goals>
									<goal>exec</goal>
								</goals>
								<phase>initialize</phase>
								<configuration>
									<executable>npm</executable>
									<arguments>
										<argument>install</argument>
									</arguments>
								</configuration>
							</execution>
							<execution>
								<id>npm run build</id>
								<goals>
									<goal>exec</goal>
								</goals>
								<phase>generate-resources</phase>
								<configuration>
									<executable>npm</executable>
									<arguments>
										<argument>run</argument>
										<argument>build</argument>
									</arguments>
								</configuration>
							</execution>
						</executions>
					</plugin>
				</plugins>
			</build>
		</profile>

		<profile>
			<id>node-system-test</id>
			<activation>
				<property>
					<name>node</name>
					<value>system</value>
				</property>
			</activation>
			<build>
				<plugins>
					<plugin>
						<groupId>org.codehaus.mojo</groupId>
						<artifactId>exec-maven-plugin</artifactId>
						<executions>
							<execution>
								<id>npm run test</id>
								<goals>
									<goal>exec</goal>
								</goals>
								<phase>test</phase>
								<configuration>
									<executable>npm</executable>
									<arguments>
										<argument>run</argument>
										<argument>test</argument>
									</arguments>
								</configuration>
							</execution>
						</executions>
					</plugin>
				</plugins>
			</build>
		</profile>

		<profile>
			<id>node-none</id>
			<!-- Use this profile if you don't want any part of this node malarkey. We'll copy a dummy index.html into
			     place to satisfy the enforce-content check, but it's not going to provide a useful visualisation
			     of the flow data -->
			<!-- The node=none property will disable the tests that assume the webapp is functional -->
			<activation>
				<property>
					<name>node</name>
					<value>none</value>
				</property>
				<file>
					<missing>${src.index.file}</missing>
				</file>
			</activation>
			<build>
				<plugins>
					<plugin>
						<!-- Step 1: copy a dummy index.html into the dist directory -->
						<artifactId>maven-resources-plugin</artifactId>
						<version>3.3.1</version>
						<executions>
							<execution>
								<id>copy-dummy-index</id>
								<goals>
									<goal>copy-resources</goal>
								</goals>
								<phase>generate-resources</phase>
								<configuration>
									<outputDirectory>${src.res.dir}</outputDirectory>
									<resources>
										<resource>
											<directory>src/main/resources/no-node</directory>
										</resource>
									</resources>
								</configuration>
							</execution>
						</executions>
					</plugin>
				</plugins>
			</build>
		</profile>

	</profiles>
</project>
