<?xml version="1.0" encoding="UTF-8"?>
<!--
  ~ Copyright © 2018-2025 Commonwealth Scientific and Industrial Research
  ~ Organisation (CSIRO) ABN 41 687 119 230.
  ~
  ~ 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>au.csiro.pathling</groupId>
    <artifactId>pathling</artifactId>
    <version>9.2.0</version>
    <relativePath>../../pom.xml</relativePath>
  </parent>
  <artifactId>r</artifactId>
  <packaging>jar</packaging>

  <name>Pathling R API</name>
  <description>A library for using Pathling with R.</description>

  <dependencies>
    <dependency>
      <groupId>au.csiro.pathling</groupId>
      <artifactId>library-runtime</artifactId>
      <scope>runtime</scope>
    </dependency>
    <dependency>
      <groupId>au.csiro.pathling</groupId>
      <artifactId>encoders</artifactId>
      <type>test-jar</type>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>au.csiro.pathling</groupId>
      <artifactId>library-api</artifactId>
      <type>test-jar</type>
      <scope>test</scope>
    </dependency>
  </dependencies>

  <properties>
    <!--suppress UnresolvedMavenProperty -->
    <pathling.Rapi.version.base>${parsedVersion.majorVersion}.${parsedVersion.minorVersion}.${parsedVersion.incrementalVersion}</pathling.Rapi.version.base>
    <pathling.Rapi.version.qualifier/>
    <pathling.Rapi.version>${pathling.Rapi.version.base}${pathling.Rapi.version.qualifier}</pathling.Rapi.version>
    <!-- 
      This needs to maintained as the most recent version of Spark that is in the versions manifest
      of the sparklyr version that we depend upon that is on the archive site. The reason we can't 
      use the latest from the CDN is that the files drop off the CDN when a new version is released.
    -->
    <pathling.Rapi.sparkVersion>4.0.1</pathling.Rapi.sparkVersion>
    <pathling.Rapi.scalaVersion>${pathling.scalaVersion}</pathling.Rapi.scalaVersion>
    <pathling.Rapi.hadoopMajorVersion>${pathling.hadoopMajorVersion}</pathling.Rapi.hadoopMajorVersion>
    <pathling.Rapi.hadoopVersion>${pathling.hadoopVersion}</pathling.Rapi.hadoopVersion>
    <pathling.Rapi.deltaVersion>${pathling.deltaVersion}</pathling.Rapi.deltaVersion>
    <pathling.Rapi.packageName>pathling</pathling.Rapi.packageName>
    <!--suppress UnresolvedMavenProperty -->
    <skipRapiTests>${skipTests}</skipRapiTests>
  </properties>

  <build>
    <sourceDirectory>${project.basedir}</sourceDirectory>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-clean-plugin</artifactId>
        <configuration>
          <filesets>
            <fileset>
              <directory>${project.basedir}/inst/java</directory>
              <includes>
                <include>*.jar</include>
              </includes>
            </fileset>
            <fileset>
              <directory>${project.basedir}/man</directory>
              <includes>
                <include>*.*</include>
              </includes>
            </fileset>
            <fileset>
              <directory>${project.basedir}/</directory>
              <includes>
                <include>NAMESPACE</include>
                <include>DESCRIPTION</include>
              </includes>
            </fileset>
            <fileset>
              <directory>${project.basedir}/tests/testthat</directory>
              <includes>
                <include>testdata</include>
              </includes>
            </fileset>
            <fileset>
              <directory>${project.basedir}</directory>
              <includes>
                <include>**/*.Rcheck/**</include>
              </includes>
            </fileset>
          </filesets>
        </configuration>
      </plugin>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <executions>
          <execution>
            <id>default-compile</id>
            <phase>none</phase>
          </execution>
          <execution>
            <id>default-testCompile</id>
            <phase>none</phase>
          </execution>
        </executions>
      </plugin>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <executions>
          <execution>
            <id>default-test</id>
            <phase>none</phase>
          </execution>
        </executions>
      </plugin>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-dependency-plugin</artifactId>
        <executions>
          <execution>
            <id>copy-test-data</id>
            <phase>generate-test-resources</phase>
            <goals>
              <goal>unpack-dependencies</goal>
            </goals>
            <configuration>
              <type>test-jar</type>
              <includeTypes>test-jar</includeTypes>
              <includeArtifactIds>library-api,encoders</includeArtifactIds>
              <includeGroupIds>${project.groupId}</includeGroupIds>
              <includeScope>test</includeScope>
              <excludeTransitive>true</excludeTransitive>
              <includes>/test-data/**,/data/**</includes>
              <fileMappers>
                <org.codehaus.plexus.components.io.filemappers.RegExpFileMapper>
                  <pattern>^test-data/</pattern>
                  <replacement>testdata/</replacement>
                </org.codehaus.plexus.components.io.filemappers.RegExpFileMapper>
                <org.codehaus.plexus.components.io.filemappers.RegExpFileMapper>
                  <pattern>^data/</pattern>
                  <replacement>testdata/encoders/</replacement>
                </org.codehaus.plexus.components.io.filemappers.RegExpFileMapper>
              </fileMappers>
              <outputDirectory>${project.basedir}/tests/testthat</outputDirectory>
            </configuration>
          </execution>
        </executions>
      </plugin>
      <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>exec-maven-plugin</artifactId>
        <executions>
          <execution>
            <id>generate-DESCRIPTION</id>
            <phase>initialize</phase>
            <goals>
              <goal>exec</goal>
            </goals>
            <configuration>
              <executable>sed</executable>
              <workingDirectory>${project.basedir}</workingDirectory>
              <arguments>
                <argument>-e</argument>
                <argument>s/%pathling.version%/${project.version}/</argument>
                <argument>-e</argument>
                <argument>s/%pathling.Rapi.version%/${pathling.Rapi.version}/</argument>
                <argument>-e</argument>
                <argument>s/%pathling.Rapi.sparkVersion%/${pathling.Rapi.sparkVersion}/</argument>
                <argument>-e</argument>
                <argument>s/%pathling.Rapi.scalaVersion%/${pathling.Rapi.scalaVersion}/</argument>
                <argument>-e</argument>
                <argument>s/%pathling.Rapi.hadoopMajorVersion%/${pathling.Rapi.hadoopMajorVersion}/</argument>
                <argument>-e</argument>
                <argument>s/%pathling.Rapi.hadoopVersion%/${pathling.Rapi.hadoopVersion}/</argument>
                <argument>-e</argument>
                <argument>s/%pathling.Rapi.deltaVersion%/${pathling.Rapi.deltaVersion}/</argument>
                <argument>${project.basedir}/DESCRIPTION.src</argument>
              </arguments>
              <outputFile>${project.basedir}/DESCRIPTION</outputFile>
            </configuration>
          </execution>
          <execution>
            <id>install-dependencies</id>
            <phase>initialize</phase>
            <goals>
              <goal>exec</goal>
            </goals>
            <configuration>
              <executable>Rscript</executable>
              <workingDirectory>${project.basedir}</workingDirectory>
              <arguments>
                <argument>--verbose</argument>
                <argument>-e</argument>
                <argument>Sys.setenv(CXX11="clang++", CXX11STD="-std=c++11", CXXFLAGS="-std=c++11")</argument>
                <argument>-e</argument>
                <argument>install.packages("devtools")</argument>
                <argument>-e</argument>
                <argument>devtools::install_dev_deps()</argument>
                <argument>-e</argument>
                <argument>sparklyr::spark_install(version='${pathling.Rapi.sparkVersion}', hadoop_version='${pathling.Rapi.hadoopMajorVersion}')</argument>
              </arguments>
            </configuration>
          </execution>
          <execution>
            <id>generate-docs</id>
            <phase>process-sources</phase>
            <goals>
              <goal>exec</goal>
            </goals>
            <configuration>
              <executable>Rscript</executable>
              <workingDirectory>${project.basedir}</workingDirectory>
              <arguments>
                <argument>-e</argument>
                <argument>roxygen2::roxygenise()</argument>
              </arguments>
            </configuration>
          </execution>
          <execution>
            <id>styler-check</id>
            <phase>test</phase>
            <goals>
              <goal>exec</goal>
            </goals>
            <configuration>
              <skip>${skipRapiTests}</skip>
              <executable>Rscript</executable>
              <workingDirectory>${project.basedir}</workingDirectory>
              <arguments>
                <argument>-e</argument>
                <argument>
result &lt;- rbind(styler::style_dir("R", dry = "on"), styler::style_dir("tests/testthat", dry = "on"))
if (any(result$changed)) {
  cat("The following files need formatting:\n")
  print(result[result$changed, "file"])
  stop("Code style check failed. Run 'mvn validate -Pformat -pl lib/R' to fix.")
}
                </argument>
              </arguments>
            </configuration>
          </execution>
          <execution>
            <id>lintr-check</id>
            <phase>test</phase>
            <goals>
              <goal>exec</goal>
            </goals>
            <configuration>
              <skip>${skipRapiTests}</skip>
              <executable>Rscript</executable>
              <workingDirectory>${project.basedir}</workingDirectory>
              <arguments>
                <argument>-e</argument>
                <argument>
lints &lt;- c(lintr::lint_dir("R"), lintr::lint_dir("tests/testthat"))
if (length(lints) > 0) {
  print(lints)
  stop("Linting failed with ", length(lints), " issue(s).")
}
                </argument>
              </arguments>
            </configuration>
          </execution>
          <execution>
            <id>test</id>
            <phase>test</phase>
            <goals>
              <goal>exec</goal>
            </goals>
            <configuration>
              <skip>${skipRapiTests}</skip>
              <executable>Rscript</executable>
              <workingDirectory>${project.basedir}</workingDirectory>
              <arguments>
                <argument>-e</argument>
                <argument>library(sparklyr)</argument>
                <argument>-e</argument>
                <argument>devtools::test(stop_on_failure = TRUE)</argument>
              </arguments>
            </configuration>
          </execution>
          <execution>
            <id>build-package</id>
            <phase>package</phase>
            <goals>
              <goal>exec</goal>
            </goals>
            <configuration>
              <executable>Rscript</executable>
              <workingDirectory>${project.basedir}</workingDirectory>
              <arguments>
                <argument>--verbose</argument>
                <argument>-e</argument>
                <argument>devtools::build(path="target")</argument>
              </arguments>
            </configuration>
          </execution>
          <execution>
            <id>install-package</id>
            <phase>install</phase>
            <goals>
              <goal>exec</goal>
            </goals>
            <configuration>
              <executable>R</executable>
              <workingDirectory>${project.basedir}</workingDirectory>
              <arguments>
                <argument>CMD</argument>
                <argument>INSTALL</argument>
                <argument>--preclean</argument>
                <argument>--no-multiarch</argument>
                <argument>--with-keep.source</argument>
                <argument>.</argument>
              </arguments>
            </configuration>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>
  <profiles>
    <profile>
      <id>docs</id>
      <build>
        <plugins>
          <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>exec-maven-plugin</artifactId>
            <executions>
              <execution>
                <id>generate-site</id>
                <phase>process-sources</phase>
                <goals>
                  <goal>exec</goal>
                </goals>
                <configuration>
                  <executable>Rscript</executable>
                  <workingDirectory>${project.basedir}</workingDirectory>
                  <arguments>
                    <argument>--verbose</argument>
                    <argument>-e</argument>
                    <argument>devtools::build_site(override = list(destination = '${project.basedir}/target/docs'))</argument>
                  </arguments>
                </configuration>
              </execution>
              <execution>
                <id>generate-manual</id>
                <phase>process-sources</phase>
                <goals>
                  <goal>exec</goal>
                </goals>
                <configuration>
                  <executable>Rscript</executable>
                  <workingDirectory>${project.basedir}</workingDirectory>
                  <arguments>
                    <argument>--verbose</argument>
                    <argument>-e</argument>
                    <argument>devtools::build_manual(path='${project.basedir}/target')</argument>
                  </arguments>
                </configuration>
              </execution>
            </executions>
          </plugin>
          <!-- Create a JAR containing the Rapi docs -->
          <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <executions>
              <execution>
                <id>doc-jar</id>
                <phase>package</phase>
                <goals>
                  <goal>jar</goal>
                </goals>
                <configuration>
                  <classesDirectory>${project.build.directory}/docs</classesDirectory>
                  <classifier>docs</classifier>
                </configuration>
              </execution>
            </executions>
          </plugin>
        </plugins>
      </build>
    </profile>
    <profile>
      <id>check</id>
      <build>
        <plugins>
          <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>exec-maven-plugin</artifactId>
            <executions>
              <execution>
                <id>check-package</id>
                <phase>verify</phase>
                <goals>
                  <goal>exec</goal>
                </goals>
                <configuration>
                  <skip>${skipRapiTests}</skip>
                  <executable>R</executable>
                  <workingDirectory>${project.basedir}</workingDirectory>
                  <arguments>
                    <argument>CMD</argument>
                    <argument>check</argument>
                    <argument>--output=${project.basedir}/target</argument>
                    <argument>--as-cran</argument>
                    <argument>target/${pathling.Rapi.packageName}_${pathling.Rapi.version}.tar.gz</argument>
                  </arguments>
                </configuration>
              </execution>
            </executions>
          </plugin>
        </plugins>
      </build>
    </profile>
    <profile>
      <id>format</id>
      <build>
        <plugins>
          <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>exec-maven-plugin</artifactId>
            <executions>
              <execution>
                <id>styler-format</id>
                <phase>validate</phase>
                <goals>
                  <goal>exec</goal>
                </goals>
                <configuration>
                  <executable>Rscript</executable>
                  <workingDirectory>${project.basedir}</workingDirectory>
                  <arguments>
                    <argument>-e</argument>
                    <argument>styler::style_dir("R"); styler::style_dir("tests/testthat")</argument>
                  </arguments>
                </configuration>
              </execution>
            </executions>
          </plugin>
        </plugins>
      </build>
    </profile>
  </profiles>
</project>
