/*
 * Copyright (c) 2023, WSO2 LLC. (http://www.wso2.org). All Rights Reserved.
 *
 * 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.
 *
 */

plugins {
    id "java"
    id "com.github.johnrengelman.shadow"
    id "checkstyle"
}

dependencies {
    checkstyle project(':checkstyle')
    checkstyle "com.puppycrawl.tools:checkstyle:${project.checkstylePluginVersion}"

    implementation "org.ballerinalang:ballerina-lang:${ballerinaLangVersion}"
    implementation "org.ballerinalang:ballerina-parser:${ballerinaLangVersion}"
    implementation "org.ballerinalang:ballerina-tools-api:${ballerinaLangVersion}"
    implementation "org.ballerinalang:ballerina-cli:${ballerinaLangVersion}"
    implementation "org.ballerinalang:formatter-core:${ballerinaLangVersion}"
    implementation "io.ballerina.stdlib:graphql-commons:${stdlibGraphqlVersion}"
    implementation "commons-io:commons-io:${commonsIoVersion}"
    implementation "org.json:json:${orgJsonVersion}"
    implementation "com.graphql-java:graphql-java:${graphqlJavaVersion}"
    implementation "commons-logging:commons-logging:${commonsLoggingVersion}"
    testImplementation "org.testng:testng:${testngVersion}"

    jbalTools ("org.ballerinalang:jballerina-tools:${ballerinaLangVersion}") {
        transitive = false
    }
}

def excludePattern = '**/module-info.java'
tasks.withType(Checkstyle) {
    exclude excludePattern
}

checkstyle {
    toolVersion "${project.checkstylePluginVersion}"
    configFile rootProject.file("config/checkstyle/build/checkstyle.xml")
    configProperties = ["suppressionFile" : file("${rootDir}/config/checkstyle/build/suppressions.xml")]
}

checkstyleMain.dependsOn(":checkstyle:downloadCheckstyleRuleFiles")
checkstyleTest.dependsOn(":checkstyle:downloadCheckstyleRuleFiles")

def bDistribution = file("$project.buildDir/extracted-distribution/jballerina-tools-${ballerinaLangVersion}")
task jBallerinaPack {
    doLast {
        configurations.jbalTools.resolvedConfiguration.resolvedArtifacts.each { artifact ->
            copy {
                from project.zipTree(artifact.getFile())
                into new File(project.buildDir, "extracted-distribution/")
            }
        }
    }
    outputs.dir bDistribution
}

task unpackStdLibs() {
    doLast {
        configurations.ballerinaStdLibs.resolvedConfiguration.resolvedArtifacts.each { artifact ->
            copy {
                from project.zipTree(artifact.getFile())
                into new File("${project.buildDir}/extracted-distribution/" + artifact.name + "-zip")
            }
        }
    }
}

task copyStdlibs(type: Copy) {
    def ballerinaDist = "$project.buildDir/extracted-distribution/jballerina-tools-${ballerinaLangVersion}"
    into ballerinaDist

    duplicatesStrategy = DuplicatesStrategy.EXCLUDE
    /* Standard Libraries */
    configurations.ballerinaStdLibs.resolvedConfiguration.resolvedArtifacts.each { artifact ->
        def artifactExtractedPath = "${project.buildDir}/extracted-distribution/" + artifact.name + "-zip"
        into("repo/bala") {
            from "${artifactExtractedPath}/bala/"
        }
        into("repo/cache") {
            from "${artifactExtractedPath}/cache"
        }
    }
}

shadowJar {
    configurations = [project.configurations.runtimeClasspath]
    dependencies {
        include(dependency("commons-logging:commons-logging:${commonsLoggingVersion}"))
        include(dependency("com.graphql-java:graphql-java:${graphqlJavaVersion}"))
        exclude('META-INF/*.SF')
        exclude('META-INF/*.DSA')
        exclude('META-INF/*.RSA')
    }
}

task copyJar(type: Copy) {
    from "$project.buildDir/libs/graphql-code-generator-${version}.jar"
    into "$project.buildDir/extracted-distribution/jballerina-tools-${ballerinaLangVersion}/bre/lib"
}

compileJava {
    doFirst {
        options.compilerArgs = [
                '--module-path', classpath.asPath,
        ]
        classpath = files()
    }
}

unpackStdLibs.dependsOn jBallerinaPack
copyStdlibs.dependsOn unpackStdLibs
shadowJar.dependsOn copyStdlibs
copyJar.dependsOn shadowJar
copyJar.dependsOn jar

build.dependsOn(copyJar)
