overdrive_that_matters/build.gradle.kts
2023-01-19 14:39:17 +07:00

458 lines
13 KiB
Plaintext

import groovy.lang.Closure
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
import java.util.Date
import java.text.SimpleDateFormat
import it.unimi.dsi.fastutil.io.FastByteArrayOutputStream
import java.util.UUID
val mod_version: String by project
val mc_version: String by project
val forge_version: String by project
val mod_id: String by project
val handle_deps: String by project
val use_commit_hash_in_version: String by project
val handleDeps = handle_deps == "true"
plugins {
java
kotlin
`maven-publish`
id("net.minecraftforge.gradle")
id("org.spongepowered.mixin")
}
configurations {
create("library") // non-mod libraries
create("klibrary") // kotlin libs
get("implementation").extendsFrom(get("library"), get("klibrary"))
}
data class GitInfo(val version: String, val count: String, val tag: String) {
// val tagIsVersion: Boolean get() = tag != "" && tag.matches(Regex("v[0-9]+\\.[0-9]\\.[0-9]"))
val publishVersion: String get() {
if (tag != "")
return mod_version
else
return "$mod_version-SNAPSHOT"
}
val jarName: String get() {
if (tag != "")
return "$mod_version-$version"
// if (count != "")
// return "$mod_version-SNAPSHOT-${version}_$count"
if (version != "") {
return "$mod_version-SNAPSHOT-$version"
} else {
return "$mod_version-SNAPSHOT"
}
}
val modVersion: String get() {
if (tag != "")
return mod_version
if (version != "") {
return "$mod_version-SNAPSHOT-$version"
} else {
return "$mod_version-SNAPSHOT"
}
}
}
val gitVersion = getCommitVersion() ?: GitInfo("", "", "")
version = gitVersion.modVersion
group = "ru.dbotthepony"
fun getCommitVersion(): GitInfo? {
try {
val versionStream = FastByteArrayOutputStream()
val tagStream = FastByteArrayOutputStream()
val countStream = FastByteArrayOutputStream()
val gotVersion = exec {
commandLine("git", "rev-parse", "--short", "HEAD")
workingDir(".")
standardOutput = versionStream
}.exitValue == 0
val gotCount = exec {
commandLine("git", "rev-list", "--count", "HEAD")
workingDir(".")
standardOutput = countStream
}.exitValue == 0
val gotTag = exec {
commandLine("git", "tag", "--points-at", "HEAD")
workingDir(".")
standardOutput = tagStream
}.exitValue == 0
if (!gotVersion || !gotCount || !gotTag) {
return null
}
val version = versionStream.array.copyOfRange(0, versionStream.length).toString(Charsets.UTF_8).trim()
val tag = tagStream.array.copyOfRange(0, tagStream.length).toString(Charsets.UTF_8).trim()
val count = countStream.array.copyOfRange(0, countStream.length).toString(Charsets.UTF_8).trim()
return GitInfo(version, count, tag)
} catch(err: Throwable) {
println("Error getting git version")
println(err)
}
return null
}
java.toolchain.languageVersion.set(JavaLanguageVersion.of(17))
println("Targeting Java ${java.toolchain.languageVersion.get()}")
tasks.withType(KotlinCompile::class.java) {
kotlinOptions {
freeCompilerArgs = listOf("-Xjvm-default=all")
jvmTarget = java.toolchain.languageVersion.get().toString()
}
}
tasks.withType(JavaCompile::class.java) {
options.compilerArgs.add("-Xlint:all")
}
sourceSets {
create("data") {
compileClasspath += sourceSets["main"].output
runtimeClasspath += sourceSets["main"].output
}
this["main"].resources {
srcDir("src/data/resources")
exclude("**/.cache/**")
}
}
tasks.test {
useJUnitPlatform()
}
dependencies {
val jupiter_version: String by project
val kotlin_version: String by project
val kotlin_for_forge_version: String by project
val kotlin_coroutines_version: String by project
val kotlin_serialization_version: String by project
val mixin_version: String by project
minecraft("net.minecraftforge:forge:$mc_version-$forge_version")
testImplementation("org.junit.jupiter:junit-jupiter:${jupiter_version}")
implementation("thedarkcolour:kotlinforforge:$kotlin_for_forge_version")
fun library(notation: Any) { this.add("library", notation) }
fun klibrary(notation: Any) { this.add("klibrary", notation) }
val excludeKGroup = closureOf<Any> {
(this as ExternalModuleDependency).exclude(group = "org.jetbrains", module = "annotations")
} as Closure<Any>
// Add everything to the classpath correctly
klibrary(create("org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version", excludeKGroup))
klibrary(create("org.jetbrains.kotlin:kotlin-reflect:$kotlin_version", excludeKGroup))
klibrary(create("org.jetbrains.kotlinx:kotlinx-coroutines-core:$kotlin_coroutines_version", excludeKGroup))
klibrary(create("org.jetbrains.kotlinx:kotlinx-coroutines-jdk8:$kotlin_coroutines_version", excludeKGroup))
klibrary(create("org.jetbrains.kotlinx:kotlinx-serialization-json:$kotlin_serialization_version", excludeKGroup))
// Real mod deobf dependency examples - these get remapped to your current mappings
// compileOnly fg.deobf("mezz.jei:jei-${mc_version}:${jei_version}:api") // Adds JEI API as a compile dependency
// runtimeOnly fg.deobf("mezz.jei:jei-${mc_version}:${jei_version}") // Adds the full JEI mod as a runtime dependency
// implementation fg.deobf("com.tterrag.registrate:Registrate:MC${mc_version}-${registrate_version}") // Adds registrate as a dependency
// Examples using mod jars from ./libs
compileOnly("yalter.mousetweaks:MouseTweaks:2.23:api")
annotationProcessor("org.spongepowered:mixin:${mixin_version}:processor")
// compile against the JEI API but do not include it at runtime
//compileOnly fg.deobf("mezz.jei:jei-${mc_version}:${jei_version}:api")
// at runtime, use the full JEI jar
//runtimeOnly fg.deobf("mezz.jei:jei-${mc_version}:${jei_version}")
if (handleDeps) {
val jei_version: String by project
val mekanism_version: String by project
val curios_version: String by project
val cosmetic_armor_reworked_version: String by project
val jade_id: String by project
val configured_id: String by project
val worldedit_fileid: String by project
val more_overlays_version: String by project
val deps_mc_version: String by project
val jei_mc_version: String by project
compileOnly(fg.deobf("top.theillusivec4.curios:curios-forge:${deps_mc_version}-${curios_version}"))
compileOnly(fg.deobf("lain.mods.cos:CosmeticArmorReworked:${deps_mc_version}-${cosmetic_armor_reworked_version}"))
compileOnly(fg.deobf("mezz.jei:jei-${jei_mc_version}-common-api:${jei_version}"))
compileOnly(fg.deobf("mezz.jei:jei-${jei_mc_version}-forge-api:${jei_version}"))
runtimeOnly(fg.deobf("mezz.jei:jei-${jei_mc_version}-forge:${jei_version}"))
// runtimeOnly(fg.deobf("curse.maven:jade-324717:${jade_id}"))
// runtimeOnly(fg.deobf("curse.maven:configured-457570:${configured_id}"))
// runtimeOnly(fg.deobf("curse.maven:worldedit-225608:${worldedit_fileid}"))
// runtimeOnly(fg.deobf("at.ridgo8.moreoverlays:MoreOverlays-updated:${more_overlays_version}"))
compileOnly(fg.deobf("mekanism:Mekanism:${deps_mc_version}-${mekanism_version}:all"))
}
}
configurations {
getByName("dataImplementation").extendsFrom(getByName("implementation"))
getByName("library").resolutionStrategy.cacheChangingModulesFor(10, "minutes")
}
minecraft {
mappings("official", mc_version)
accessTransformer(file("src/main/resources/META-INF/accesstransformer.cfg"))
runs {
create("client") {
mods {
create(mod_id) {
source(sourceSets["main"])
}
}
val usernameStream = FastByteArrayOutputStream()
val gotUsername = exec {
commandLine("git", "config", "--global", "--get", "user.name")
workingDir(".")
standardOutput = usernameStream
}.exitValue == 0
if (gotUsername) {
val originalUsername = usernameStream.array.copyOfRange(0, usernameStream.length).toString(Charsets.UTF_8).trim()
if (originalUsername.isNotEmpty()) {
var username = originalUsername
var speculatedUUID = UUID.nameUUIDFromBytes("OfflinePlayer:$username".toByteArray(Charsets.UTF_8))
var counter = 1
while (speculatedUUID.hashCode() and 1 == 1) {
username = originalUsername + "_".repeat(counter)
speculatedUUID = UUID.nameUUIDFromBytes("OfflinePlayer:$username".toByteArray(Charsets.UTF_8))
counter++
}
args("--username", username)
} else {
args("--username", "Dev_${System.getProperty("user.name")}")
}
} else {
args("--username", "Dev_${System.getProperty("user.name")}")
}
args("-mixin.config=$mod_id.mixins.json")
}
create("server") {
mods {
create(mod_id) {
source(sourceSets["main"])
}
}
args("nogui", "-mixin.config=$mod_id.mixins.json")
}
create("data") {
args("--mod", mod_id, "--all", "--output", file("src/data/resources/"), "--existing", file("src/main/resources/"))
mods {
create(mod_id) {
sources(sourceSets["main"], sourceSets["data"])
}
}
forceExit(false)
}
}
}
mixin {
add(sourceSets.main.get(), "$mod_id.refmap.json")
config("$mod_id.mixins.json")
}
minecraft.runs.all {
workingDirectory = project.file("run").absolutePath
// "SCAN": For mods scan.
// "REGISTRIES": For firing of registry events.
// "REGISTRYDUMP": For getting the contents of all registries.
property("forge.logging.markers", "REGISTRIES")
// Log4j console level
property("forge.logging.console.level", "debug")
lazyToken("minecraft_classpath") {
configurations["library"]
.copyRecursive()
.resolve()
.map { it.absolutePath }
.toMutableList()
.also { it.addAll(configurations["klibrary"].copyRecursive().resolve().map { it.absolutePath }) }
.joinToString(File.pathSeparator)
}
}
repositories {
// If you have mod jar dependencies in ./libs, you can declare them as a repository like so:
flatDir {
dir("libs")
}
maven {
url = uri("https://maven.dbotthepony.ru")
content {
includeGroup("top.theillusivec4.curios")
includeGroup("yalter.mousetweaks")
includeGroup("mekanism")
includeGroup("lain.mods.cos")
includeGroup("at.ridgo8.moreoverlays")
}
}
maven {
name = "Kotlin for Forge"
url = uri("https://thedarkcolour.github.io/KotlinForForge/")
content {
includeGroup("thedarkcolour")
includeGroup("org.jetbrains.kotlin")
includeGroup("org.jetbrains.kotlinx")
}
}
maven {
url = uri("https://www.cursemaven.com")
content {
includeGroup("curse.maven")
}
}
maven {
name = "Jared's Maven"
url = uri("https://maven.blamejared.com/")
content {
includeGroup("mezz.jei")
}
}
// mavenCentral()
}
fun org.gradle.jvm.tasks.Jar.attachManifest() {
manifest {
attributes(mapOf(
"Specification-Title" to project.name,
"Specification-Vendor" to "DBotThePony",
"Specification-Version" to "1", // We are version 1 of ourselves
"Implementation-Title" to project.name,
"Implementation-Version" to gitVersion.modVersion,
"Implementation-Vendor" to "DBotThePony",
"Implementation-Timestamp" to SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ").format(Date())
))
}
}
// Example configuration to allow publishing using the maven-publish plugin
// This is the preferred method to reobfuscate your jar file
tasks.jar.configure {
from(configurations["library"].map { if (it.isDirectory) it else zipTree(it) })
finalizedBy("reobfJar")
attachManifest()
archiveVersion.set(gitVersion.jarName)
}
tasks {
create("sourceJar", org.gradle.jvm.tasks.Jar::class.java) {
archiveClassifier.set("sources")
from(sourceSets.main.get().allSource)
}
create("deobfJar", org.gradle.jvm.tasks.Jar::class.java) {
archiveClassifier.set("deobf")
from(configurations["library"].map { if (it.isDirectory) it else zipTree(it) })
from(sourceSets.main.get().output)
attachManifest()
}
}
if (project.hasProperty("mavenUser") && project.hasProperty("mavenPassword") && project.hasProperty("mavenUrl")) {
val mavenUser: String by project
val mavenPassword: String by project
val mavenUrl: String by project
publishing {
publications {
create<MavenPublication>("mavenJava") {
// from(components["java"])
artifact(tasks["jar"])
artifact(tasks["sourceJar"])
artifact(tasks["deobfJar"])
version = gitVersion.publishVersion
pom {
scm {
url.set("https://gitlab.com/DBotThePony/overdrive-that-matters.git")
}
issueManagement {
system.set("gitlab")
url.set("https://gitlab.com/DBotThePony/overdrive-that-matters/issues")
}
}
}
}
repositories {
maven {
url = uri(mavenUrl)
credentials {
username = mavenUser
password = mavenPassword
}
}
}
}
}
// However if you are in a multi-project build, dev time needs unobfed jar files, so you can delay the obfuscation until publishing by doing
// publish.dependsOn("reobfJar")
/*
publishing {
publications {
mavenJava(MavenPublication) {
artifact jar
}
}
repositories {
maven {
url "file://${project.projectDir}/mcmodsrepo"
}
}
}
*/