/*
 * Copyright 2016 Danny Althoff
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import org.gradle.plugins.signing.Sign

apply plugin: 'java'
apply plugin: 'maven'
apply plugin: 'signing'
apply plugin: 'checkstyle'

repositories {
    mavenLocal()
    mavenCentral()
}

dependencies {
    compile gradleApi()
    testCompile gradleTestKit()
    testCompile group: 'org.testng', name: 'testng', version: '6.10'
    // java 8
    if( file("${System.properties['java.home']}/../lib/ant-javafx.jar").exists() ){
        compile files("${System.properties['java.home']}/../lib/ant-javafx.jar")
    }
    // java 9
    if( file("${System.properties['java.home']}/lib/ant-javafx.jar").exists() ){
        compile files("${System.properties['java.home']}/lib/ant-javafx.jar")
    }
    compile 'org.ow2.asm:asm-all:5.1'
    runtime 'org.ow2.asm:asm-all:5.1'
}

group = 'de.dynamicfiles.projects.gradle.plugins'
archivesBaseName = 'javafx-gradle-plugin'
version = gradle.javafxGradlePluginVersion

ext.isReleaseVersion = !version.endsWith("SNAPSHOT")

task javadocJar(type: Jar) {
    classifier = 'javadoc'
    from javadoc
}

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

// http://stackoverflow.com/a/22991189/1961102
compileJava.dependsOn 'checkstyle'
task checkstyle(type: Checkstyle) {
    configFile file("${project.rootDir}/config/checkstyle/checkstyle.xml")
    configProperties.checkstyleConfigDir = file("${project.rootDir}/config/checkstyle")
    source 'src'
    include '**/*.java'
    classpath = files()
}
test {
    useTestNG()
    testLogging.showStandardStreams = true
}

artifacts {
    archives jar
    archives javadocJar
    archives sourcesJar
}
signing {
    required { isReleaseVersion }
    sign configurations.archives
}

// read some information when required
// https://docs.gradle.org/current/userguide/signing_plugin.html
gradle.taskGraph.whenReady { taskGraph ->
    if (taskGraph.allTasks.any { it instanceof Sign } && isReleaseVersion ) {
        // Use Java 6's console to read from the console (no good for
        // a CI environment)
        Console console = System.console()
        console.printf "\n\nWe have to sign some things in this build." +
                       "\n\nPlease enter your signing details.\n\n"

        def password = console.readPassword("PGP Private Key Password: ")

        allprojects { ext."signing.password" = password }

        console.printf "\nThanks.\n\n"
    }
}
// http://stackoverflow.com/a/27741137/1961102
if(!hasProperty('ossrhUsername')) {
    ext.ossrhUsername = ''
}
if(!hasProperty('ossrhPassword')) {
    ext.ossrhPassword = ''
}
uploadArchives {
    repositories {
        mavenDeployer {
            if(isReleaseVersion) {
                beforeDeployment { MavenDeployment deployment -> signing.signPom(deployment) }
            }
            repository(url: "https://oss.sonatype.org/service/local/staging/deploy/maven2/") {
                authentication(userName: ossrhUsername, password: ossrhPassword)
            }

            snapshotRepository(url: "https://oss.sonatype.org/content/repositories/snapshots/") {
                authentication(userName: ossrhUsername, password: ossrhPassword)
            }

            pom.project {
                name 'javafx-gradle-plugin'
                packaging 'jar'

                description 'The JavaFX Gradle Plugin provides a way to to assemble distributable bundles for JavaFX applications from within Gradle. It provides a wrapper around the JavaFX packaging tools which are provided as part of the JavaFX installation.'
                url 'https://github.com/FibreFoX/javafx-gradle-plugin'

                scm {
                    connection 'scm:git:git://github.com/FibreFoX/javafx-gradle-plugin.git'
                    developerConnection 'scm:git:git@github.com:FibreFoX/javafx-gradle-plugin.git'
                    url 'https://github.com/FibreFoX/javafx-gradle-plugin'
                }

                licenses {
                    license {
                        name 'The Apache License, Version 2.0'
                        url 'http://www.apache.org/licenses/LICENSE-2.0.txt'
                        distribution 'repo'
                    }
                }

                developers {
                    developer {
                        id 'FibreFoX'
                        name 'Danny Althoff'
                        email 'fibrefox@dynamicfiles.de'
                    }
                }

                profiles {
                    profile {
                        id 'java9-dependencies'
                        activation {
                            file {
                                exists '${java.home}/lib/ant-javafx.jar'
                            }
                        }
                        dependencies {
                            dependency {
                                groupId 'javafx-packager'
                                artifactId 'javafx-packager'
                                version '9'
                                scope 'system'
                                systemPath '${java.home}/lib/ant-javafx.jar'
                            }
                        }
                    }
                    profile {
                        id 'java8-dependencies'
                        activation {
                            file {
                                exists '${java.home}/../lib/ant-javafx.jar'
                            }
                        }
                        dependencies {
                            dependency {
                                groupId 'javafx-packager'
                                artifactId 'javafx-packager'
                                version '1.8'
                                scope 'system'
                                systemPath '${java.home}/../lib/ant-javafx.jar'
                            }
                        }
                    }
                }
            }
        }
    }
}
