#!/usr/bin/env groovy

node {
    stage('checkout') {
        checkout scm
    }

<%_ if (integrations.includes('gitlab')) { _%>
    gitlabCommitStatus('build') {
<%_ } _%>
<%_ if (integrations.includes('docker')) { _%>
<%= gitLabIndent %>    docker.image('openjdk:8').inside('-u root<% if (buildTool === 'maven') { %> -e MAVEN_OPTS="-Duser.home=./"<% } else if (buildTool === 'gradle') { %> -e GRADLE_USER_HOME=.gradle<% } %>') {
<%_ } _%>
<%= indent %>    stage('check java') {
<%= indent %>        sh "java -version"
<%= indent %>    }

<%_ if (buildTool === 'maven') { _%>
<%= indent %>    stage('clean') {
<%= indent %>        sh "./mvnw clean"
<%= indent %>    }

    <%_ if (!skipClient) { _%>
        <%_ if (clientPackageManager === 'yarn') { _%>
<%= indent %>    stage('install tools') {
<%= indent %>        sh "./mvnw com.github.eirslett:frontend-maven-plugin:install-node-and-yarn -DnodeVersion=v6.9.4 -DyarnVersion=v0.18.1"
<%= indent %>    }

<%= indent %>    stage('yarn install') {
<%= indent %>        sh "./mvnw com.github.eirslett:frontend-maven-plugin:yarn"
<%= indent %>    }

        <%_ } else if (clientPackageManager === 'npm') { _%>
<%= indent %>    stage('install tools') {
<%= indent %>        sh "./mvnw com.github.eirslett:frontend-maven-plugin:install-node-and-npm -DnodeVersion=v6.9.4 -DnpmVersion=3.10.9"
<%= indent %>    }

<%= indent %>    stage('npm install') {
<%= indent %>        sh "./mvnw com.github.eirslett:frontend-maven-plugin:npm"
<%= indent %>    }

        <%_ } _%>
    <%_ } _%>
<%= indent %>    stage('backend tests') {
<%= indent %>        try {
<%= indent %>            sh "./mvnw test"
<%= indent %>        } catch(err) {
<%= indent %>            throw err
<%= indent %>        } finally {
<%= indent %>            junit '**/target/surefire-reports/TEST-*.xml'
<%= indent %>        }
<%= indent %>    }

    <%_ if (!skipClient) { _%>
<%= indent %>    stage('frontend tests') {
<%= indent %>        try {
<%= indent %>            sh "./mvnw com.github.eirslett:frontend-maven-plugin:gulp -Dfrontend.gulp.arguments=test"
<%= indent %>        } catch(err) {
<%= indent %>            throw err
<%= indent %>        } finally {
<%= indent %>            junit '**/target/test-results/karma/TESTS-*.xml'
<%= indent %>        }
<%= indent %>    }

    <%_ } _%>
    <%_ if (integrations.includes('heroku')) { _%>
<%= indent %>    stage('package and deploy') {
<%= indent %>        sh "HEROKU_API_KEY=\"<%= herokuApiKey %>\" ./mvnw com.heroku.sdk:heroku-maven-plugin:1.1.1:deploy-war -DskipTests -Pprod -Dheroku.appName=<%= herokuAppName %>"
    <%_ } else { _%>
<%= indent %>    stage('packaging') {
<%= indent %>        sh "./mvnw package -Pprod -DskipTests"
    <%_ } _%>
<%= indent %>        archiveArtifacts artifacts: '**/target/*.war', fingerprint: true
<%= indent %>    }

<%_ } else if (buildTool === 'gradle') { _%>
<%= indent %>    stage('clean') {
<%= indent %>        sh "./gradlew clean --no-daemon"
<%= indent %>    }

    <%_ if (!skipClient) { _%>
<%= indent %>    stage('npm install') {
<%= indent %>        sh "./gradlew npmInstall -PnodeInstall --no-daemon"
<%= indent %>    }

    <%_ } _%>
<%= indent %>    stage('backend tests') {
<%= indent %>        try {
<%= indent %>            sh "./gradlew test -PnodeInstall --no-daemon"
<%= indent %>        } catch(err) {
<%= indent %>            throw err
<%= indent %>        } finally {
<%= indent %>            junit '**/build/**/TEST-*.xml'
<%= indent %>        }
<%= indent %>    }

    <%_ if (!skipClient) { _%>
<%= indent %>    stage('frontend tests') {
<%= indent %>        try {
<%= indent %>            sh "./gradlew gulp_test -PnodeInstall --no-daemon"
<%= indent %>        } catch(err) {
<%= indent %>            throw err
<%= indent %>        } finally {
<%= indent %>            junit '**/build/test-results/karma/TESTS-*.xml'
<%= indent %>        }
<%= indent %>    }

    <%_ } _%>
<%= indent %>    stage('packaging') {
<%= indent %>        sh "./gradlew bootRepackage -x test -Pprod -PnodeInstall --no-daemon"
<%= indent %>        archiveArtifacts artifacts: '**/build/*.war', fingerprint: true
<%= indent %>    }

    <%_ if (integrations.includes('heroku')) { _%>
<%= indent %>    stage('deployment') {
<%= indent %>        sh "HEROKU_API_KEY=\"<%= herokuApiKey %>\" ./gradlew deployHeroku --no-daemon"
<%= indent %>    }

     <%_ } _%>
<%_ } _%>
<%_ if (integrations.includes('sonar')) { _%>
<%= indent %>    stage('quality analysis') {
<%= indent %>        withSonarQubeEnv('<%= jenkinsSonarName %>') {
    <%_ if (buildTool === 'maven') { _%>
<%= indent %>            sh "./mvnw sonar:sonar"
    <%_ } else if (buildTool === 'gradle' ) { _%>
<%= indent %>            sh "./gradlew sonarqube --no-daemon"
    <%_ } _%>
<%= indent %>        }
<%= indent %>    }
<%_ } _%>
<%_ if (integrations.includes('docker')) { _%>
<%= gitLabIndent %>    }
<%_ } _%>
<%_ if (integrations.includes('gitlab')) { _%>
    }
<%_ } _%>
}
