import org.gradle.internal.os.OperatingSystem
apply from: 'jpos/libraries.gradle'

allprojects {
    apply plugin: 'idea'
    apply plugin: 'eclipse'
}

subprojects {
    apply plugin: 'java-library'
    apply plugin: 'maven-publish'
    apply plugin: 'signing'
    apply plugin: 'pmd'
    apply plugin: 'jacoco'
    apply plugin: 'project-report'

    group = 'org.jpos'
    version = '2.1.15-SNAPSHOT'
    [ compileJava, compileTestJava, javadoc ]*.options*.encoding = 'UTF-8'
    def isSnapshot = version.contains("SNAPSHOT")
    def mavenCentralRepo = isSnapshot ?
        'https://oss.sonatype.org/content/repositories/snapshots/' :
        'https://oss.sonatype.org/service/local/staging/deploy/maven2';

    configurations.implementation.transitive = true
    javadoc.failOnError = false
    tasks.withType(Javadoc) {
        options.addStringOption('Xdoclint:none', '-quiet')
        failOnError = false
        enabled = false
    }
    pmd.ignoreFailures = true

    sourceCompatibility = 1.8
    targetCompatibility = 1.8
    repositories {
        mavenCentral()
        maven { url 'https://jpos.org/maven' }
        mavenLocal()
    }
    if (project.hasProperty("lint")) {
      // gradle -Plint ...
      tasks.withType(JavaCompile) {
        options.compilerArgs << "-Xlint" 
      } 
    }
    extensions.configure('java') { ext ->
        ext.withJavadocJar()
        ext.withSourcesJar()
    }
    publishing {
        publications {
            mavenJava(MavenPublication) {
                pom {
                    name = 'jPOS Project'
                    description = """
    jPOS is an ISO-8583 based financial transaction 
    library/framework that can be customized and 
    extended in order to implement financial interchanges.
  """
                    url = "http://jpos.org"
                    organization {
                        name = 'jPOS.org'
                        url = 'http://jpos.org'
                    }
                    issueManagement {
                        system = 'Github Issues'
                        url = 'https://github.com/jpos/jPOS/issues'
                    }
                    scm {
                        url = "http://github.com/jpos/jPOS"
                        connection = "scm:git:https://github.com/jpos/jPOS.git"
                        developerConnection = "scm:git:git@github.com:jpos/jPOS.git"
                    }
                    licenses {
                        license {
                            name = 'GNU AFFERO GENERAL PUBLIC LICENSE'
                            url = 'http://www.gnu.org/licenses/agpl-v3.html'
                            comments = 'See http://jpos.org/license for more details.'
                            distribution = 'repo'
                        }
                    }
                    developers {
                        developer {
                            id = 'jpos-team'
                            name = 'jPOS Development Team'
                            email = 'info@jpos.org'
                        }
                    }
                }
                groupId = 'org.jpos'
                artifactId = 'jpos'
                from components.java
            }
        }
        repositories {
            maven {
                def releasesRepoUrl = mavenCentralRepo
                def snapshotsRepoUrl = 'file:///opt/local/maven'
                url = isSnapshot ? snapshotsRepoUrl : releasesRepoUrl
                if (!isSnapshot) {
                    credentials {
                        if (project.hasProperty("mavenCentralUsername"))
                            username = mavenCentralUsername
                        if (project.hasProperty("mavenCentralPassword"))
                            password = mavenCentralPassword
                    }
                }
            }
        }
    }

    signing {
        required { !isSnapshot }
        sign publishing.publications.mavenJava
    }

    javadoc {
        configure(options) {
            windowTitle = "jPOS ${project.version}"
            header = "<b>jPOS ${project.version}</b>"
            linkSource = true
            links(
                'https://docs.oracle.com/javase/8/docs/api'
            )
            tags(
                'apiNote:a:API Note:',
                'implSpec:a:Implementation Requirements:',
                'implNote:a:Implementation Note:'
            )
        }
    }

    test {
        useJUnitPlatform()
        testLogging {
            events "passed", "skipped", "failed"
        }
        systemProperty 'user.language', 'en'
        if (!(System.getenv("GITHUB_ACTIONS") != null && System.getenv("GITHUB_ACTIONS") == "true" &&
                (OperatingSystem.current().isMacOsX() || OperatingSystem.current().isWindows()))) {
            maxParallelForks = Runtime.runtime.availableProcessors()
        }
    }
    jacoco {
        toolVersion = '0.8.6'
    }
}

// Configure IDEA to use Git
idea {
    project {
        ipr {
            withXml { 
                provider -> provider.node.component.find { 
                    it.@name == 'VcsDirectoryMappings' 
                }.mapping.@vcs = 'Git' 
            }
        }
        languageLevel = '1.8'
    }
}

task aggregatedJavadoc (type: Javadoc, description: "Aggregated Javadocs") {
    source subprojects.collect {project ->
        project.sourceSets.main.allJava
    }
    destinationDir = new File(buildDir, 'docs/javadoc')
    classpath = files(subprojects.collect {project ->
        project.sourceSets.main.compileClasspath})
    failOnError = false
}

