import com.github.jengelman.gradle.plugins.shadow.transformers.ManifestResourceTransformer

apply plugin: 'groovy'
apply plugin: 'shadow'
apply plugin: 'maven-publish'
apply plugin: 'com.jfrog.bintray'

group = 'com.8x8.cloud'
archivesBaseName = 'swagger2raml'
version = '1.0.0'

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.github.jengelman.gradle.plugins:shadow:0.8'
        classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:0.6'
    }
}

if (project.hasProperty('bintrayUser') && project.hasProperty('bintrayKey') && project.hasProperty('bintrayPassphrase')) {
    task sourcesJar(type: Jar, dependsOn: classes) {
        classifier = 'sources'
        from sourceSets.main.allSource
    }

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

    artifacts {
        archives sourcesJar, javadocJar
    }

    publishing {
        publications {
            mavenJava(MavenPublication) {
                from components.java
                artifact sourcesJar {
                    classifier "sources"
                }

                artifact javadocJar {
                    classifier "javadoc"
                }
                pom.withXml {
                    def devs = ['rucek': 'Jacek Kunicki', 'mmatloka': 'Michał Matłoka']
                    def root = asNode()
                    root.appendNode('name', 'swagger2raml')
                    root.appendNode('packaging', 'jar')
                    root.appendNode('url', 'https://github.com/8x8Cloud/swagger2raml')
                    root.appendNode('description', 'A utility to generate RAML documentation from Swagger JSON.')

                    def license = root.appendNode('licenses').appendNode('license')
                    license.appendNode('name', 'Apache-2.0')
                    license.appendNode('url', 'https://github.com/8x8Cloud/swagger2raml/blob/master/LICENSE.md')
                    license.appendNode('distribution', 'repo')

                    root.appendNode('scm').appendNode('url', 'https://github.com/8x8Cloud/swagger2raml.git')

                    def developers = root.appendNode('developers')
                    devs.each {
                        def d = developers.appendNode('developer')
                        d.appendNode('id', it.key)
                        d.appendNode('name', it.value)
                    }
                }
            }
        }
    }

    bintray {
        user = bintrayUser
        key = bintrayKey
        publications = ['mavenJava']
        publish = false
        pkg {
            repo = 'maven'
            userOrg = '8x8'
            name = 'swagger2raml'
            websiteUrl = 'https://github.com/8x8Cloud/swagger2raml'
            issueTrackerUrl = 'https://github.com/8x8Cloud/swagger2raml/issues'
            vcsUrl = 'https://github.com/8x8Cloud/swagger2raml.git'
            desc = 'A utility to generate RAML documentation from Swagger JSON.'
            licenses = ['Apache-2.0']
            licenses = ['groovy', 'swagger', 'raml']
            version {
                vcsTag = project.version
                gpg {
                    sign = true
                    passphrase = bintrayPassphrase
                }
            }
        }
    }

    bintrayUpload.dependsOn shadowJar
}

repositories {
    mavenCentral()
    maven { url 'http://oss.sonatype.org/content/groups/public/' }
    maven { url 'https://repository-master.mulesoft.org/releases/' }
}

dependencies {
    compile 'org.codehaus.groovy:groovy-all:2.3.6'
    compile 'commons-cli:commons-cli:1.2'
    testCompile 'org.spockframework:spock-core:0.7-groovy-2.0'
    testCompile 'org.raml:raml-parser:0.8.4'
}

test {
    systemProperties 'file.encoding': 'UTF-8'
}

shadow {
    outputFile new File("dist/swagger2raml-${project.version}-bundled.jar")

    exclude 'META-INF/*.SF'
    exclude 'META-INF/*.DSA'
    exclude 'META-INF/*.RSA'

    transformer(ManifestResourceTransformer) {
        attributes "Main-Class": 'com._8x8.cloud.swagger2raml.RamlGenerator'
    }
}

shadowJar.dependsOn build

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