import aQute.bnd.gradle.Bundle

group 'info.picocli'
description 'Java command line parser with both an annotations API and a programmatic API. Usage help with ANSI styles and colors. Autocomplete. Nested subcommands. Easily included as source to avoid adding a dependency.'
version "$projectVersion"
ext.moduleName = 'info.picocli'

buildscript {
    repositories {
        gradlePluginPortal()
        mavenCentral()
    }

    dependencies {
        classpath "org.asciidoctor:asciidoctor-gradle-jvm:$asciidoctorGradlePluginVersion"
        classpath 'org.asciidoctor:asciidoctorj-pdf:1.6.2'
        classpath "gradle.plugin.org.beryx:badass-jar:1.2.0"
        classpath 'biz.aQute.bnd:biz.aQute.bnd.gradle:6.1.0'
        classpath "io.codearte.gradle.nexus:gradle-nexus-staging-plugin:0.30.0"
    }
}

apply plugin: "org.beryx.jar"
apply plugin: 'io.codearte.nexus-staging'

if (System.getenv('MAVEN_OSS_USER')) { // on home system
    apply plugin: 'biz.aQute.bnd.builder'
} else {
    try { // otherwise, only apply if available
        Class.forName('aQute.bnd.gradle.BndPlugin')
        Class.forName('aQute.bnd.build.Project')
        apply plugin: 'biz.aQute.bnd.builder'
    } catch (Throwable ignored) {}
}
pluginManager.withPlugin('biz.aQute.bnd.builder') { // if plugin applied, execute this action
    configurations {
        bundleCompile
        baseline
    }
    dependencies {
        baseline('group': group, 'name': jar.archiveBaseName) {
            version {
                strictly "(,${jar.archiveVersion}["
            }
            transitive = false
        }
    }
    sourceSets {
        bundle
    }
    task bundle(type: Bundle) {
        from sourceSets.bundle.output
        bndfile = 'bnd.bnd'
        sourceSet = sourceSets.bundle
    }
}


allprojects {
    apply plugin: 'java'
    apply plugin: 'java-library' // to avoid https://github.com/gradle/gradle/issues/1118

    sourceCompatibility = !JavaVersion.current().isJava9Compatible() ?
            1.5 : JavaVersion.current().isJava11Compatible() ? 1.7 : 1.6
    targetCompatibility = !JavaVersion.current().isJava9Compatible() ?
            1.5 : JavaVersion.current().isJava11Compatible() ? 1.7 : 1.6

    compileJava.options.encoding = "UTF-8"
    compileTestJava.options.encoding = "UTF-8"

    repositories {
        maven { url 'https://repo.spring.io/libs-snapshot' }
        mavenCentral()
    }

    configurations.all {
        resolutionStrategy {
            // avoid "Could not resolve junit:junit-dep:[4.9,)" caused by stefanbirkner:system-rules when building offline
            force "junit:junit-dep:$junitDepVersion"
        }
    }
    dependencies {
        testImplementation "junit:junit:$junitVersion",
                           "org.hamcrest:hamcrest-core:$hamcrestCoreVersion",
                           "org.fusesource.jansi:jansi:$jansiVersion",
                           "org.codehaus.groovy:groovy-all:$groovyVersion",
                           "com.github.stefanbirkner:system-rules:$systemRulesVersion",
                           "pl.pragmatists:JUnitParams:1.1.1"
    }
    tasks.withType(JavaCompile) {
        options.compilerArgs << '-Xlint:unchecked' << '-Xlint:deprecation'
    }

    javadoc.destinationDir = file("build/docs/apidocs")
    javadoc.options.addStringOption('Xdoclint:none', '-quiet')

    // work around https://github.com/gradle/gradle/issues/4046
    javadoc.dependsOn('copyJavadocDocFiles')
    task copyJavadocDocFiles(type: Copy) {
        from('src/main/java')
        into 'build/docs/apidocs'
        include '**/doc-files/*.*'
    }
}

sourceSets.main.java.srcDirs = ['src/main/java', 'src/main/java9']
compileJava {
    inputs.property("moduleName", moduleName)
    doFirst {
        if (JavaVersion.current().isJava9Compatible()) {
            options.compilerArgs = [
                    '--module-path', classpath.asPath,
            ]
        }
        classpath = files()
    }
}

jar {
    manifest {
        attributes 'Specification-Title'   : 'picocli',
                   'Specification-Vendor'  : 'Remko Popma',
                   'Specification-Version' : archiveVersion.get(),
                   'Implementation-Title'  : 'picocli',
                   'Implementation-Vendor' : 'Remko Popma',
                   'Implementation-Version': archiveVersion.get(),
                   'Main-Class'            : 'picocli.AutoComplete'
    }
    // copy module-info.class to META-INF/versions/9
    multiRelease = true
}

// jacoco 0.8.2 does not work with Java 13; gradle 4.x has no JavaVersion enum value for Java 12
if (JavaVersion.current().isJava11Compatible()) {
    project.logger.lifecycle("skipping jacoco test for Java version ${JavaVersion.current()}")
} else {
    project.logger.lifecycle("applying jacoco build file for Java version ${JavaVersion.current()}")
    apply from: "gradle/jacoco.gradle"
}

if (JavaVersion.current().isCompatibleWith(JavaVersion.VERSION_18)) {
    // https://github.com/remkop/picocli/issues/1503
    test { // explicitly enable security manager on Java 18 for System.exit tests
        systemProperty "java.security.manager", "allow"
    }
}

// javadoc and asciidoc customization
apply from: "${rootProject.projectDir}/gradle/docs.gradle"

// publishing-related
ext {
    PUBLISH_GROUP_ID = group
    PUBLISH_ARTIFACT_ID = project.name
    PUBLISH_VERSION = "$projectVersion"
}
apply from: "${rootProject.projectDir}/gradle/publish-mavencentral.gradle"

// release-related custom gradle tasks and release procedure steps
apply from: "${rootProject.projectDir}/gradle/release-tasks.gradle"
