/*
 * 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

buildscript {
    repositories {
        maven {
            url = 'https://maven.pkg.github.com/ballerina-platform/plugin-gradle'
            credentials {
                username System.getenv("packageUser")
                password System.getenv("packagePAT")
            }
        }
    }
    dependencies {
        classpath "io.ballerina:plugin-gradle:${project.ballerinaGradlePluginVersion}"
    }
}

description = 'Ballerina - File Ballerina Generator'

def packageName = "file"
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}", "")
    }
}

apply plugin: 'io.ballerina.plugin'

ballerina {
    packageOrganization = packageOrg
    module = packageName
    langVersion = ballerinaLangVersion
}

configurations {
    externalJars
}

dependencies {
    externalJars(group: 'org.wso2.transport.file', name: 'org.wso2.transport.local-file-system', version:
            "${transportVersion}") {
        transitive = false
    }
}

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

        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}-test-utils:build"
build.dependsOn ":${packageName}-compiler-plugin:build"
test.dependsOn ":${packageName}-native:build"
test.dependsOn ":${packageName}-test-utils:build"
test.dependsOn ":${packageName}-compiler-plugin:build"
