<?xml version="1.0" encoding="UTF-8"?>
<!--

    Copyright 2015-2017 Hewlett Packard Enterprise Development LP.

    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>com.github.cafapi</groupId>
        <artifactId>swagger-restapi-client-base</artifactId>
        <version>1.10.0-205</version>
        <relativePath/>
    </parent>

    <groupId>com.github.cafdataprocessing</groupId>
    <artifactId>processing-service-client</artifactId>
    <description>Built as an example only - consumers should generate their own client tailored as required.</description>
    <version>1.3.0-70</version>

    <properties>
        <swagger-contract-artifactId>processing-service-contract</swagger-contract-artifactId>
        <swagger-codegen-inputSpec>/com/github/cafdataprocessing/processing/service/contract/swagger.yaml</swagger-codegen-inputSpec>
        <swagger-codegen-modelPackage>com.github.cafdataprocessing.processing.service.client.model</swagger-codegen-modelPackage>
        <swagger-codegen-apiPackage>com.github.cafdataprocessing.processing.service.client.api</swagger-codegen-apiPackage>
        <swagger-codegen-invokerPackage>com.github.cafdataprocessing.processing.service.client</swagger-codegen-invokerPackage>
    </properties>

    <dependencies>
        <dependency>
            <groupId>${project.groupId}</groupId>
            <artifactId>${swagger-contract-artifactId}</artifactId>
            <version>${project.version}</version>
            <scope>provided</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <!-- unpack the contract, we need to modify it as swagger-code-gen doesn't recognize 'null' type for properties and omits them from generated classes -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <version>2.10</version>
                <executions>
                    <execution>
                        <phase>initialize</phase>
                        <goals>
                            <goal>unpack</goal>
                        </goals>
                        <configuration>
                            <artifactItems>
                                <artifactItem>
                                    <groupId>${project.groupId}</groupId>
                                    <artifactId>${swagger-contract-artifactId}</artifactId>
                                    <version>${project.version}</version>
                                    <outputDirectory>${project.build.directory}/swagger-contract</outputDirectory>
                                </artifactItem>
                            </artifactItems>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

            <!-- remove the 'null' type as swagger-code-gen doesn't currently support it -->
            <plugin>
                <groupId>org.codehaus.gmavenplus</groupId>
                <artifactId>gmavenplus-plugin</artifactId>
                <version>1.5</version>
                <executions>
                    <execution>
                        <id>update contract</id>
                        <phase>generate-sources</phase>
                        <goals>
                            <goal>execute</goal>
                        </goals>
                        <configuration>
                            <properties>
                                <!-- Specifying as property here since Maven filtering won't escape the '\' on Windows environment resulting in
                                  Groovy compilation error once substituted -->
                                <property>
                                    <name>project.build.directory</name>
                                    <value>${project.build.directory}</value>
                                </property>
                            </properties>
                            <scripts>
                                <script>
                                    <![CDATA[
                                        def sourceFile = new File(project.build.directory+"/swagger-contract"+project.properties['swagger-codegen-inputSpec'])
                                        def sourceText = sourceFile.text;
                                        if(sourceText==null){
                                            return;
                                        }
                                        def dest = new File(project.build.directory+'/swagger-input.yaml');
                                        def destText = sourceText.replaceAll('\\[string, "null"\\]', "string").replaceAll('\\[integer, "null"\\]', "integer");
                                        dest.write(destText);
                                    ]]>
                                </script>
                            </scripts>
                        </configuration>
                    </execution>
                </executions>
                <dependencies>
                    <dependency>
                        <groupId>org.codehaus.groovy</groupId>
                        <artifactId>groovy-all</artifactId>
                        <version>2.4.7</version>
                        <scope>runtime</scope>
                    </dependency>
                </dependencies>
            </plugin>

            <!-- generate client library from modified contract -->
            <plugin>
                <groupId>io.swagger</groupId>
                <artifactId>swagger-codegen-maven-plugin</artifactId>
                <version>${swagger-codegen-maven-plugin-version}</version>
                <executions>
                    <execution>
                        <id>generate-java-client-code</id>
                        <goals>
                            <goal>generate</goal>
                        </goals>
                        <configuration>
                            <inputSpec>${project.build.directory}/swagger-input.yaml</inputSpec>
                            <language>java</language>
                            <modelPackage>${swagger-codegen-modelPackage}</modelPackage>
                            <apiPackage>${swagger-codegen-apiPackage}</apiPackage>
                            <invokerPackage>${swagger-codegen-invokerPackage}</invokerPackage>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>
