<%#
 Copyright 2013-2017 the original author or authors from the JBooter project.

 This file is part of the JBooter project, see https://jbooter.github.io/
 for more information.

 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.
-%>
<%_ if (gitlabUseDocker) { _%>
image: openjdk:8
<%_ } _%>

<%_ if (buildTool === 'gradle') { _%>
cache:
    key: "$CI_BUILD_REF_NAME"
    paths:
        - node_modules
        - .gradle/wrapper
        - .gradle/caches
<%_ } _%>
<%_ if (buildTool === 'maven') { _%>
cache:
    key: "$CI_BUILD_REF_NAME"
    paths:
        - node_modules
        - .maven
<%_ } _%>
stages:
    - build
    - test
    - package
<%_ if (heroku.includes('gitlab')) { _%>
    - deploy
<%_ } _%>

<%_ if (buildTool === 'gradle') { _%>
before_script:
    - export GRADLE_USER_HOME=`pwd`/.gradle
    - chmod +x gradlew
    <%_ if (!skipClient) { _%>
        <%_ if (clientPackageManager === 'yarn') { _%>
    - ./gradlew yarn_install -PnodeInstall --no-daemon
        <%_ } else if (clientPackageManager === 'npm') { _%>
    - ./gradlew npmInstall -PnodeInstall --no-daemon
        <%_ } _%>
    <%_ } _%>

gradle-build:
    stage: build
    script:
        - ./gradlew compileJava -x check -PnodeInstall --no-daemon
gradle-test:
    stage: test
    script:
        - ./gradlew test -PnodeInstall --no-daemon
    artifacts:
        paths:
            - build/reports/tests/*
    <%_ if (!skipClient) { _%>
gradle-front-test:
    stage: test
    script:
        <%_ if (clientFramework === 'angular1') { _%>
        - ./gradlew gulp_test -PnodeInstall --no-daemon
        <%_ } else { _%>
            <%_ if (clientPackageManager === 'yarn') { _%>
        - ./gradlew yarn_test -PnodeInstall --no-daemon
            <%_ } else if (clientPackageManager === 'npm') { _%>
        - ./gradlew npm_test -PnodeInstall --no-daemon
            <%_ } _%>
        <%_ } _%>
    artifacts:
        paths:
            - build/test-results/karma/*
    <%_ } _%>
    <%_ if (testFrameworks.includes('gatling')) { _%>
gatling-test:
    stage: test
    allow_failure: true
    script:
        - ./gradlew gatlingRun -x cleanResources -PnodeInstall --no-daemon
    before_script:
        - export GRADLE_USER_HOME=`pwd`/.gradle
        - chmod +x gradlew
        <%_ if (!skipClient) { _%>
            <%_ if (clientPackageManager === 'yarn') { _%>
        - ./gradlew yarn_install -PnodeInstall --no-daemon
            <%_ } else if (clientPackageManager === 'npm') { _%>
        - ./gradlew npmInstall -PnodeInstall --no-daemon
            <%_ } _%>
        <%_ } _%>
        - ./gradlew bootRun &
    artifacts:
        paths:
            - build/reports/gatling/*
    <%_ } _%>
gradle-repackage:
    stage: package
    script:
        - ./gradlew bootRepackage -Pprod -x check -PnodeInstall --no-daemon
    artifacts:
        paths:
            - build/libs/*.war
    <%_ if (heroku.includes('gitlab')) { _%>
gradle-deploy:
    stage: deploy
    script:
        - ./gradlew deployHeroku --no-daemon
    <%_ } _%>
<%_ } _%>
<%_ if (buildTool === 'maven') { _%>
before_script:
    - export MAVEN_USER_HOME=`pwd`/.maven
    - chmod +x mvnw
    <%_ if (!skipClient) { _%>
        <%_ if (clientPackageManager === 'yarn') { _%>
    - ./mvnw com.github.eirslett:frontend-maven-plugin:install-node-and-yarn -DnodeVersion=v<%= NODE_VERSION %> -DyarnVersion=v<%= YARN_VERSION %>
    - ./mvnw com.github.eirslett:frontend-maven-plugin:yarn
        <%_ } else if (clientPackageManager === 'npm') { _%>
    - ./mvnw com.github.eirslett:frontend-maven-plugin:install-node-and-npm -DnodeVersion=v<%= NODE_VERSION %> -DnpmVersion=<%= NPM_VERSION %>
    - ./mvnw com.github.eirslett:frontend-maven-plugin:npm
        <%_ } _%>
    <%_ } _%>

maven-build:
    stage: build
    script: ./mvnw compile -Dmaven.repo.local=$MAVEN_USER_HOME

maven-test:
    stage: test
    script:
        - ./mvnw test -Dmaven.repo.local=$MAVEN_USER_HOME
    artifacts:
        paths:
            - target/surefire-reports/*
    <%_ if (!skipClient) { _%>
maven-front-test:
    stage: test
    script:
        <%_ if (clientFramework === 'angular1') { _%>
        - ./mvnw com.github.eirslett:frontend-maven-plugin:gulp -Dfrontend.gulp.arguments=test
        <%_ } else { _%>
            <%_ if (clientPackageManager === 'yarn') { _%>
        - ./mvnw com.github.eirslett:frontend-maven-plugin:yarn -Dfrontend.yarn.arguments=test
            <%_ } else if (clientPackageManager === 'npm') { _%>
        - ./mvnw com.github.eirslett:frontend-maven-plugin:npm -Dfrontend.yarn.arguments=test
            <%_ } _%>
        <%_ } _%>
    artifacts:
        paths:
            - target/test-results/karma/*
    <%_ } _%>
    <%_ if (testFrameworks.includes('gatling')) { _%>
gatling-test:
    stage: test
    allow_failure: true
    script:
        - ./mvnw gatling:execute -Dmaven.repo.local=$MAVEN_USER_HOME
    before_script:
        - export MAVEN_USER_HOME=`pwd`/.maven
        - chmod +x mvnw
        <%_ if (!skipClient) { _%>
            <%_ if (clientPackageManager === 'yarn') { _%>
        - ./mvnw com.github.eirslett:frontend-maven-plugin:install-node-and-yarn -DnodeVersion=v<%= NODE_VERSION %> -DyarnVersion=v<%= YARN_VERSION %>
        - ./mvnw com.github.eirslett:frontend-maven-plugin:yarn
            <%_ } else if (clientPackageManager === 'npm') { _%>
        - ./mvnw com.github.eirslett:frontend-maven-plugin:install-node-and-npm -DnodeVersion=v<%= NODE_VERSION %> -DnpmVersion=<%= NPM_VERSION %>
        - ./mvnw com.github.eirslett:frontend-maven-plugin:npm
            <%_ } _%>
        <%_ } _%>
        - ./mvnw &
    artifacts:
        paths:
            - target/gatling/*
    <%_ } _%>
    <%_ if (heroku.includes('gitlab')) { _%>
maven-deploy:
    stage: deploy
     script:
        - ./mvnw com.heroku.sdk:heroku-maven-plugin:1.1.1:deploy -DskipTests -Pprod -Dheroku.appName=<%= herokuAppName %>
    <%_ } else { _%>
maven-package:
    stage: package
    script:
        - ./mvnw package -Pprod -DskipTests -Dmaven.repo.local=$MAVEN_USER_HOME
    <%_ } _%>
    artifacts:
        paths:
            - target/*.war
<%_ } _%>
