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

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

plugins {
    id 'io.ballerina.plugin'
}

description = 'Ballerina - GraphQL Tool'

def packageName = "tool.graphql"
def packageOrg = "ballerina"
def tomlVersion = stripBallerinaExtensionVersion("${project.version}")
def ballerinaTomlFilePlaceHolder = new File("${project.rootDir}/config/resources/Ballerina.toml")
def balToolTomlFilePlaceHolder = new File("${project.rootDir}/config/resources/BalTool.toml")
def ballerinaTomlFile = new File("$project.projectDir/Ballerina.toml")
def balToolTomlFile = new File("$project.projectDir/BalTool.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
}

configurations {
    externalJars
}

dependencies {
    externalJars (group: 'com.graphql-java', name: 'graphql-java', version: "${graphqlJavaVersion}") {
        transitive = false
    }
}

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

        def newBalToolTomlFile = balToolTomlFilePlaceHolder.text.replace("@project.version@", project.version)
        newBalToolTomlFile = newBalToolTomlFile.replace("@graphql.java.version@", graphqlJavaVersion)
        balToolTomlFile.text = newBalToolTomlFile
    }
}

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

publishing {
    publications {
        maven(MavenPublication) {
            artifact source: createArtifactZip, extension: 'zip'
        }
    }
    repositories {
        maven {
            name = "GitHubPackages"
            url = uri("https://maven.pkg.github.com/ballerina-platform/graphql-tools")
            credentials {
                username = System.getenv("packageUser")
                password = System.getenv("packagePAT")
            }
        }
    }
}

updateTomlFiles.dependsOn copyStdlibs
build.dependsOn "generatePomFileForMavenPublication"
build.dependsOn ":graphql-code-generator:build"
build.dependsOn ":graphql-schema-file-generator:build"
build.dependsOn ":graphql-cli:build"

publishToMavenLocal.dependsOn build
publish.dependsOn build
