plugins {
    id 'com.gradle.build-scan' version '2.0.2'
    id "com.gradle.plugin-publish" version "0.9.4"
    id "com.jfrog.bintray" version "1.6"
}
apply plugin: 'groovy'
apply plugin: 'maven'
apply plugin: 'maven-publish'
apply plugin: 'signing'
apply plugin: 'jacoco'
apply plugin: 'project-report'

sourceCompatibility = 1.6
targetCompatibility = 1.6

defaultTasks 'clean', 'build'

version = '2.14.1'
group = 'com.eriwen'
ext.archivesBaseName = 'gradle-js-plugin'
ext.isSnapshot = version.endsWith("-SNAPSHOT")

buildScan {
    termsOfServiceUrl = 'https://gradle.com/terms-of-service'
    termsOfServiceAgree = 'yes'
}

repositories {
    jcenter()
}

task createClasspathManifest {
    def outputDir = sourceSets.test.output.resourcesDir

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

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

dependencies {
    compile gradleApi()
    compile('com.google.javascript:closure-compiler:v20160208') {
        exclude module: 'junit'
    }
    compile('io.jdev.html2js:html2js:0.1') {
        exclude module: 'groovy-all'
    }
    testCompile gradleTestKit()
    testCompile files(createClasspathManifest)
    testCompile('org.spockframework:spock-core:1.0-groovy-2.4') {
        exclude module: 'junit-dep'
        exclude module: 'groovy-all'
    }
    testCompile 'commons-lang:commons-lang:2.6'
}

compileGroovy {
    options.compilerArgs = ['-Xlint:deprecation', '-Xlint:unchecked']
}

task sourceJar(type: Jar) {
    description = 'An archive of the source code for Maven Central'
    classifier = 'sources'
    from sourceSets.main.groovy
}

task groovydocJar(type: Jar) {
    description = 'An archive of the GroovyDocs for Maven Central'
    classifier = 'javadoc'
    from groovydoc
}

artifacts {
    archives groovydocJar, sourceJar
}

publishing {
    publications {
        maven(MavenPublication) {
            if (plugins.hasPlugin('war')) {
                from components.web
            } else {
                from components.java
            }

            artifact sourceJar {
                classifier 'sources'
            }

            artifact groovydocJar {
                classifier 'javadoc'
            }
        }
    }
}

pluginBundle {
    website = 'https://github.com/eriwen/gradle-js-plugin/'
    vcsUrl = 'https://github.com/eriwen/gradle-js-plugin/'
    description = 'Gradle plugin for working with JS'
    tags = ['javascript', 'jshint', 'requirejs']

    plugins {
        jsPlugin {
            id = 'com.eriwen.gradle.js'
            displayName = 'Gradle JavaScript Plugin'
        }
    }
    mavenCoordinates {
        groupId = 'com.eriwen'
        artifactId = 'gradle-js-plugin'
    }
}

signing {
    sign configurations.archives
}

bintray {
    user = System.properties['bintrayUsername']
    key = System.properties['bintrayApiKey']
    publications = ['maven']
    pkg {
        repo = 'gradle-plugins'
        name = 'gradle-js-plugin'
        desc = 'Gradle plugin for working with JS.'
        licenses = ['Apache-2.0']
        labels = ['gradle', 'js']
    }
}

test {
    systemProperties['version'] = version
    testLogging {
        stackTraceFilters 'truncate', 'groovy'
        events 'passed', 'skipped', 'failed'
        exceptionFormat = 'full'
    }
}

jacoco {
    toolVersion = "0.7.6.201602180812"
    reportsDir = file("$buildDir/customJacocoReportDir")
}

jacocoTestReport {
    reports {
        xml.enabled false
        csv.enabled false
        html.destination file("${buildDir}/jacocoHtml")
    }
}

jacocoTestCoverageVerification {
    violationRules {
        rule {
            element = 'PACKAGE'
            includes = ['com.eriwen.gradle.*']

            limit {
                counter = 'LINE'
                value = 'COVEREDRATIO'
                minimum = 0.6
            }
            limit {
                counter = 'CLASS'
                value = 'MISSEDCOUNT'
                maximum = 0
            }
        }
    }
}

// Only *require* signing if we are uploading a non snapshot version
gradle.taskGraph.whenReady { taskGraph ->
    tasks.withType(org.gradle.plugins.signing.Sign).all {
        required = taskGraph.hasTask(":uploadArchives") && !isSnapshot
    }
}

install.repositories.mavenInstaller {
    pom.project(pomConfiguration)
    if (signing.signatory) {
        beforeDeployment { signing.signPom(it) }
    }
}

uploadArchives {
    repositories.mavenDeployer {
        if (signing.signatory) {
            beforeDeployment { signing.signPom(it) }
        }
        name = 'mavenCentralReleaseDeployer'
        repository(url: 'https://oss.sonatype.org/service/local/staging/deploy/maven2/') {
            authentication(userName: System.properties['mavenCentralUsername'], password: System.properties['mavenCentralPassword'])
            releases(updatePolicy: 'always')
            snapshots(updatePolicy: 'always')
        }
        pom.project(pomConfiguration)
    }
}

/**
 * Create POM config and return for use by other tasks.
 */
def getPomConfiguration() {
    return {
        name 'Gradle JS Plugin'
        packaging 'jar'
        description 'A Gradle plugin for working with JS.'
        url 'https://github.com/eriwen/gradle-js-plugin'
        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 'eriwen'
                name 'Eric Wendelin'
                email 'me@eriwen.com'
            }
        }
        scm {
            connection 'scm:https://eriwen@github.com/eriwen/gradle-js-plugin'
            developerConnection 'scm:git@github.com:eriwen/gradle-js-plugin.git'
            url 'https://github.com/eriwen/gradle-js-plugin'
        }
    }
}

// download dependencies all at once, keeps downloads out of travis output
task resolveAllDependencies {
    doLast {
        configurations.all { it.resolve() }
    }
}
