apply plugin: 'kotlin'
apply plugin: 'application'
apply plugin: 'org.junit.platform.gradle.plugin'
apply plugin: 'maven'
apply plugin: 'maven-publish'
apply plugin: 'com.jfrog.bintray'

mainClassName = 'com.gojuno.swarmer.MainKt'

dependencies {
    compile libraries.kotlinStd
    compile libraries.jCommander
    compile libraries.commanderOs
    compile libraries.commanderAndroid
    compile libraries.rxJava
}

dependencies {
    testCompile libraries.spek
    testRuntime libraries.spekJunitPlatformEngine
    testCompile libraries.assertJ
    testCompile libraries.mockito
    testCompile libraries.mockitoKotlin
}

jar {
    // Build jar with dependencies.
    from(configurations.compile.collect { it.isDirectory() ? it : zipTree(it) }) {
        exclude 'META-INF/*.SF'
        exclude 'META-INF/*.DSA'
        exclude 'META-INF/*.RSA'
    }

    manifest {
        attributes('Main-Class': mainClassName)
    }
}

junitPlatform {
    platformVersion = versions.junitPlatform

    filters {
        engines {
            include 'spek'
        }
    }
}

task sourcesJar(type: Jar, dependsOn: classes) {
    classifier = 'sources'
    from sourceSets.main.allSource
}

task javadocJar(type: Jar, dependsOn: javadoc) {
    classifier = 'javadoc'
    from javadoc.destinationDir
}

task validatePublishing {
    doLast {
        validateTagAndVersion()
    }
}

bintrayUpload.dependsOn validatePublishing

def pomConfig = {
    licenses {
        license {
            name 'The Apache Software License, Version 2.0'
            url 'http://www.apache.org/licenses/LICENSE-2.0.txt'
            distribution 'repo'
        }
    }
    developers {
        developer {
            id 'gojuno'
            name 'Juno Inc.'
            email 'opensource@gojuno.com'
        }
    }
}

publishing {
    publications {
        SwarmerPublication(MavenPublication) {
            from components.java

            artifact sourcesJar
            artifact javadocJar

            groupId 'com.gojuno.swarmer'
            artifactId 'swarmer'
            version projectVersion()

            pom.withXml {
                def root = asNode()
                root.appendNode('description', 'Tool to create and start multiple Android Emulators in parallel.')
                root.appendNode('name', 'Swarmer')
                root.appendNode('url', 'https://github.com/gojuno/swarmer')
                root.children().last() + pomConfig
            }
        }
    }
}

bintray {
    user = System.getenv('BINTRAY_USER')
    key = System.getenv('BINTRAY_API_KEY')
    publish = true

    pkg {
        repo = 'maven'
        name = 'swarmer'
        licenses = ['Apache-2.0']
        vcsUrl = 'https://github.com/gojuno/swarmer.git'
        issueTrackerUrl = 'https://github.com/gojuno/swarmer/issues'
        publications = ['SwarmerPublication']

        version {
            name = projectVersion()
            vcsTag = gitTag()

            gpg {
                sign = true
                passphrase = System.getenv('BINTRAY_GPG_PASSPHRASE')
            }
        }
    }
}
