Skip to content
This repository has been archived by the owner on Sep 2, 2024. It is now read-only.

Commit

Permalink
escape from gradle hell
Browse files Browse the repository at this point in the history
  • Loading branch information
homchom committed Aug 25, 2024
1 parent 3606e49 commit 6b35579
Show file tree
Hide file tree
Showing 7 changed files with 139 additions and 117 deletions.
8 changes: 0 additions & 8 deletions build.gradle.kts

This file was deleted.

19 changes: 18 additions & 1 deletion buildSrc/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,25 @@ plugins {

repositories {
mavenCentral()
maven {
name = "Fabric"
url = uri("https://maven.fabricmc.net/")
}
}

dependencies {
//implementation(libs.kotlin.gradlePlugin)
fun pluginImplementation(plugin: Provider<PluginDependency>) {
val dep = plugin.get()
implementation(group = dep.pluginId, name = "${dep.pluginId}.gradle.plugin") {
version {
strictly(dep.version.strictVersion)
require(dep.version.requiredVersion)
prefer(dep.version.preferredVersion)
for (version in dep.version.rejectedVersions) reject(version)
}
}
}

pluginImplementation(libs.plugins.kotlin.jvm)
pluginImplementation(libs.plugins.fabric.loom)
}
8 changes: 8 additions & 0 deletions buildSrc/src/main/kotlin/recode.fabric-conventions.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
plugins {
id("recode.kotlin-conventions")
id("fabric-loom")
}

java {
withSourcesJar()
}
29 changes: 28 additions & 1 deletion buildSrc/src/main/kotlin/recode.kotlin-conventions.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,3 +1,30 @@
import org.jetbrains.kotlin.gradle.dsl.JvmTarget
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile

plugins {
//kotlin("jvm")
kotlin("jvm")
}

object JvmVersions {
val java get() = JavaVersion.VERSION_17
val kotlin get() = JvmTarget.JVM_17
}

java {
sourceCompatibility = JvmVersions.java
targetCompatibility = JvmVersions.java
}

tasks.withType<JavaCompile>().configureEach {
options.release = JvmVersions.java.majorVersion.toInt()
options.encoding = "UTF-8"
}

tasks.withType<KotlinCompile>().configureEach {
compilerOptions {
jvmTarget = JvmVersions.kotlin
freeCompilerArgs = listOf(
"-Xjvm-default=all"
)
}
}
6 changes: 2 additions & 4 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,16 @@ fabric-api = { module = "net.fabricmc.fabric-api:fabric-api", version = "0.96.1+

adventure-platform-fabric = { module = "net.kyori:adventure-platform-fabric", version = "5.12.0" }
libgui = { module = "io.github.cottonmc:LibGui", version = "9.2.2+1.20.2" }
cloth-config = { module = "me.shedaniel.cloth:cloth-config-fabric", version = "13.0.121" }
clothConfig = { module = "me.shedaniel.cloth:cloth-config-fabric", version = "13.0.121" }

# optional; change versions every minecraft update
modmenu = { module = "com.terraformersmc:modmenu", version = "9.0.0" }
sodium = { module = "maven.modrinth:sodium", version = "mc1.20.4-0.5.8" }

# change versions as needed
fabric-loader = { module = "net.fabricmc:fabric-loader", version = "0.15.2" }

kotlin-gradlePlugin = { module = "org.jetbrains.kotlin:kotlin-gradle-plugin", version = "2.0.0" }
fabric-language-kotlin = { module = "net.fabricmc:fabric-language-kotlin", version = "1.11.0+kotlin.2.0.0" }

[plugins]
kotlin-jvm = { id = "org.jetbrains.kotlin.jvm", version = "2.0.0" }
fabric-loom = { id = "fabric-loom", version = "1.5-SNAPSHOT" }
fabric-loom = { id = "fabric-loom", version = "1.7-SNAPSHOT" }
171 changes: 72 additions & 99 deletions mod/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,12 +1,5 @@
import org.jetbrains.kotlin.gradle.dsl.JvmTarget
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile

plugins {
// unresolved issue: gson resolves incorrectly if this is uncommented and the line after is commented
//id("recode.kotlin-conventions")
kotlin("jvm") version "2.0.0"

alias(libs.plugins.fabric.loom)
id("recode.fabric-conventions")

id("com.modrinth.minotaur") version "2.+"
id("com.github.johnrengelman.shadow") version "8.1.1"
Expand All @@ -28,9 +21,9 @@ val flkVersion: String by project
val requiredDependencyMods = dependencyModsOfType("required")
val optionalDependencyMods = dependencyModsOfType("optional")

/*base {
archivesName.set(modName)
}*/
base {
archivesName = modName
}

repositories {
exclusiveContent {
Expand Down Expand Up @@ -66,123 +59,103 @@ val shade: Configuration by configurations.creating {
}

dependencies {
fun includeModImplementation(mod: Provider<MinimalExternalModuleDependency>) {
modImplementation(mod)
include(mod)
}

// minecraft
minecraft("com.mojang:minecraft:$minecraftVersion")
minecraft(libs.minecraft)
mappings(loom.officialMojangMappings())

// fabric
modImplementation("net.fabricmc.fabric-api:fabric-api:$fabricVersion")
modImplementation("net.fabricmc:fabric-loader:$loaderVersion")
modImplementation(libs.fabric.api)
modImplementation(libs.fabric.loader)
modImplementation(libs.fabric.language.kotlin)

// kotlin
modImplementation("net.fabricmc:fabric-language-kotlin:$flkVersion")
// other required mods
includeModImplementation(libs.adventure.platform.fabric)
includeModImplementation(libs.libgui)
includeModImplementation(libs.clothConfig)

// mod dependencies listed in gradle.properties
for (mod in requiredDependencyMods) {
include(modImplementation("${mod.artifact}:${mod.version}")!!)
}
for (mod in optionalDependencyMods) {
modCompileOnly("${mod.artifact}:${mod.version}")
}
// optional mods
modCompileOnly(libs.modmenu)
modCompileOnly(libs.sodium)

// Websocket TODO: clean this up
shade(implementation("org.java-websocket:Java-WebSocket:1.5.3")!!)
include(implementation("javax.websocket:javax.websocket-api:1.1")!!)
}

java {
sourceCompatibility = JavaVersion.VERSION_17
targetCompatibility = JavaVersion.VERSION_17

// Loom will automatically attach sourcesJar to a RemapSourcesJar task and to the "build" task
// if it is present. If you remove this line, sources will not be generated.
withSourcesJar()
}

tasks {
withType<JavaCompile> {
options.encoding = "UTF-8"
}

withType<KotlinCompile> {
compilerOptions {
jvmTarget.set(JvmTarget.JVM_17)
freeCompilerArgs.set(listOf(
"-Xjvm-default=all",
))
}
}

processResources {
// these properties can be used in fabric_mod_json_template.txt in Groovy template syntax
val exposedProperties = arrayOf(
"modName" to modName,
"version" to modVersion,
"minecraftVersion" to minecraftVersion,
"loaderVersion" to loaderVersion,
"fabricVersion" to fabricVersion,
"flkVersion" to flkVersion
tasks.processResources {
// these properties can be used in fabric_mod_json_template.txt in Groovy template syntax
val exposedProperties = arrayOf(
"modName" to modName,
"version" to modVersion,
"minecraftVersion" to minecraftVersion,
"loaderVersion" to loaderVersion,
"fabricVersion" to fabricVersion,
"flkVersion" to flkVersion
)

inputs.properties(*exposedProperties)
inputs.properties(project.properties.filterKeys { it.startsWith("required.") })

// evaluate fabric_mod_json_template.txt as a Groovy template
filesMatching("fabric_mod_json_template.txt") {
val metadataRegex = Regex("""\+.+$""")
expand(
*exposedProperties,
"metadataRegex" to metadataRegex.toPattern(),
"dependencyMods" to requiredDependencyMods.joinToString(", ") { mod ->
val version = mod.version.replace(metadataRegex, "")
"\"${mod.id}\": \"${mod.versionSpec}$version\""
}
)

inputs.properties(*exposedProperties)
inputs.properties(project.properties.filterKeys { it.startsWith("required.") })

// evaluate fabric_mod_json_template.txt as a Groovy template
filesMatching("fabric_mod_json_template.txt") {
val metadataRegex = Regex("""\+.+$""")
expand(
*exposedProperties,
"metadataRegex" to metadataRegex.toPattern(),
"dependencyMods" to requiredDependencyMods.joinToString(", ") { mod ->
val version = mod.version.replace(metadataRegex, "")
"\"${mod.id}\": \"${mod.versionSpec}$version\""
}
)
}
rename("fabric_mod_json_template.txt", "fabric.mod.json")
}
rename("fabric_mod_json_template.txt", "fabric.mod.json")
}

jar {
// disable jar (in favor of shadowJar)
enabled = false
}
tasks.jar {
// disable jar (in favor of shadowJar)
enabled = false
}

shadowJar {
configurations = listOf(shade)
from("LICENSE")
tasks.shadowJar {
configurations = listOf(shade)
from("LICENSE")

// output shaded jar in the correct destination to be used by remapJar
destinationDirectory.set(file("build/devlibs"))
archiveClassifier.set("dev")
// output shaded jar in the correct destination to be used by remapJar
destinationDirectory = file("build/devlibs")
archiveClassifier = "dev"

// relocate
isEnableRelocation = true
relocationPrefix = "$mavenGroup.$modName.shaded"
}
// relocate
isEnableRelocation = true
relocationPrefix = "$mavenGroup.$modName.shaded"
}

remapJar {
// use the shaded jar with remapJar
inputFile.value(shadowJar.get().archiveFile)
}
tasks.remapJar {
// use the shaded jar with remapJar
inputFile.value(tasks.shadowJar.get().archiveFile)
}

tasks.modrinth.get().dependsOn(tasks.modrinthSyncBody)

modrinth {
// DO NOT PUT THIS IN RECODE'S GRADLE.PROPERTIES. Your modrinth token should remain private to everyone.
token.set(findProperty("privateModrinthToken")?.toString() ?: "")
token = findProperty("privateModrinthToken")?.toString() ?: ""

projectId.set("recode")
versionNumber.set(modVersionWithMeta)
projectId = "recode"
versionNumber = modVersionWithMeta

val match = Regex("""-(?<phase>beta|alpha)\.""").find(modVersion)
if (match == null) {
versionName.set(modVersion)
versionType.set("release")
versionName = modVersion
versionType = "release"
} else {
val phase = match.groups["phase"]!!.value
versionName.set(modVersion.replaceRange(match.range, " $phase "))
versionType.set(phase)
versionName = modVersion.replaceRange(match.range, " $phase ")
versionType = phase
}

// remove "LATEST" classifiers when uploading to modrinth
Expand All @@ -199,8 +172,8 @@ modrinth {
}

// TODO: use something other than readText?
//syncBodyFrom.set(file("README.md").readText())
//changelog.set(file("CHANGELOG.md").readText())
syncBodyFrom = file("../README.md").readText()
changelog = file("../CHANGELOG.md").readText()
}

data class DependencyMod(
Expand Down
15 changes: 11 additions & 4 deletions settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,17 @@ include("mod")
pluginManagement {
repositories {
mavenCentral()
maven {
name = "Fabric"
url = uri("https://maven.fabricmc.net/")
}
gradlePluginPortal()
}
}

// https://github.com/FabricMC/fabric-loom/issues/1122 and https://github.com/gradle/gradle/issues/1370
buildscript {
repositories {
mavenCentral()
}

dependencies {
classpath("com.google.code.gson:gson:2.10.1")
}
}

0 comments on commit 6b35579

Please sign in to comment.