buildscript {
    repositories { mavenCentral() }
}

plugins {
    id "net.saliman.properties" version "1.4.4"
    id "org.sonarqube" version "2.6.1"
    id "org.springframework.boot" version "1.5.2.RELEASE"
}

// >>>>> building for different profiles
// >>>>> For configuration use -Pprofile=profile_name with run ("dev" is used by default)
loadConfiguration()

def loadConfiguration() {
    def profile = hasProperty("profile") ? profile : "dev"
    project.ext.setProperty("profile", profile)
}

apply from: "profiles.gradle"

// >>>>> applied plugins

apply plugin: "idea"
apply plugin: "java"
apply plugin: "jacoco"

task copyLaunchScripts(type: Copy) {
    from "$rootDir/scripts/pipeline-launch"
    into "$rootDir/api/src/main/resources/static/"
}

task copyCommitRunScripts(type: Copy) {
    from "$rootDir/scripts/commit-run-scripts/"
    into "$rootDir/api/src/main/resources/static/commit-run-scripts"
}

task copyFsAutoscalerScripts(type: Copy) {
    from "$rootDir/scripts/autoscaling/fsautoscale.sh"
    into "$rootDir/api/src/main/resources/static/"
}

task copyEmergencyNodeTerminationScript(type: Copy) {
    from "$rootDir/scripts/autoscaling/emergency_node_terminator.sh"
    into "$rootDir/api/src/main/resources/static/"
}

task copyLocalDtsStartupScripts(type: Copy) {
    from "$rootDir/scripts/dts/DeployDts.ps1", "$rootDir/scripts/dts/deploy_dts.sh"
    into "$rootDir/api/src/main/resources/static/"
}

task generateComponentsVersions(type: Exec) {
    commandLine "bash",
                "$project.rootDir/scripts/pipeline-scripts/generate_components_versions.sh",
                "$project.rootDir",
                "$rootDir/api/src/main/resources/static/components_versions.json"
}

jar {
    archiveName "pipeline.jar"
    manifest {
        attributes(
                "Implementation-Version": "$version")
    }
}

// >>>>> Exclude tests through property defined in the build.gradle file
test {
    if (project.hasProperty('excludeTests')) {
        exclude project.property('excludeTests')
    }
    testLogging {
        events "passed", "skipped", "failed"
    }
    afterSuite { desc, result ->
        if (!desc.parent) { // will match the outermost suite
            println "Results: ${result.resultType} (${result.testCount} total, ${result.successfulTestCount} success, ${result.failedTestCount} failure, ${result.skippedTestCount} skipped)"
        }
    }
}

sourceCompatibility = 1.8

repositories {
    mavenCentral()
    maven {
        url System.getProperty("user.dir") + "/api/common"

    }
}

ext {
    versionSpringBoot = "1.5.2.RELEASE"
    versionSpringSession = "1.3.1.RELEASE"
    versionElasticsearch = "6.8.3"
}

ext["elasticsearch.version"] = project.ext.versionElasticsearch

def apiDeps = [":client:buildUI", ":core:build", ":buildPipe", ":pipe-cli:build", ":pipe-cli:buildLinux",
":pipe-cli:buildWin", ":fs-browser:build", ":workflows:buildGpuStat", ":cloud-pipeline-webdav-client:buildWin",
":cloud-pipeline-webdav-client:buildLinux", ":cloud-pipeline-webdav-client:buildCli"]

build.mustRunAfter apiDeps
bootRepackage.mustRunAfter apiDeps

dependencies {
    // Spring Boot
    configurations {
        all*.exclude module : "spring-boot-starter-logging"
    }
    compile("org.springframework.boot:spring-boot-starter-actuator")
    compile group: "org.springframework.boot", name: "spring-boot-starter-web", version: project.ext.versionSpringBoot
    compile group: "org.springframework.boot", name: "spring-boot-starter-data-jpa", version: project.ext.versionSpringSession
    compile group: "org.springframework.session", name: "spring-session-jdbc", version: project.ext.versionSpringSession
    compile("org.springframework.boot:spring-boot-starter-tomcat")
    compile("org.aspectj:aspectjweaver:1.8.8")
    compile "com.squareup.retrofit2:converter-gson:2.0.0-beta3"

    //security
    compile("org.springframework.boot:spring-boot-starter-security")
    compile "com.auth0:java-jwt:3.1.0"
    compile "org.springframework.security.extensions:spring-security-saml2-core:1.0.2.RELEASE"
    compile group: "org.springframework.security", name: "spring-security-acl", version: "4.2.2.RELEASE"
    compile group: "net.sf.ehcache", name: "ehcache", version: "2.10.4"
    compile group: "org.springframework", name: "spring-context", version: "4.3.7.RELEASE"
    compile group: "org.springframework", name: "spring-context-support", version: "4.3.7.RELEASE"
    compile "com.jcraft:jsch:0.1.55"

    //Cache
    compile("org.springframework.boot:spring-boot-starter-cache")
    compile group: 'org.springframework.data', name: 'spring-data-redis', version: '1.8.23.RELEASE'
    compile group: 'redis.clients', name: 'jedis', version: '2.9.0'

    //GCP
    compile group: "com.google.auth", name: "google-auth-library-oauth2-http", version: "0.10.0"
    compile group: "com.google.api-client", name: "google-api-client", version: "1.34.0"
    compile "com.google.apis:google-api-services-compute:v1-rev20220720-1.32.1"
    compile "com.google.apis:google-api-services-cloudbilling:v1-rev30-1.25.0"
    compile "com.google.apis:google-api-services-iamcredentials:v1-rev32-1.25.0"
    compile "com.google.cloud:google-cloud-storage:1.70.0"

    testCompile group: "org.springframework.security", name: "spring-security-test", version: "4.2.2.RELEASE"
    testCompile group: "com.github.tomakehurst", name: "wiremock", version: "2.14.0"

    //DB
    compile("org.springframework:spring-jdbc")
    compile group: "org.postgresql", name: "postgresql", version: "42.7.3"
    compile group: "com.mchange", name: "c3p0", version: "0.9.5.2"
    compile group: 'org.hibernate', name: 'hibernate-java8', version: '5.0.12.Final'

    // Logging
    compile group: "org.slf4j", name: "slf4j-api", version: "1.7.24"
    compile group: "org.apache.logging.log4j", name: "log4j-slf4j-impl", version: log4jVersion
    compile group: "org.apache.logging.log4j", name: "log4j-api", version: log4jVersion
    compile group: "org.apache.logging.log4j", name: "log4j-core", version: log4jVersion
    compile group: "org.apache.logging.log4j", name: "log4j-jcl", version: log4jVersion

    // Core library
    compile project(":core")

    // Event sourcing client module to work with Redis Streams
    compile project(":cloud-pipeline-common:event-sourcing-java-client")

    //Kubernetes
    compile "io.fabric8:kubernetes-client:2.2.7"
    compile "io.fabric8:kubernetes-model:2.1.2"

    //Lombok
    compileOnly "org.projectlombok:lombok:1.16.16"
    testCompileOnly "org.projectlombok:lombok:1.16.16"


    //Notifications
    compile group: "com.relayrides", name: "pushy", version: "0.9.3"
    compile group: "us.raudi.pushraven", name: "Pushraven", version: "1.0.2"

    // Apache Commons
    compile group: "commons-io", name: "commons-io", version: "2.4"
    compile group: "org.apache.commons", name: "commons-lang3", version: "3.0"
    compile group: "org.apache.commons", name: "commons-collections4", version: "4.0"
    compile group: "commons-validator", name: "commons-validator", version: "1.5.0"
    compile group: "org.apache.commons", name: "commons-math3", version: "3.6.1"

    // https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml
    compile group: "org.apache.poi", name: "poi", version: "3.16"
    compile group: "org.apache.poi", name: "poi-ooxml", version: "3.16"

    // https://mvnrepository.com/artifact/com.opencsv/opencsv
    compile group: "com.opencsv", name: "opencsv", version: "3.9"
    compile group: "org.apache.commons", name: "commons-csv", version: "1.5"

    // Flyway database migration to update embedded database when application starts
    compile group: "org.flywaydb", name: "flyway-core", version: "4.1.2"

    // Swagger
    compile group: "io.springfox", name: "springfox-swagger2", version: "2.6.1"
    compile group: "io.springfox", name: "springfox-swagger-ui", version: "2.6.1"

    //AWS
    compile group: "com.amazonaws", name: "aws-java-sdk-ec2", version: "1.12.256"
    // https://mvnrepository.com/artifact/com.amazonaws/aws-java-sdk-s3
    compile group: "com.amazonaws", name: "aws-java-sdk-s3", version: "1.12.256"
    // https://mvnrepository.com/artifact/com.amazonaws/aws-java-sdk-sts
    compile group: "com.amazonaws", name: "aws-java-sdk-sts", version: "1.11.704"
    // https://mvnrepository.com/artifact/com.amazonaws/aws-java-sdk-iam
    compile group: "com.amazonaws", name: "aws-java-sdk-iam", version: "1.11.704"
    // https://mvnrepository.com/artifact/com.amazonaws/aws-java-sdk-fsx
    compile group: 'com.amazonaws', name: 'aws-java-sdk-fsx', version: '1.12.770'
    compile group: 'com.amazonaws', name: 'aws-java-sdk-route53', version: '1.11.880'
    compile group: "com.amazonaws", name: "aws-java-sdk-omics", version: "1.12.717"
    compile group: 'com.amazonaws', name: 'aws-java-sdk-pricing', version: '1.12.780'


    // https://mvnrepository.com/artifact/com.microsoft.azure/azure-mgmt-resources
    compile group: "com.microsoft.azure", name: "azure-mgmt-resources", version: "1.19.0"
    // https://mvnrepository.com/artifact/com.microsoft.azure/azure
    compile group: "com.microsoft.azure", name: "azure", version: "1.19.0"
    // Azure Storage Blob
    compile group: "com.microsoft.azure", name: "azure-storage-blob", version: "11.0.0"

    // Retrofit
    compile group: "com.squareup.retrofit2", name: "retrofit", version: "2.3.0"
    compile group: "com.squareup.retrofit2", name: "converter-jackson", version: "2.3.0"
    compile group: "com.squareup.okhttp3", name: "okhttp", version: "3.9.1"
    compile group: "com.fasterxml.jackson.datatype", name: "jackson-datatype-jsr310", version: "2.9.6"
    compile group: "com.squareup.retrofit2", name: "converter-scalars", version: "2.3.0"

    // Apache Commons FileUpload
    compile group: "commons-fileupload", name: "commons-fileupload", version: "1.3.3"

    // Simple SAML
    compile group: "com.coveo", name: "saml-client", version: "1.4.0"

    //Mapstruct
    compile group: "org.mapstruct", name: "mapstruct-jdk8", version: "1.2.0.Final"
    compile group: "org.mapstruct", name: "mapstruct-processor", version: "1.2.0.Final"

    // RxJava
    compile group: "io.reactivex.rxjava2", name: "rxjava", version: "2.1.12"

    // Elasticsearch
    compile group: "org.elasticsearch.client", name: "elasticsearch-rest-client", version: project.ext.versionElasticsearch
    compile group: "org.elasticsearch.client", name: "elasticsearch-rest-high-level-client", version: project.ext.versionElasticsearch
    testCompile group: "org.elasticsearch.client", name: "transport", version: project.ext.versionElasticsearch

    // Pipeline run scheduling
    compile group: 'org.quartz-scheduler', name: 'quartz', version: '2.3.2'

    //LDAP
    compile 'org.springframework.ldap:spring-ldap-core:2.3.2.RELEASE'

    //Tests
    compile group: "org.springframework.boot", name: "spring-boot-starter-test", version: project.ext.versionSpringBoot

    //ShedLock
    compile group: 'net.javacrumbs.shedlock', name: 'shedlock-spring', version: '3.0.0'
    compile group: 'net.javacrumbs.shedlock', name: 'shedlock-provider-jdbc-template', version: '3.0.0'

    compile('io.reflectoring.diffparser:diffparser:1.4')

    compile 'dnsjava:dnsjava:3.4.3'

    //Template engine
    compile group: 'org.apache.velocity', name: 'velocity', version: '1.7'
}

// >>>>> processes profiles
processResources.dependsOn.addAll([copyConfiguration, copyLaunchScripts, copyCommitRunScripts, copyFsAutoscalerScripts,
                                   copyLocalDtsStartupScripts, generateComponentsVersions,
                                   copyEmergencyNodeTerminationScript])
