buildscript {

    def a_user = hasProperty('artifactory_user') ? artifactory_user : System.getenv('artifactory_user')
    def a_password = hasProperty('artifactory_password') ? artifactory_password : System.getenv('artifactory_password')

    repositories {
        mavenCentral()
        jcenter()
        maven {
            url 'http://dl.bintray.com/cbeust/maven'
        }
        maven {
            url 'http://oss.jfrog.org/artifactory/plugins-release'
            credentials {
                username = "${a_user}"
                password = "${a_password}"
            }
        }
    }

    dependencies {
        classpath "io.codearte.gradle.nexus:gradle-nexus-staging-plugin:0.5.3"
    }
}

plugins {
    id "com.jfrog.bintray" version "1.6"
    id "com.jfrog.artifactory" version "4.3.0"
    id "org.sonarqube" version "2.0.1"
    id "nebula.optional-base" version "3.0.3"
    id "nebula.provided-base" version "3.0.3"
}

task wrapper(type: Wrapper) {
    gradleVersion = '2.13'
}

group = 'org.testng'
version = '6.9.12-SNAPSHOT'

apply plugin: 'java'
apply plugin: 'groovy'
apply plugin: 'jacoco'
apply plugin: 'osgi'

targetCompatibility = "1.7"
sourceCompatibility = "1.7"

repositories {
    mavenCentral()
    jcenter()
    maven {
        url 'http://dl.bintray.com/cbeust/maven'
    }
}

dependencies {
    compile 'org.beanshell:bsh:2.0b4'
    compile 'com.beust:jcommander:1.48'

    compile 'org.apache.ant:ant:1.7.0'
    compile 'junit:junit:4.12', optional
    compile 'org.yaml:snakeyaml:1.15', optional

    provided 'com.google.inject:guice:4.0:no_aop'

    testCompile 'org.assertj:assertj-core:2.0.0'
    testCompile 'org.testng:testng:6.9.4'
    testCompile 'org.codehaus.groovy:groovy-all:2.4.7'
    testCompile 'org.spockframework:spock-core:1.0-groovy-2.4'
}

task sourceJar(type: Jar) {
    group 'Build'
    description 'An archive of the source code'
    classifier 'sources'
    from sourceSets.main.allSource
}

artifacts {
    sourceJar
}

import org.apache.tools.ant.filters.ReplaceTokens

def generatedSourcesFolder = projectDir.toString() + '/src/generated/java'

def dirFrom = projectDir.toString() + '/src/main/resources/org/testng/internal'
def dirTo = generatedSourcesFolder + "/org/testng/internal"
def fileFrom = 'VersionTemplateJava'
def fileTo = 'Version.java'

task removeVersion {
    delete dirTo + fileTo
}

sourceSets {
    generated {
        java {
            srcDir 'src/generated/java'
        }
        resources {
            srcDir 'src/generated/resources'
        }
    }
}

sourceSets {
    main {
        compileClasspath += generated.output
        runtimeClasspath += generated.output
    }
}

gradle.projectsEvaluated {
    compileJava.dependsOn(myDir)
}

task myDir {
    delete dirTo + "/" + fileTo
    mkdir(dirTo)
}

// Include the generated Version.class in the jar
jar {
    manifest {
        instruction 'Bundle-License', 'http://apache.org/licenses/LICENSE-2.0'
        instruction 'Bundle-Description', 'TestNG is a testing framework.'
        instruction 'Import-Package',
            'bsh.*;version="[2.0.0,3.0.0)";resolution:=optional',
            'com.beust.jcommander.*;version="[1.7.0,3.0.0)";resolution:=optional',
            'com.google.inject.*;version="[1.2,1.3)";resolution:=optional',
            'junit.framework;version="[3.8.1, 5.0.0)";resolution:=optional',
            'org.junit.*;resolution:=optional',
            'org.apache.tools.ant.*;version="[1.7.0, 2.0.0)";resolution:=optional',
            'org.yaml.*;version="[1.6,2.0)";resolution:=optional',
            '!com.beust.testng',
            '!org.testng.*',
            '!com.sun.*',
            '*'
    }
    from "$buildDir/classes/generated"
}

task createVersion(type: Copy, dependsOn: myDir) {
    println("Creating Version file: ${version} in ${dirTo}")
    from dirFrom
    include fileFrom
    into(dirTo)
    rename(fileFrom, fileTo)
    filter(ReplaceTokens, tokens: [version: version])
}

compileJava.dependsOn(createVersion)

test {
    useTestNG() {
        suites 'src/test/resources/testng.xml'
    }
//    testLogging.showStandardStreams = true
    systemProperties = System.getProperties()
    systemProperties['test.resources.dir'] = 'build/resources/test/'
}

if (JavaVersion.current().isJava8Compatible()) {
    allprojects {
        tasks.withType(Javadoc) {
            options.addStringOption('Xdoclint:none', '-quiet')
        }
    }
}

sonarqube {
    properties {
        property "sonar.host.url", "https://nemo.sonarqube.org"
        property "sonar.github.repository", "cbeust/testng"
        property "sonar.github.login", "testng-bot"
    }
}

apply from: 'gradle/publishing.gradle'
