/*
 * Copyright (c) 2020, WSO2 Inc. (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.
 *
 */

import org.apache.tools.ant.taskdefs.condition.Os

plugins {
    id 'io.ballerina.plugin'
}

description = 'Ballerina - GraphQl Ballerina'

def packageName = "graphql"
def packageOrg = "ballerina"
def tomlVersion = stripBallerinaExtensionVersion("${project.version}")
def ballerinaTomlFilePlaceHolder = new File("${project.rootDir}/build-config/resources/Ballerina.toml")
def compilerPluginTomlFilePlaceHolder = new File("${project.rootDir}/build-config/resources/CompilerPlugin.toml")
def ballerinaTomlFile = new File("$project.projectDir/Ballerina.toml")
def compilerPluginTomlFile = new File("$project.projectDir/CompilerPlugin.toml")

def stripBallerinaExtensionVersion(String extVersion) {
    if (extVersion.matches(project.ext.timestampedVersionRegex)) {
        def splitVersion = extVersion.split('-')
        if (splitVersion.length > 3) {
            def strippedValues = splitVersion[0..-4]
            return strippedValues.join('-')
        } else {
            return extVersion
        }
    } else {
        return extVersion.replace("${project.ext.snapshotVersion}", "")
    }
}

ballerina {
    packageOrganization = packageOrg
    module = packageName
    langVersion = ballerinaLangVersion
    testCoverageParam = "--code-coverage --coverage-format=xml --includes=io.ballerina.stdlib.graphql.*:ballerina.graphql*"
}

task updateTomlFiles {
    doLast {
        def newBallerinaToml = ballerinaTomlFilePlaceHolder.text.replace("@project.version@", project.version)
        newBallerinaToml = newBallerinaToml.replace("@toml.version@", tomlVersion)
        ballerinaTomlFile.text = newBallerinaToml

        def newCompilerPluginToml = compilerPluginTomlFilePlaceHolder.text.replace("@project.version@", project.version)
        compilerPluginTomlFile.text = newCompilerPluginToml
    }
}

task commitTomlFiles {
    doLast {
        project.exec {
            ignoreExitValue true
            if (Os.isFamily(Os.FAMILY_WINDOWS)) {
                commandLine 'cmd', '/c', "git commit -m \"[Automated] Update the native jar versions\" Ballerina.toml Dependencies.toml CompilerPlugin.toml"
            } else {
                commandLine 'sh', '-c', "git commit -m '[Automated] Update the native jar versions' Ballerina.toml Dependencies.toml CompilerPlugin.toml"
            }
        }
    }
}

publishing {
    publications {
        maven(MavenPublication) {
            artifact source: createArtifactZip, extension: 'zip'
        }
    }
    repositories {
        maven {
            name = "GitHubPackages"
            url = uri("https://maven.pkg.github.com/ballerina-platform/module-${packageOrg}-${packageName}")
            credentials {
                username = System.getenv("publishUser")
                password = System.getenv("publishPAT")
            }
        }
    }
}

updateTomlFiles.dependsOn copyStdlibs

build.dependsOn "generatePomFileForMavenPublication"
build.dependsOn ":${packageName}-native:build"
build.dependsOn ":${packageName}-compiler-plugin:build"
build.dependsOn ":${packageName}-commons:build"
test.dependsOn ":${packageName}-native:build"
test.dependsOn ":${packageName}-compiler-plugin:build"
test.dependsOn ":${packageName}-commons:build"

publishToMavenLocal.dependsOn build
publish.dependsOn build
