import org.jetbrains.kotlin.gradle.tasks.KotlinCompile

plugins {
    id "org.jetbrains.kotlin.jvm" version "1.6.10"
    id "java"
    id "idea"
    id "eclipse"
    id "maven-publish"
}

group "me.shedaniel"

def isSnapshot = System.getenv("PR_NUM") != null
def runNumber = (System.getenv("GITHUB_RUN_NUMBER") == null ? "9999" : System.getenv("GITHUB_RUN_NUMBER"))

if (!isSnapshot) {
    version = base_version + "." + runNumber
} else {
    version = base_version + "-PR." + System.getenv("PR_NUM") + "." + runNumber
}

def pluginId = "architectury-plugin"

logger.lifecycle(":building architectury plugin v${version}")

sourceCompatibility = targetCompatibility = 1.8

repositories {
    maven { url "https://maven.fabricmc.net/" }
    maven { url "https://maven.minecraftforge.net/" }
    maven { url "https://maven.shedaniel.me/" }
    gradlePluginPortal()
    mavenLocal()
}

apply plugin: 'java-gradle-plugin'

sourceSets {
    loom06 {
        java {
            compileClasspath += main.compileClasspath
            runtimeClasspath += main.runtimeClasspath
            compileClasspath += main.output
            runtimeClasspath += main.output
        }
    }
    loom09 {
        java {
            compileClasspath += main.compileClasspath
            runtimeClasspath += main.runtimeClasspath
            compileClasspath += main.output
            runtimeClasspath += main.output
        }
    }
    loom010Legacy {
        java {
            compileClasspath += main.compileClasspath
            runtimeClasspath += main.runtimeClasspath
            compileClasspath += main.output
            runtimeClasspath += main.output
        }
    }
    loom010 {
        java {
            compileClasspath += main.compileClasspath
            runtimeClasspath += main.runtimeClasspath
            compileClasspath += main.output
            runtimeClasspath += main.output
        }
    }
    loom011 {
        java {
            compileClasspath += main.compileClasspath
            runtimeClasspath += main.runtimeClasspath
            compileClasspath += main.output
            runtimeClasspath += main.output
        }
    }
    loom11 {
        java {
            compileClasspath += main.compileClasspath
            runtimeClasspath += main.runtimeClasspath
            compileClasspath += main.output
            runtimeClasspath += main.output
        }
    }
}

dependencies {
    implementation gradleApi()
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:1.3.72"
    implementation "org.jetbrains.kotlin:kotlin-reflect:1.3.72"
    implementation "gradle.plugin.org.jetbrains.gradle.plugin.idea-ext:gradle-idea-ext:0.10"
    implementation "dev.architectury:architectury-transformer:$transformer_version"
    loom06CompileOnly "me.shedaniel:forgified-fabric-loom:$loom_version_06"
    loom09CompileOnly "dev.architectury:architectury-loom:$loom_version_09"
    loom010LegacyCompileOnly "dev.architectury:architectury-loom:$loom_version_010Legacy"
    loom010CompileOnly "dev.architectury:architectury-loom:$loom_version_010"
    loom011CompileOnly "dev.architectury:architectury-loom:$loom_version_011"
    loom011CompileOnly "org.jetbrains.kotlin:kotlin-stdlib-jdk8:1.5.10"
    loom011CompileOnly "org.jetbrains.kotlin:kotlin-reflect:1.5.10"
    loom11CompileOnly "dev.architectury:architectury-loom:$loom_version_11"
    implementation "dev.architectury:tiny-remapper:1.1.0"
    implementation "com.google.code.gson:gson:2.8.5"
}

tasks.withType(KotlinCompile) {
    kotlinOptions {
        jvmTarget = "1.8"
        freeCompilerArgs = ['-Xjvm-default=compatibility']
    }
}

jar {
    manifest {
        attributes 'Implementation-Version': project.version
    }

    from sourceSets.loom06.output + sourceSets.loom09.output + sourceSets.loom010Legacy.output + sourceSets.loom010.output + sourceSets.loom011.output + sourceSets.loom11.output
}

gradlePlugin {
    plugins {
        architect {
            id = pluginId
            implementationClass = 'dev.architectury.plugin.ArchitecturyPlugin'
        }
    }
}

task sourcesJar(type: Jar, dependsOn: classes) {
    classifier("sources")
    from sourceSets.main.allSource
}

publishing {
    publications {
        plugin(MavenPublication) {
            groupId pluginId
            artifactId pluginId + '.gradle.plugin'
            
            from components.java
            artifact(sourcesJar)
        }

        pluginSnapshot(MavenPublication) {
            groupId pluginId
            artifactId pluginId + '.gradle.plugin'
            version base_version + '-SNAPSHOT'

            from components.java
            artifact(sourcesJar)
        }
    }

    repositories {
        if (System.getenv("MAVEN_PASS") != null) {
            maven {
                url = "https://deploy.shedaniel.me/"
                credentials {
                    username = "shedaniel"
                    password = System.getenv("MAVEN_PASS")
                }
            }
        }
    }
}