/**
 * settings and plugins for build
 */
plugins {
    id "base"
    id "maven"
    id "signing"
    id "it.gianluz.capsule" version "1.0.3"
    id "biz.aQute.bnd.builder" version "4.1.0"
    id 'org.openjfx.javafxplugin' version '0.0.7' apply false
}

/**
 * group and version (artifact is set from rootProject.name in settings.gradle
 */
group "com.glazedlists"
version "$nextReleaseVersion${isSnapshot == "true" ? "-SNAPSHOT" : ""}"

/**
 * variables
 */
ext.targetDir = "target/"
ext.user = System.getProperty("user.name")
ext.buildTime = new Date()
def exportedProjects = subprojects.findAll { it.name != 'issuesbrowser' && it.name != 'jmh-benchmark' }

/**
 * settings for all modules including root-module
 */
allprojects {
    sourceCompatibility = javaTargetVersion
    targetCompatibility = javaTargetVersion
}

/**
 * settings for subprojects = everything except the root-project
 */
subprojects {
    apply plugin: 'java'
    tasks.withType(JavaCompile) {
        options.encoding = "UTF-8"
    }
    tasks.withType(Javadoc) {
        options.encoding = "UTF-8"
    }

    repositories {
        jcenter()

        // for SWT
        maven {
            url "http://maven-eclipse.github.io/maven"
        }
    }

    /* dependencies for test */
    dependencies {
        testCompile 'junit:junit:4.11'
        testCompile 'org.awaitility:awaitility:4.0.3'
        testCompile 'org.assertj:assertj-core:3.16.1'
    }
    test {
        systemProperties 'java.awt.headless': 'true'
        maxHeapSize = "512m"

        if (project.hasProperty('excludeTests')) { 
            exclude project.property('excludeTests')
        } 

        testLogging {
            events "skipped", "failed"
            exceptionFormat "full"
        }
    }

    jar.archiveName = "glazedlists-${name}.jar"
}

/**
 * settings for subprojects except core
 */
configure(subprojects.findAll { it.name != 'core' }) {
    /* everything depends on core */
    dependencies {
        compile project(':core')
        testCompile project(':core').sourceSets.test.output
    }
}

/**
 * add directories to clean of root-project
 */
clean.doLast {
    file(targetDir).deleteDir()
}


/**
 * JavaDoc
 */
task docs(type: Javadoc) {
    options.addStringOption('Xdoclint:none', '-quiet')
    options.jFlags('-Dhttp.agent=gradle-javadoc')
    classpath = files(exportedProjects.collect { it.sourceSets.main.compileClasspath + it.sourceSets.main.output })
    source = exportedProjects.collect { it.sourceSets.main.allJava }
    destinationDir = file("$targetDir/docs/api")
    title = "Glazed Lists $version"
    exclude '**/impl/**'

    options {
        author true
        use = true
        bottom """<a href="http://www.glazedlists.com" target="_top">Glazed Lists</a>, Copyright &#169; 2003 publicobject.com, O&apos;Dell Engineering.<br>Documentation build by $user at $buildTime"""

        links(
            "http://docs.oracle.com/javase/8/docs/api/",
            "http://help.eclipse.org/neon/topic/org.eclipse.platform.doc.isv/reference/api/",
            "http://doc.formdev.com/jgoodies-forms/",
            "http://nachocalendar.sourceforge.net/apidocs/",
            "http://www.jfree.org/jfreechart/api/javadoc/",
	    "https://static.javadoc.io/org.swinglabs.swingx/swingx-all/1.6.5-1/",
            "https://docs.jboss.org/hibernate/orm/current/javadocs/"
        )
    }
}

/**
 * JavaDoc-Jar
 */
task javadocjar(type: Jar, dependsOn: "docs") {
	classifier = 'javadoc'
    destinationDir = file(targetDir)
    archiveName = "${rootProject.name}-${version}-javadoc.jar"
    from fileTree("$targetDir/docs/api")
}

/**
 * Source-Jar
 */
task sourcejar(type: Jar) {
	classifier = 'sources'
    destinationDir = file(targetDir)
    archiveName = "${rootProject.name}-$version-sources.jar"
    includeEmptyDirs = false
    from files(
        subprojects.collect { it.sourceSets.main.allSource }
    )
}

/**
 * Jar (osgi compatible)
 */
jar {
    destinationDir = file(targetDir)
    archiveName = "${rootProject.name}-${version}.jar"

    apply from: 'glazedlists.bnd.gradle'
    bndfile = makeBNDFile()

    from files(
        exportedProjects.collect { it.sourceSets.main.output }
    )
}

/**
 * Dist
 */
import org.apache.tools.ant.DirectoryScanner
task dist(type: Zip) {
    /* fileTree uses ANT default excludes, but .gitignore should not be excluded */
    doFirst {
      DirectoryScanner.removeDefaultExclude "**/.gitignore"
    }
    
    destinationDir = file(targetDir)
    archiveName = "${rootProject.name}-$version-dist.zip"
    includeEmptyDirs = false
    from fileTree(".") {
        exclude ".git"
        exclude "www"
        exclude "**/.classpath"
        exclude "**/.project"
        exclude "**/.settings/*"
        file(".gitignore").eachLine {
            exclude it
        }
    }
    
    doLast {
      DirectoryScanner.resetDefaultExcludes()
    }    
}

/**
 * Build
 */
build.dependsOn(["jar", "sourcejar", "javadocjar", "dist"])

artifacts {
    archives javadocjar, sourcejar, dist
}

signing {
    required { gradle.taskGraph.hasTask("uploadArchives") }

    sign configurations.archives
}

uploadArchives {
    repositories {
        mavenDeployer {
            beforeDeployment { MavenDeployment deployment -> signing.signPom(deployment) }

            repository(url: "${mavenStagingRepositoryUrl}") {
                authentication(userName: sonatypeUsername, password: sonatypePassword)
            }

            snapshotRepository(url: "${mavenSnapshotsRepositoryUrl}") {
                authentication(userName: sonatypeUsername, password: sonatypePassword)
            }

            pom.project {
                packaging 'jar'
                name "Glazed Lists"
                description "Event-driven lists for dynamically filtered and sorted tables"

                url "http://www.glazedlists.com"

                issueManagement {
                    system "GitHub Issues"
                    url "https://github.com/glazedlists/glazedlists/issues"
                }

                inceptionYear 2003

                scm {
                    connection "scm:git:git://github.com/glazedlists/glazedlists"
                    developerConnection "scm:git:git://github.com/glazedlists/glazedlists"
                    url "https://github.com/glazedlists/glazedlists"
                }

                licenses {
                    license {
                        name "GNU Lesser General Public License"
                        url "http://www.gnu.org/copyleft/lesser.html"
                        distribution "repo"
                    }
                    license {
                        name "Mozilla Public License Version 1.1"
                        url "http://www.mozilla.org/MPL/MPL-1.1.html"
                        distribution "repo"
                    }
                }

                developers {
                    developer {
                        id "jessewilson"
                        name "Jesse Wilson"
                        email "jesse@swank.ca"
                        roles {
                            role "Java developer"
                        }
                        timezone "PDT"
                    }
                    developer {
                        id "jplemieux"
                        name "James Lemieux"
                        email ""
                        roles {
                            role "Java developer"
                        }
                        timezone "PDT"
                    }
                    developer {
                        id "kevinmaltby"
                        name "Kevin Maltby"
                        email ""
                        roles {
                            role "Java developer"
                        }
                        timezone "EST"
                    }
                    developer {
                        id "robeden"
                        name "Rob Eden"
                        email "robeden1@gmail.com"
                        roles {
                            role "Java developer"
                        }
                        timezone "CST"
                    }
                    developer {
                        id "brands"
                        name "Holger Brands"
                        email ""
                        roles {
                            role "Java developer"
                        }
                        timezone "CET"
                    }
                }

                contributors {
                    contributor {
                        name "Geoffrey De Smet"
                        email "ge0ffrey.spam_AT_gmail.com"
                        roles {
                            role "Maven2izer"
                        }
                        timezone "+1"
                    }
                    contributor {
                        name "Fabian Zeindl"
                        email ""
                        roles {
                            role "Java developer"
                        }
                        timezone "CET"
                    }
                }
                dependencies {
                    dependency {
                        groupId "junit"
                        artifactId "junit"
                        version "4.11"
                        scope "test"
                    }
                }
            }
        }
    }
}
uploadArchives.dependsOn sourcejar
uploadArchives.dependsOn javadocjar
uploadArchives.dependsOn dist
