plugins {
    id 'maven-publish'
    id 'groovy'
    id 'net.researchgate.release'
    id "com.gradle.plugin-publish" version "1.2.1"
    id 'com.jfrog.bintray' version '1.8.5'
}

task createClasspathManifest {
    def outputDir = file("$buildDir/$name")

    inputs.files sourceSets.main.runtimeClasspath
    outputs.dir outputDir

    doLast {
        outputDir.mkdirs()
        file("${outputDir}/plugin-classpath.txt").text = (sourceSets.main.runtimeClasspath + sourceSets.test.runtimeClasspath).join('\n')
    }
}

dependencies {
    implementation gradleApi()
    implementation localGroovy()

    implementation 'commons-io:commons-io:2.13.0'
    implementation 'org.apache.commons:commons-lang3:3.13.0'
    implementation 'org.apache.commons:commons-text:1.10.0'
    implementation 'com.github.jknack:handlebars:4.3.1'
    implementation "com.fasterxml.jackson.core:jackson-core:${rootProject.ext.jacksonVersion}"
    implementation "com.fasterxml.jackson.module:jackson-module-jaxb-annotations:${rootProject.ext.jacksonVersion}"
    implementation "com.fasterxml.jackson.module:jackson-module-scala_2.12:${rootProject.ext.jacksonVersion}"
    implementation 'javax.xml.bind:jaxb-api:2.3.1'

    implementation "io.swagger:swagger-jersey2-jaxrs:${rootProject.ext.swaggerVersion}"

    implementation "javax.ws.rs:javax.ws.rs-api:${rootProject.ext.javaxVersion}"
    implementation 'com.sun.jersey:jersey-server:1.19.4'
    implementation 'org.glassfish.jersey.core:jersey-server:3.1.3'
    implementation "org.springframework:spring-context:${rootProject.ext.springVersion}"
    implementation "org.springframework:spring-web:${rootProject.ext.springVersion}"

    testImplementation gradleTestKit()

    testImplementation project(':test-models')
    testImplementation project(':java-tests')
    testImplementation project(':kotlin-tests')
    testImplementation project(':scala-tests')
    testImplementation 'cglib:cglib-nodep:3.3.0'
    testImplementation 'org.objenesis:objenesis:3.3'
    testImplementation('org.spockframework:spock-core:2.3-groovy-4.0') {
        //We want to depend on the gradle version of groovy since this is a plugin
        exclude module: 'groovy-all'
    }

    testRuntimeOnly files(createClasspathManifest)
}

java {
    withSourcesJar()
}

test {
    useJUnitPlatform()
    if (System.properties['test.profile'] == 'integration') {
        include '**/*ITest*'
    } else if (System.properties['test.profile'] == 'unit') {
        exclude '**/*ITest*'
    }

    inputs.dir "$project.rootDir/externaltestdata"
    testLogging {
        events 'failed', 'passed', 'skipped'
        exceptionFormat 'full'
    }
    options {
        systemProperties(System.getProperties())
    }
}

if (project.hasProperty('bintray_user') && project.hasProperty('bintray_apiKey')) {
    bintray {
        user = bintray_user
        key = bintray_apiKey
        publications = ['pluginPublication']
        pkg {
            repo = 'maven'
            name = 'swagger-gradle-plugin'
            websiteUrl = 'https://github.com/gigaSproule/swagger-gradle-plugin'
            vcsUrl = 'https://github.com/gigaSproule/swagger-gradle-plugin'
            githubRepo = 'gigaSproule/swagger-gradle-plugin'
            githubReleaseNotesFile = 'README.md'
            licenses = ['Apache-2.0']
            publish = true
            version {
                name = project.version
                released = new Date()
            }
        }
    }

    afterReleaseBuild.dependsOn bintrayUpload
}

gradlePlugin {
    website = 'https://github.com/gigaSproule/swagger-gradle-plugin'
    vcsUrl = 'https://github.com/gigaSproule/swagger-gradle-plugin'
    description = 'Plugin to create Swagger documentation using Gradle'

    plugins {
        swagger {
            id = 'com.benjaminsproule.swagger'
            displayName = 'Swagger Gradle Plugin'
            tags.set(['swagger', 'documentation'])
            implementationClass = 'com.benjaminsproule.swagger.gradleplugin.GradleSwaggerPlugin'
        }
    }
}

publishing {
    publications {
        pluginPublication(MavenPublication) {
            from components.java
            groupId project.group
            artifactId 'swagger-gradle-plugin'
            version "${project.version}"
        }
    }
}

afterReleaseBuild.dependsOn publishPlugins

release {
    preCommitText = '[skip ci]'
}
