/*
 * 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 - Transaction Ballerina Generator'

def packageName = "transaction"
def packageOrg = "ballerinai"
def tomlVersion = "0.0.0"
def ballerinaTomlFilePlaceHolder = new File("${project.rootDir}/build-config/resources/Ballerina.toml")
def ballerinaTomlFile = new File("$project.projectDir/Ballerina.toml")

ballerina {
    packageOrganization = packageOrg
    module = packageName
    langVersion = ballerinaLangVersion
    testCoverageParam = "--code-coverage --coverage-format=xml --includes=ballerinai.transaction.*:org.ballerinalang.stdlib.transaction.*"
    customTomlVersion = "0.0.0"
}

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

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"
            } else {
                commandLine 'sh', '-c', "git commit -m '[Automated] Update the native jar versions' Ballerina.toml Dependencies.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 ":transaction-native:build"
build.dependsOn ":transaction-ballerina:generatePomFileForMavenPublication"
test.dependsOn ":transaction-native:build"

publishToMavenLocal.dependsOn build
publish.dependsOn build
