group 'com.epam.pipeline'

def buildNumber = hasProperty("buildNumber") ? buildNumber : ""
def profile = hasProperty("profile") ? profile : ""

allprojects {
    version = new Version(major: 0, minor: 17, patch: 0, build: buildNumber)

    task printVersion {
        doFirst {
            println "Project version is $version"
        }
    }

    apply plugin: "checkstyle"
    apply plugin: "pmd"

    pmd {
        ruleSetFiles = files("$rootDir/config/pmd/pmd-ruleset.xml")
    }

    checkstyle {
        toolVersion = "7.2"
    }
}

class Version {
    int major, minor, patch
    String build

    String toString() {
        "$major.$minor.$patch${build ? ".$build" : ''}"
    }
}

task cleanPipe(type: Delete) {
    group = 'build'
    delete "$project.rootDir/dist/pipe-common.tar.gz"
}

task buildPipe(type: Tar, dependsOn: cleanPipe) {
    group = 'build'
    from("$project.rootDir/workflows/pipe-common/")
    baseName = 'pipe-common'
    destinationDir = file("$project.rootDir/dist/")
    extension = 'tar.gz'
    compression = Compression.GZIP
    doLast {
        delete "$project.rootDir/api/src/main/resources/static/pipe-common.tar.gz"
        copy {
            from "$project.rootDir/dist/pipe-common.tar.gz"
            into "$project.rootDir/api/src/main/resources/static/"
        }
    }
}

task runMkdocs(type: Exec) {
    doFirst {
        delete fileTree("$rootDir/docs/site")
    }
    commandLine "mkdocs", "build"
    workingDir = "$rootDir/docs"
}

task buildDoc(type: Tar) {
    dependsOn runMkdocs
    from("$rootDir/docs/site") {
    }
    archiveName = "cloud-pipeline-docs.tar.gz"
    destinationDir file("$rootDir/dist")
    extension = 'tar.gz'
    compression = Compression.GZIP
}

task buildAllFast(dependsOn: [":core:build", "client:buildUI", buildPipe, buildDoc,
                          ":pipe-cli:build", ":pipe-cli:buildLinux", ":pipe-cli:buildWin", ":jwt-generator:shadowJar",
                          ":api:bootRepackage", ":notifier:smtp:bootRepackage", ":docker-comp-scan:bootRepackage",
                          ":elasticsearch-agent:bootJar", ":vm-monitor:bootJar",
                          ":data-transfer-service:bootJar", ":data-transfer-service:bundleWindows",
                          ":data-transfer-service:bundleLinux",
                          ":data-sharing-service:buildFast", ":fs-browser:build", ":workflows:buildGpuStat", ":billing-report-agent:bootJar",
                          ":cloud-pipeline-webdav-client:buildWin", ":cloud-pipeline-webdav-client:buildLinux", ":cloud-pipeline-webdav-client:buildCli",
                          ":git-reader:build", ":monitoring-service:build", ":storage-lifecycle-service:build"]) {
    group = 'build'
}

task buildAll(dependsOn: [":core:build", "client:buildUI", buildPipe, buildDoc,
                          ":pipe-cli:build", ":pipe-cli:buildLinux", ":pipe-cli:buildWin", ":jwt-generator:shadowJar",
                          ":api:build", ":notifier:smtp:build", ":docker-comp-scan:build",
                          ":elasticsearch-agent:build", ":vm-monitor:build",
                          ":data-transfer-service:build", ":data-transfer-service:bundleWindows",
                          ":data-transfer-service:bundleLinux",
                          ":data-sharing-service:buildAll", ":fs-browser:build", ":workflows:buildGpuStat", ":billing-report-agent:build",
                          ":cloud-pipeline-webdav-client:buildWin", ":cloud-pipeline-webdav-client:buildLinux", ":cloud-pipeline-webdav-client:buildCli",
                          ":git-reader:build", ":monitoring-service:build", ":storage-lifecycle-service:build"]) {
    group = 'build'
}


task installDist() {
    group = 'distribution'

    if (project.hasProperty('fast')) {
        dependsOn buildAllFast
    } else {
        dependsOn buildAll
    }

    doLast {
        def distFolder = "$project.buildDir/install/bin"
        mkdir(distFolder)

        // copy various scripts
        def scriptsFolder = "$distFolder/scripts"
        def pipelineScriptsFolder = "$scriptsFolder/pipeline"

        copy {
            from "$rootDir/scripts/autoscaling"
            into scriptsFolder
        }

        copy {
            from "$rootDir/scripts/pipeline-scripts"
            into pipelineScriptsFolder
        }

        def distDirs = files(
                "$rootDir/scripts/git-role-management",
                "$rootDir/scripts/nfs-roles-management",
        )

        distDirs.each {
            dir ->
                copy {
                    from dir
                    into "$scriptsFolder/$dir.name"
                }
        }

        copy {
            from "$rootDir/workflows/pipe-templates"
            into "$distFolder/pipe-templates"
        }

        copy {
            from "$rootDir/elasticsearch-agent/src/main/resources/templates"
            into "$distFolder/search-mappings"
        }

        copy {
            from "$rootDir/monitoring-service/src/main/resources/templates"
            into "$distFolder/monitoring-index-mappings"
        }

        copy {
            from "$rootDir/vm-monitor/src/main/resources/templates"
            into "$distFolder/vm-monitor-templates"
        }

        def distFiles = files(
                "$rootDir/api/build/libs/pipeline.jar",
                "$rootDir/api/pipeline.sh",
                "$rootDir/jwt-generator/build/libs/jwt-generator.jar",
                "$rootDir/notifier/smtp/build/libs/notifier.jar",
                "$rootDir/docker-comp-scan/build/libs/docker-comp-scan.jar",
                "$rootDir/dist/cloud-pipeline-docs.tar.gz",
                "$rootDir/elasticsearch-agent/build/libs/elasticsearch-agent.jar",
                "$rootDir/data-transfer-service/build/libs/data-transfer-service.jar",
                "$rootDir/data-transfer-service/build/distributions/data-transfer-service-windows.zip",
                "$rootDir/data-transfer-service/build/distributions/data-transfer-service-linux.zip",
                "$rootDir/vm-monitor/build/libs/vm-monitor.jar",
                "$rootDir/data-sharing-service/api/build/libs/data-sharing-service.jar",
                "$rootDir/billing-report-agent/build/libs/billing-report-agent.jar",
                "$rootDir/git-reader/dist/gitreader.tar.gz",
                "$rootDir/monitoring-service/build/libs/monitoring-service.jar",
                "$rootDir/storage-lifecycle-service/dist/sls.tar.gz"
        )

        distFiles.each {
            distFile ->
                copy {
                    from distFile
                    into distFolder
                }
        }
    }
}

task distZip(type: Zip, dependsOn: installDist) {
    version = project.version
    group = "distribution"
    archiveName "cloud-pipeline.${version}.zip"
    destinationDir(file("$project.buildDir/install/dist"))
    from("$project.buildDir/install/bin/") {
        into "bin/"
    }
    include "**/**"
}

task distTar(type: Tar, dependsOn: installDist) {
    version = project.version
    group = "distribution"
    compression = Compression.GZIP
    archiveName "cloud-pipeline.${version}.tgz"
    destinationDir(file("$project.buildDir/install/dist"))
    from("$project.buildDir/install/bin/") {
        into "bin/"
    }
    include "**/**"
}

task wrapper(type: Wrapper) {
    gradleVersion = '4.10.2'
    distributionUrl = "https://services.gradle.org/distributions/gradle-$gradleVersion-all.zip"
}

task clean(type: Delete) {
    group = "build"
    delete(project.buildDir)
}