import java.nio.charset.StandardCharsets

buildscript {
    repositories {
        mavenCentral()
        maven { url = 'https://oss.sonatype.org/content/repositories/snapshots/' }
        jcenter()
    }
    dependencies {
        classpath 'com.bmuschko:gradle-nexus-plugin:2.3.1'
    }
}

plugins {
    id 'checkstyle'
    id 'com.github.hierynomus.license' version '0.15.0'
    id 'java-library'
    id 'com.github.johnrengelman.shadow' version '6.1.0'
    id 'de.marcphilipp.nexus-publish' version '0.4.0'
    id 'net.ltgt.errorprone' version '1.3.0'
}

apply from: 'scripts/dependencies.gradle'

checkstyle {
    configFile file('config/checkstyle/checkstyle.xml')
}

gradle.taskGraph.whenReady {
    gradle.taskGraph.allTasks.each {
        if (it.project.name.contains('example')) {
            it.onlyIf {
                project.hasProperty('compile-examples')
            }
        }
    }
}

allprojects {
    apply plugin: 'idea'
    apply plugin: 'checkstyle'
    apply plugin: 'com.github.hierynomus.license'

    group = 'cloud.commandframework'
    version = '1.3.0'
    description = 'Command framework and dispatcher for the JVM'

    /* Disable checkstyle on tests */
    project.gradle.startParameter.excludedTaskNames.add('checkstyleTest')

    license {
        header rootProject.file('HEADER')
        mapping 'java', 'DOUBLESLASH_STYLE'
        mapping 'kt', 'DOUBLESLASH_STYLE'
        includes(["**/*.java", "**/*.kt"])
    }

    build.dependsOn(checkstyleMain)
}

subprojects {
    apply plugin: 'java'
    apply plugin: 'java-library'
    apply plugin: 'signing'
    apply plugin: 'de.marcphilipp.nexus-publish'
    apply plugin: 'net.ltgt.errorprone'

    test {
        useJUnitPlatform()
    }

    java {
        withSourcesJar()
        withJavadocJar()
    }

    tasks.withType(JavaCompile).configureEach {
        options.compilerArgs << "-Xlint:all" << "-Xlint:-processing" << "-Werror"
        options.errorprone {
            /* These are just annoying */
            disable(
                    "JdkObsolete",
                    "FutureReturnValueIgnored",
                    "ImmutableEnumChecker",
                    "StringSplitter",
                    "EqualsGetClass",
                    "CatchAndPrintStackTrace"
            )
        }
    }

    repositories {
        mavenLocal()
        mavenCentral()
        jcenter()
        maven {
            /* Sonatype Snapshots */
            url = 'https://oss.sonatype.org/content/repositories/snapshots'
        }
        maven {
            /* ViaVersion, used for adventure */
            url = 'https://repo.viaversion.com/'
        }
        maven {
            /* Velocity, used for cloud-velocity */
            url = 'https://repo.velocitypowered.com/snapshots/'
        }
        maven {
            /* The Minecraft repository, used for cloud-brigadier */
            url = 'https://libraries.minecraft.net/'
        }
        maven {
            /* The current Sponge repository */
            url = 'https://repo-new.spongepowered.org/repository/maven-public/'
        }
        maven {
            /* The Spigot repository, used for cloud-bukkit */
            url = 'https://hub.spigotmc.org/nexus/content/repositories/snapshots/'
        }
        maven {
            /* The paper repository, used for cloud-paper */
            url = 'https://papermc.io/repo/repository/maven-public/'
        }
        maven {
            /* The NukkitX repository, used for cloud-cloudburst */
            url = 'https://repo.nukkitx.com/maven-snapshots'
        }
        maven {
            /* JitPack, used for random dependencies */
            url = 'https://jitpack.io'
        }
    }

    dependencies {
        compileOnly "org.checkerframework:checker-qual:${vers['checker-qual']}"
        api "io.leangen.geantyref:geantyref:${vers['geantyref']}"
        testImplementation "org.junit.jupiter:junit-jupiter-engine:${vers['jupiter-engine']}"
        errorprone "com.google.errorprone:error_prone_core:${vers['errorprone']}"
        errorproneJavac "com.google.errorprone:javac:9+181-r4173-1"
    }

    nexusPublishing {
        repositories {
            sonatype()
        }
    }

    sourceCompatibility = '1.8'

    [JavaCompile, Javadoc].forEach { taskType ->
        tasks.withType(taskType) {
            options.encoding(StandardCharsets.UTF_8.name())
        }
    }

    publishing {
        publications {
            mavenJava(MavenPublication) {
                from components.java

                pom {
                    name = project.name
                    url = 'https://github.com/Incendo/cloud'
                    description = project.description

                    developers {
                        developer {
                            id = 'Sauilitired'
                            name = 'Alexander Söderberg'
                            url = 'https://alexander-soderberg.com'
                            email = 'contact@alexander-soderberg.com'
                        }
                    }

                    issueManagement {
                        system = 'GitHub Issues'
                        url = 'https://github.com/Incendo/cloud/issues'
                    }

                    licenses {
                        license {
                            name = 'MIT License'
                            url = 'https://opensource.org/licenses/MIT'
                        }
                    }

                    scm {
                        connection = 'scm:git@github.com:Incendo/cloud.git'
                        developerConnection = 'scm:git@github.com:Incendo/cloud.git'
                        url = 'https://github.com/Incendo/cloud/'
                    }
                }
            }
        }
    }

    signing {
        required {
            project.hasProperty('signing.keyId') && (gradle.taskGraph.hasTask(':publish')
                    || gradle.taskGraph.hasTask(':publishToSonatype')
                    || gradle.taskGraph.hasTask(':publishToMavenLocal'))
        }
        sign publishing.publications.mavenJava
    }

}
