/*
 *  Copyright (c) 2022, WSO2 LLC. (http://www.wso2.com) All Rights Reserved.
 *
 *  WSO2 LLC. licenses this file to you 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.
 */

apply from: "$rootDir/gradle/javaProject.gradle"

description = 'Model generator core for project design diagram generation'

configurations.all {
    resolutionStrategy.preferProjectModules()
}

configurations {
    balTools
}

dependencies {
    implementation project(":model-generator-commons")

    implementation "org.ballerinalang:language-server-commons:${ballerinaLangVersion}"
    implementation "org.ballerinalang:ballerina-lang:${ballerinaLangVersion}"
    implementation "org.ballerinalang:ballerina-parser:${ballerinaLangVersion}"
    implementation "org.ballerinalang:ballerina-tools-api:${ballerinaLangVersion}"
    implementation "org.ballerinalang:ballerina-runtime:${ballerinaLangVersion}"
    implementation "com.google.code.gson:gson:${gsonVersion}"

    testImplementation "org.testng:testng:${testngVersion}"

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

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

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

task copyStdlibs() {
    dependsOn(unpackStdLibs)
    doLast {
        /* Standard Libraries */
        configurations.ballerinaStdLibs.resolvedConfiguration.resolvedArtifacts.each { artifact ->
            def artifactExtractedPath = "${project.buildDir}/extracted-stdlibs/" + artifact.name + '-zip'
            project.copy {
                def ballerinaDist = "build/extracted-distribution/jballerina-tools-${project.ballerinaLangVersion}"
                into ballerinaDist
                into('repo/bala') {
                    from "${artifactExtractedPath}/bala"
                }
                into('repo/cache') {
                    from "${artifactExtractedPath}/cache"
                }
            }
        }
    }
}

test {
    dependsOn {
        copyStdlibs
    }
    systemProperty "ballerina.home", balDistribution

    useTestNG() {
        suites "src/test/resources/testng.xml"
    }
}

ext.moduleName = 'io.ballerina.architecturemodelgenerator.core'

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

defineSyncTask("bre/lib")
