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 {
    apply from: 'dependencies.gradle'

    repositories {
        gradlePluginPortal()
        mavenCentral()
    }

    dependencies {
        classpath "org.asciidoctor:asciidoctor-gradle-jvm:$asciidoctorGradlePluginVersion"
        classpath 'org.asciidoctor:asciidoctorj-pdf:2.3.15'
        classpath "org.beryx:badass-jar:2.0.0"
        classpath 'biz.aQute.bnd:biz.aQute.bnd.gradle:6.4.0'
        classpath "io.codearte.gradle.nexus:gradle-nexus-staging-plugin:0.30.0"
        classpath "net.ltgt.gradle:gradle-errorprone-plugin:3.1.0"
    }
}

apply plugin: 'org.beryx.jar' // for compiling module-info on Java 8
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

    // https://errorprone.info/docs/installation requires Java 11+
    if (JavaVersion.current().isJava11Compatible()) {
        apply plugin: "net.ltgt.errorprone"

        dependencies {
            errorprone("com.google.errorprone:error_prone_core:2.27.1")
        }

        tasks.withType(JavaCompile).configureEach {
           options.errorprone.disableAllWarnings = true
        }
    }

    if (!JavaVersion.current().isJava9Compatible()) {
        sourceCompatibility = 1.5
        targetCompatibility = 1.5
    } else { // from Java 9 and up
        sourceCompatibility = 1.8
        targetCompatibility = 1.8
//        tasks.withType(JavaCompile).configureEach {
//            javaCompiler = javaToolchains.compilerFor {
//                languageVersion = JavaLanguageVersion.of(JavaVersion.current().majorVersion)
//            }
//        }
    }

    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 supportDependencies.jansi
        testImplementation supportDependencies.groovy

        testImplementation supportDependencies.junit
        testImplementation supportDependencies.hamcrestCore
        testImplementation supportDependencies.systemRules
        testImplementation supportDependencies.junitParams
    }
    tasks.withType(JavaCompile) {
        options.compilerArgs << '-Xlint:unchecked' << '-Xlint:deprecation'
    }

    javadoc.destinationDir = file("build/docs/apidocs")
    javadoc.options.addStringOption('Xdoclint:none', '-quiet')
    // Workaround frpm https://bugs.openjdk.org/browse/JDK-4973681
    javadoc.options.addStringOption('sourcepath', 'src/main/java')

    normalization {
        runtimeClasspath {
            metaInf {
                ignoreAttribute("Bnd-LastModified")
            }
        }
    }
}

//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()
//    }
}
moduleConfig {
    // copy module-info.class to META-INF/versions/9
    multiReleaseVersion = 9
    moduleInfoPath = 'src/main/java9/module-info.java'
    version = project.version
    neverCompileModuleInfo = true
}
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'
    }
}
configurations {
    apiElements {
        canBeConsumed = true
        canBeResolved = false
        attributes {
            attribute(Category.CATEGORY_ATTRIBUTE, objects.named(Category, Category.LIBRARY))
            attribute(Usage.USAGE_ATTRIBUTE, objects.named(Usage, Usage.JAVA_RUNTIME))
            attribute(Bundling.BUNDLING_ATTRIBUTE, objects.named(Bundling, Bundling.EXTERNAL))
            // not sure why the below is needed, but when building with Java 8, using VERSION_1_5 gives this error:
            //
            // Could not determine the dependencies of task ':picocli-codegen:compileTestJava'.
            //> Could not resolve all task dependencies for configuration ':picocli-codegen:testCompileClasspath'.
            //   > Could not resolve project :.
            //     Required by:
            //         project :picocli-codegen
            //      > The consumer was configured to find an API of a library compatible with Java 6,
            //        preferably in the form of class files, preferably optimized for standard JVMs, and its dependencies declared externally.
            //        However we cannot choose between the following variants of project ::
            //          - apiElements
            //          - runtimeElements
            //        All of them match the consumer attributes:
            //          - Variant 'apiElements' capability info.picocli:picocli:4.7.0-SNAPSHOT declares a runtime of a library compatible with Java 5, packaged as a jar, and its dependencies declared externally:
            //              - Unmatched attribute:
            //                  - Doesn't say anything about its target Java environment (preferred optimized for standard JVMs)
            //          - Variant 'runtimeElements' capability info.picocli:picocli:4.7.0-SNAPSHOT declares a runtime of a library compatible with Java 5, packaged as a jar, and its dependencies declared externally:
            //              - Unmatched attribute:
            //                  - Doesn't say anything about its target Java environment (preferred optimized for standard JVMs)
            attribute(TargetJvmVersion.TARGET_JVM_VERSION_ATTRIBUTE, new Integer(JavaVersion.VERSION_1_6.majorVersion))

        }
    }
}
// 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"
