Skip to content

Commit

Permalink
move automatic dependency versions to json file
Browse files Browse the repository at this point in the history
  • Loading branch information
deirn committed Apr 27, 2024
1 parent 392719d commit 321d20e
Show file tree
Hide file tree
Showing 5 changed files with 164 additions and 79 deletions.
2 changes: 2 additions & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ plugins {
group = "lol.bai.megane"
version = System.getenv("MOD_VERSION") ?: "999999-local"

rootProject.initializeDependencies()

allprojects {
apply(plugin = "java")

Expand Down
126 changes: 80 additions & 46 deletions buildSrc/src/main/kotlin/Dependencies.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
@file:Suppress("ClassName", "ConstPropertyName")

import com.fasterxml.jackson.databind.JsonNode
import com.fasterxml.jackson.databind.json.JsonMapper
import org.gradle.api.Project
import org.gradle.api.artifacts.ExternalModuleDependency
import org.gradle.kotlin.dsl.exclude
import kotlin.reflect.KProperty

object versions {
const val minecraft = "1.20.1"
Expand All @@ -11,70 +15,82 @@ object deps {
const val minecraft = "com.mojang:minecraft:${versions.minecraft}"
const val mixinAp = "net.fabricmc:sponge-mixin:0.12.5+mixin.0.8.5"

object fabric {
const val loader = "net.fabricmc:fabric-loader:0.15.6"
object fabric : DependencyPath {
override val prefix = "fabric"

object wthit {
const val api = "mcp.mobius.waila:wthit-api:fabric-8.8.0"
const val runtime = "mcp.mobius.waila:wthit:fabric-8.8.0"
const val loader = "net.fabricmc:fabric-loader:0.15.10"

object wthit : DependencyPath {
override val prefix = "fabric.wthit"

val api by json()
val runtime by json()
}

const val badpackets = "lol.bai:badpackets:fabric-0.4.3"

const val ae2 = "appeng:appliedenergistics2-fabric:15.0.23"
const val alloyForge = "maven.modrinth:jhl28YkY:j3GoCoCc"
const val architectury = "dev.architectury:architectury-fabric:9.2.14"
const val create = "com.simibubi.create:create-fabric-1.20.1:0.5.1-f-build.1335+mc1.20.1"
const val clothConfig = "me.shedaniel.cloth:cloth-config-fabric:11.1.118"
const val fabricApi = "net.fabricmc.fabric-api:fabric-api:0.92.0+1.20.1"
const val flk = "net.fabricmc:fabric-language-kotlin:1.10.19+kotlin.1.9.23"
const val kibe = "maven.modrinth:OvlwmUdC:fhSgEP7Z"
const val modernDynamics = "maven.modrinth:fMpvLrnF:dLMmaJID"
const val modmenu = "com.terraformersmc:modmenu:7.2.2"
const val noIndium = "me.luligabi:NoIndium:1.1.0+1.20"
const val owo = "io.wispforest:owo-lib:0.11.2+1.20"
const val patchouli = "vazkii.patchouli:Patchouli:1.20.1-84-FABRIC"
const val powah = "maven.modrinth:KZO4S4DO:zrPCk4ZJ"

const val dml = "curse.maven:cursemod-398614:5032813"
const val indrev = "curse.maven:cursemod-391708:4742613"
const val luggage = "curse.maven:cursemod-594709:4284469"
const val pal = "io.github.ladysnake:PlayerAbilityLib:1.8.0"
const val rebornCore = "RebornCore:RebornCore-1.20:5.8.7"
const val techReborn = "TechReborn:TechReborn-1.20:5.8.7"
const val wirelessNet = "curse.maven:cursemod-461316:4610810"
val badpackets by json()

val ae2 by json()
val alloyForge by json()
val architectury by json()
val create by json()
val clothConfig by json()
val fabricApi by json()
val flk by json()
val kibe by json()
val modernDynamics by json()
val modmenu by json()
val noIndium by json()
val owo by json()
val patchouli by json()
val powah by json()

val dml by json()
val indrev by json()
val luggage by json()
val pal by json()
val rebornCore by json()
val techReborn by json()
val wirelessNet by json()

const val trEnergy = "teamreborn:energy:2.2.0"
const val libgui = "io.github.cottonmc:LibGui:8.1.1+1.20.1"
const val magna = "dev.draylar:magna:1.10.0+1.20.1"
const val stepAttr = "com.github.emilyploszaj:step-height-entity-attribute:v1.2.0"
}

object forge {
object forge : DependencyPath {
override val prefix = "forge"

const val forge = "net.neoforged:forge:${versions.minecraft}-47.1.97"

object wthit {
const val api = "mcp.mobius.waila:wthit-api:forge-8.8.0"
const val runtime = "mcp.mobius.waila:wthit:forge-8.8.0"
object wthit : DependencyPath {
override val prefix = "forge.wthit"

val api by json()
val runtime by json()
}

const val badpackets = "lol.bai:badpackets:forge-0.4.3"
val badpackets by json()

const val ae2 = "appeng:appliedenergistics2-forge:15.0.23"
const val create = "maven.modrinth:LNytGWDc:HNYrbfZZ"
const val ie = "maven.modrinth:tIm2nV03:uqGFpLXw"
const val rs = "maven.modrinth:KDvYkUg3:ZITLFjjf"
const val jei = "maven.modrinth:u6dRKJwZ:PeYsGsQy"
val ae2 by json()
val create by json()
val ie by json()
val rs by json()
val jei by json()

object mekanism {
const val core = "mekanism:Mekanism:1.20.1-10.4.6.20"
const val generators = "mekanism:Mekanism:1.20.1-10.4.6.20:generators"
object mekanism : DependencyPath {
override val prefix = "forge.mekanism"

val core by json()
val generators by json()
}

object thermal {
const val cofhCore = "maven.modrinth:OWSRM4vD:C1iOYlLu"
const val foundation = "maven.modrinth:Xvg6q5Wp:dUiPDb6I"
const val expansion = "maven.modrinth:hmD6rrUJ:kg4h60cQ"
object thermal : DependencyPath {
override val prefix = "forge.thermal"

val cofhCore by json()
val foundation by json()
val expansion by json()
}
}
}
Expand All @@ -83,3 +99,21 @@ fun ExternalModuleDependency.exclude(dep: String) {
val (group, module) = dep.split(':', limit = 2)
exclude(group, module)
}

fun Project.initializeDependencies() {
dependenciesJson = JsonMapper().readTree(file("dependencies.json"))
}

private lateinit var dependenciesJson: JsonNode

interface DependencyPath {
val prefix: String

fun json() = DependencyDelegate()
}

class DependencyDelegate {
operator fun getValue(self: DependencyPath, property: KProperty<*>): String {
return dependenciesJson["${self.prefix}.${property.name}"].asText()
}
}
71 changes: 38 additions & 33 deletions buildSrc/src/main/kotlin/FetchLatestVersionsTask.kt
Original file line number Diff line number Diff line change
@@ -1,60 +1,66 @@
import com.fasterxml.jackson.databind.json.JsonMapper
import deps.fabric
import deps.forge
import org.gradle.api.DefaultTask
import org.gradle.api.GradleException
import org.gradle.api.provider.Property
import org.gradle.api.tasks.InputFile
import org.gradle.api.tasks.TaskAction
import version.CurseForgeVersionFetcher
import version.ModrinthVersionFetcher
import version.VersionFetcher
import java.awt.Toolkit
import java.awt.datatransfer.StringSelection
import java.io.File
import java.net.http.HttpClient
import java.util.*
import kotlin.reflect.KProperty

@Suppress("LeakingThis")
abstract class FetchLatestVersionsTask : DefaultTask() {
@get:InputFile
abstract val output: Property<File>

init {
group = "megane"

output.convention(project.file("dependencies.json"))
}

@TaskAction
fun run() {
val output = StringBuilder()
val mapper = JsonMapper()
val output = mapper.createObjectNode()
val http = HttpClient.newHttpClient()

fun out(line: String = "") {
output.appendLine(line)
println(line)
}

class VersionFetcherDsl<T>(
val versionFetcher: VersionFetcher<T>,
val loader: String
val loader: String,
val path: DependencyPath
) {
fun fetch(name: KProperty<*>, project: String, mc: String = versions.minecraft, parser: (T) -> String) {
fun fetch(property: KProperty<*>, project: String, mc: String = versions.minecraft, parser: (T) -> String) {
val key = "${path.prefix}.${property.name}"
val res = versionFetcher.getLatestVersionFor(http, project, mc, loader)

if (res != null) {
// language=kotlin
out("const val ${name.name} = \"${parser(res)}\"")
output.put(key, parser(res))
} else {
throw GradleException("Failed to get version for ${name}")
throw GradleException("Failed to get version for $key")
}
}
}

fun <T> fetcher(versionFetcher: VersionFetcher<T>, loader: String, action: VersionFetcherDsl<T>.() -> Unit) {
action(VersionFetcherDsl(versionFetcher, loader))
fun <T> fetcher(versionFetcher: VersionFetcher<T>, loader: String, path: DependencyPath, action: VersionFetcherDsl<T>.() -> Unit) {
action(VersionFetcherDsl(versionFetcher, loader, path))
}

out("\nobject fabric {")
fetcher(ModrinthVersionFetcher, "fabric") {
out("\nobject wthit {")
fetcher(ModrinthVersionFetcher, "fabric", fabric.wthit) {
fetch(fabric.wthit::api, mrIds.wthit) { "mcp.mobius.waila:wthit-api:${it.version_number}" }
fetch(fabric.wthit::runtime, mrIds.wthit) { "mcp.mobius.waila:wthit:${it.version_number}" }
out("}\n")
}

fetcher(ModrinthVersionFetcher, "fabric", fabric) {
fetch(fabric::badpackets, mrIds.badpackets) { "lol.bai:badpackets:${it.version_number}" }
out()

fetch(fabric::ae2, mrIds.ae2) { "appeng:appliedenergistics2-fabric:${it.version_number.removePrefix("fabric-")}" }
fetch(fabric::alloyForge, mrIds.alloyForge) { it.maven }
Expand All @@ -72,8 +78,8 @@ abstract class FetchLatestVersionsTask : DefaultTask() {
fetch(fabric::patchouli, mrIds.patchouli) { "vazkii.patchouli:Patchouli:${it.version_number.toUpperCase(Locale.ROOT)}" }
fetch(fabric::powah, mrIds.powah) { it.maven }
}
out()
fetcher(CurseForgeVersionFetcher, "fabric") {

fetcher(CurseForgeVersionFetcher, "fabric", fabric) {
fetch(fabric::dml, cfIds.dml) { it.maven }
fetch(fabric::indrev, cfIds.indrev) { it.maven }
// fetch(fabric::luggage, cfIds.luggage) { it.maven }
Expand All @@ -82,41 +88,40 @@ abstract class FetchLatestVersionsTask : DefaultTask() {
fetch(fabric::techReborn, cfIds.techReborn) { "TechReborn:TechReborn-1.20:${it.download.fileName.removePrefix("TechReborn-")}" }
fetch(fabric::wirelessNet, cfIds.wirelessNet) { it.maven }
}
out("}")

out("\nobject forge {")
fetcher(ModrinthVersionFetcher, "forge") {
out("\nobject wthit {")
fetcher(ModrinthVersionFetcher, "forge", forge.wthit) {
fetch(forge.wthit::api, mrIds.wthit) { "mcp.mobius.waila:wthit-api:${it.version_number}" }
fetch(forge.wthit::runtime, mrIds.wthit) { "mcp.mobius.waila:wthit:${it.version_number}" }
out("}\n")
}

fetcher(ModrinthVersionFetcher, "forge", forge) {
fetch(forge::badpackets, mrIds.badpackets) { "lol.bai:badpackets:${it.version_number}" }
out()

fetch(forge::ae2, mrIds.ae2) { "appeng:appliedenergistics2-forge:${it.version_number.removePrefix("forge-")}" }
fetch(forge::create, mrIds.createForge) { it.maven }
fetch(forge::rs, mrIds.rs) { it.maven }
fetch(forge::jei, mrIds.jei) { it.maven }
}

out("\nobject mekanism {")
fetcher(ModrinthVersionFetcher, "forge", forge.mekanism) {
fetch(forge.mekanism::core, mrIds.mekCore) { "mekanism:Mekanism:1.20.1-${it.version_number}" }
fetch(forge.mekanism::generators, mrIds.mekCore) { "mekanism:Mekanism:1.20.1-${it.version_number}:generators" }
out("}")
}

out("\nobject thermal {")
fetcher(ModrinthVersionFetcher, "forge", forge.thermal) {
fetch(forge.thermal::cofhCore, mrIds.cofhCore) { it.maven }
fetch(forge.thermal::foundation, mrIds.thermalFoundation) { it.maven }
fetch(forge.thermal::expansion, mrIds.thermalExpansion) { it.maven }
out("}")
}

fetcher(ModrinthVersionFetcher, "neoforge") {
fetcher(ModrinthVersionFetcher, "neoforge", forge) {
fetch(forge::ie, mrIds.ie) { it.maven }
}
out("}")

Toolkit.getDefaultToolkit().systemClipboard.setContents(StringSelection(output.toString()), null)
mapper
.writer()
.withDefaultPrettyPrinter()
.writeValue(this.output.get(), output)
}
}

38 changes: 38 additions & 0 deletions dependencies.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
{
"fabric.wthit.api" : "mcp.mobius.waila:wthit-api:fabric-8.10.0",
"fabric.wthit.runtime" : "mcp.mobius.waila:wthit:fabric-8.10.0",
"fabric.badpackets" : "lol.bai:badpackets:fabric-0.4.3",
"fabric.ae2" : "appeng:appliedenergistics2-fabric:15.1.0",
"fabric.alloyForge" : "maven.modrinth:jhl28YkY:j3GoCoCc",
"fabric.architectury" : "dev.architectury:architectury-fabric:9.2.14",
"fabric.create" : "com.simibubi.create:create-fabric-1.20.1:0.5.1-f-build.1417+mc1.20.1",
"fabric.clothConfig" : "me.shedaniel.cloth:cloth-config-fabric:11.1.118",
"fabric.fabricApi" : "net.fabricmc.fabric-api:fabric-api:0.92.1+1.20.1",
"fabric.flk" : "net.fabricmc:fabric-language-kotlin:1.10.19+kotlin.1.9.23",
"fabric.kibe" : "maven.modrinth:OvlwmUdC:fhSgEP7Z",
"fabric.modernDynamics" : "maven.modrinth:fMpvLrnF:dLMmaJID",
"fabric.modmenu" : "com.terraformersmc:modmenu:7.2.2",
"fabric.noIndium" : "me.luligabi:NoIndium:1.1.0+1.20",
"fabric.owo" : "io.wispforest:owo-lib:0.11.2+1.20",
"fabric.patchouli" : "vazkii.patchouli:Patchouli:1.20.1-84-FABRIC",
"fabric.powah" : "maven.modrinth:KZO4S4DO:zrPCk4ZJ",
"fabric.dml" : "curse.maven:cursemod-398614:5201656",
"fabric.indrev" : "curse.maven:cursemod-391708:5254377",
"fabric.pal" : "io.github.ladysnake:PlayerAbilityLib:1.8.0",
"fabric.rebornCore" : "RebornCore:RebornCore-1.20:5.8.7",
"fabric.techReborn" : "TechReborn:TechReborn-1.20:5.8.7",
"fabric.wirelessNet" : "curse.maven:cursemod-461316:4610810",
"forge.wthit.api" : "mcp.mobius.waila:wthit-api:forge-8.10.0",
"forge.wthit.runtime" : "mcp.mobius.waila:wthit:forge-8.10.0",
"forge.badpackets" : "lol.bai:badpackets:forge-0.4.3",
"forge.ae2" : "appeng:appliedenergistics2-forge:15.1.1",
"forge.create" : "maven.modrinth:LNytGWDc:HNYrbfZZ",
"forge.rs" : "maven.modrinth:KDvYkUg3:ZITLFjjf",
"forge.jei" : "maven.modrinth:u6dRKJwZ:PeYsGsQy",
"forge.mekanism.core" : "mekanism:Mekanism:1.20.1-10.4.6.20",
"forge.mekanism.generators" : "mekanism:Mekanism:1.20.1-10.4.6.20:generators",
"forge.thermal.cofhCore" : "maven.modrinth:OWSRM4vD:C1iOYlLu",
"forge.thermal.foundation" : "maven.modrinth:Xvg6q5Wp:dUiPDb6I",
"forge.thermal.expansion" : "maven.modrinth:hmD6rrUJ:kg4h60cQ",
"forge.ie" : "maven.modrinth:tIm2nV03:MAqXk6P8"
}
6 changes: 6 additions & 0 deletions modules/fabric/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,12 @@ publishMods {
allprojects {
apply(plugin = "fabric-loom")

configurations.all {
resolutionStrategy {
force(deps.fabric.loader)
}
}

dependencies {
minecraft(deps.minecraft)
mappings(loom.officialMojangMappings())
Expand Down

0 comments on commit 321d20e

Please sign in to comment.