plugins {
    id "io.github.goooler.shadow" version "8.1.8"
    id "com.jfrog.artifactory" version "4.21.0"
}

println """
*******************************************
 You are building CommandBook! 
 
 If you encounter trouble:
 1) Read CONTRIBUTING.md if you haven't yet
 2) Try running 'build' in a separate Gradle run
 3) Use gradlew and not gradle
 4) If you still need help, ask on Discord! https://discord.gg/enginehub
 
  Output files will be in [subproject]/build/libs
*******************************************
"""

if (!project.hasProperty("artifactory_contextUrl")) ext.artifactory_contextUrl = ""
if (!project.hasProperty("artifactory_user")) ext.artifactory_user = ""
if (!project.hasProperty("artifactory_password")) ext.artifactory_password = ""

if (!project.hasProperty("gitCommitHash")) {
    try {
        def repo = org.ajoberstar.grgit.Grgit.open(project.file('.'))
        ext.gitCommitHash = repo.head().abbreviatedId
    } catch (Exception e) {
        ext.gitCommitHash = "no_git_id"
    }
}

apply plugin: 'java'
apply plugin: 'maven-publish'

group = pluginGroup
version = pluginVersion
ext.internalVersion = pluginVersion + ";" + gitCommitHash

java {
    toolchain {
        languageVersion = JavaLanguageVersion.of(21)
    }
}

repositories {
    mavenLocal()
    mavenCentral()
    maven {
        name = 'EngineHub Repo'
        url = 'https://maven.enginehub.org/repo/'
    }
    maven {
        name = 'Destroystokyo Repo'
        url = 'https://repo.destroystokyo.com/repository/maven-public/'
    }
    maven {
        name 'Packet Loss Gaming Repository'
        url 'https://mvn.packetloss.gg/'
    }
}

dependencies {
    compileOnly 'io.papermc.paper:paper-api:1.21-R0.1-SNAPSHOT'
    compileOnly 'com.sk89q.worldedit:worldedit-core:7.3.4'
    compileOnly 'com.sk89q.worldedit:worldedit-bukkit:7.3.4'
    compileOnly 'org.enginehub.jinglenote:jinglenote-core:1.0.0-SNAPSHOT'
    implementation('org.enginehub.jinglenote:jinglenote-bukkit:1.0.0-SNAPSHOT') {
        exclude group: 'org.spigotmc', module: 'spigot-api'
    }
    implementation 'com.zachsthings.libcomponents:libcomponents-core:1.3.1'
    implementation 'com.zachsthings.libcomponents:libcomponents-bukkit:1.3.1'
    implementation 'net.sf.opencsv:opencsv:2.0'

    compileOnly 'com.sk89q.worldedit.worldedit-libs:ap:7.3.4'
    annotationProcessor 'com.sk89q.worldedit.worldedit-libs:ap:7.3.4'
    annotationProcessor "com.google.guava:guava:21.0"

    testImplementation group: 'junit', name: 'junit', version: '4.12'
}

tasks.withType(JavaCompile).configureEach {
    it.options.compilerArgs.add("-Aarg.name.key.prefix=")
}

task sourcesJar(type: Jar, dependsOn: classes) {
    archiveClassifier = 'sources'
    from sourceSets.main.allSource
}

task javadocJar(type: Jar, dependsOn: javadoc) {
    archiveClassifier = 'javadoc'
    from javadoc.destinationDir
}

publishing {
    publications {
        maven(MavenPublication) {

            from components.java

            artifact sourcesJar
            artifact javadocJar
        }
    }
}

artifacts {
    archives shadowJar
    archives sourcesJar
    archives javadocJar
}

build.dependsOn(sourcesJar)
build.dependsOn(javadocJar)

jar {
    manifest {
        attributes("Class-Path": "CommandBook/WorldEdit.jar lib/WorldEdit.jar ../lib/WorldEdit.jar ../WorldEdit.jar",
                   "CommandBook-Version": pluginVersion)
    }
}

shadowJar {
    archiveClassifier.set('dist')

    dependencies {
        include(dependency('com.zachsthings.libcomponents:libcomponents-core'))
        include(dependency('com.zachsthings.libcomponents:libcomponents-bukkit'))
        relocate ("au.com.bytecode.opencsv", "com.sk89q.commandbook.shaded.opencsv") {
            include(dependency('net.sf.opencsv:opencsv'))
        }
        relocate ("org.enginehub.jinglenote", "com.sk89q.commandbook.shaded.jinglenote") {
            include(dependency("org.enginehub.jinglenote:jinglenote-core"))
            include(dependency("org.enginehub.jinglenote:jinglenote-bukkit"))
        }
    }

    exclude 'GradleStart**'
    exclude '.cache'
    exclude 'LICENSE*'
}

artifactory {
    contextUrl = "${artifactory_contextUrl}"
    publish {
        repository {
            repoKey = project.version.contains("SNAPSHOT") ? 'libs-snapshot-local' : 'libs-release-local'
            username = "${artifactory_user}"
            password = "${artifactory_password}"
            maven = true
        }
    }

    resolve {
        repository {
            repoKey = 'repo'
            username = "${artifactory_user}"
            password = "${artifactory_password}"
            maven = true
        }
    }
}

artifactoryPublish {
    publishConfigs("archives")
}

processResources {
    duplicatesStrategy DuplicatesStrategy.INCLUDE

    from (sourceSets.main.resources.srcDirs) {
        expand 'internalVersion': project.internalVersion
        include 'plugin.yml'
    }
}
