Skip to content

Commit

Permalink
separate resolver to its own module
Browse files Browse the repository at this point in the history
shadow only the loader, because of illegal access
  • Loading branch information
deirn committed Sep 21, 2024
1 parent 35c6866 commit e043611
Show file tree
Hide file tree
Showing 21 changed files with 303 additions and 249 deletions.
151 changes: 45 additions & 106 deletions build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,133 +1,72 @@
plugins {
`java-gradle-plugin`
`kotlin-dsl`
`maven-publish`

kotlin("jvm") version "1.9.0"
id("com.github.johnrengelman.shadow") version "8.1.1"
}

group = "lol.bai"
version = providers.environmentVariable("VERSION").getOrElse("9999-local")
allprojects {
apply(plugin = "maven-publish")
apply(plugin = "org.jetbrains.kotlin.jvm")
apply(plugin = "com.github.johnrengelman.shadow")

repositories {
mavenCentral()
maven("https://maven.fabricmc.net/")
maven("https://maven.minecraftforge.net/")
maven("https://libraries.minecraft.net/")
}

configurations {
create("shade")
}
version = providers.environmentVariable("VERSION").getOrElse("9999-local")
group = "lol.bai.explosion"

dependencies {
val shade by configurations
fun compileOnlyShade(artifact: String, action: ExternalModuleDependency.() -> Unit = {}) {
compileOnly(artifact, action)
shade(artifact, action)
repositories {
mavenCentral()
}

implementation("com.google.guava:guava:32.1.2-jre")
implementation("org.apache.maven:maven-artifact:3.8.1")
implementation("com.electronwill.night-config:core:3.7.3")
implementation("com.electronwill.night-config:toml:3.7.3")
implementation("org.apache.logging.log4j:log4j-api:2.22.1")
implementation("org.apache.logging.log4j:log4j-core:2.22.1")
implementation("org.slf4j:slf4j-api:2.0.16")
implementation("com.google.code.gson:gson:2.11.0")

compileOnlyShade("net.fabricmc:fabric-loader:0.15.10")
shade("net.fabricmc:access-widener:2.1.0")

compileOnlyShade("com.mojang:logging:1.2.7") { isTransitive = false }
compileOnlyShade("net.minecraftforge:fmlloader:1.20.4-49.0.49") { isTransitive = false }
compileOnlyShade("net.minecraftforge:modlauncher:10.2.1") { isTransitive = false }
compileOnlyShade("net.minecraftforge:securemodules:2.2.12") { isTransitive = false }
compileOnlyShade("net.minecraftforge:forgespi:7.1.5") { isTransitive = false }
compileOnlyShade("net.minecraftforge:JarJarFileSystems:0.3.26") { isTransitive = false }
compileOnlyShade("net.minecraftforge:JarJarMetadata:0.3.26") { isTransitive = false }
compileOnlyShade("net.minecraftforge:JarJarSelector:0.3.26") { isTransitive = false }
compileOnlyShade("net.minecraftforge:unsafe:0.9.2") { isTransitive = false }
compileOnlyShade("net.minecraftforge:mergetool-api:1.0")

testImplementation(kotlin("test"))
}
kotlin {
jvmToolchain(17)
}

tasks.test {
useJUnitPlatform()
}
configurations {
val shade by creating

kotlin {
jvmToolchain(17)
}

gradlePlugin {
plugins {
create("explosion") {
id = "lol.bai.explosion"
implementationClass = "lol.bai.explosion.ExplosionPlugin"
compileOnly {
extendsFrom(shade)
}
}
}

tasks.processResources {
val meta = "${project.group}:${project.name}:${project.version}"
inputs.property("meta", meta)
tasks.shadowJar {
configurations = listOf(project.configurations["shade"])
archiveClassifier.set("")

filesMatching("__meta.txt") {
expand("meta" to meta)
minimize()
mergeServiceFiles()
}
}

tasks.shadowJar {
configurations = listOf(project.configurations["shade"])
archiveClassifier.set("")
mergeServiceFiles()

relocate("net.fabricmc", "lol.bai.explosion.internal.lib.net.fabricmc")
relocate("net.minecraftforge", "lol.bai.explosion.internal.lib.net.minecraftforge")
relocate("cpw", "lol.bai.explosion.internal.lib.cpw")
relocate("com.mojang", "lol.bai.explosion.internal.lib.com.mojang")

exclude("ui/**")
exclude("assets/**")
exclude("fabric*.json")
exclude("log4j2*")
exclude("LICENSE_fabric-loader")
exclude("META-INF/jars/**")
exclude("META-INF/org/apache/logging/**")
}

tasks.build {
dependsOn("shadowJar")
}

publishing {
repositories {
maven {
name = "LocalMaven"
url = projectDir.resolve(".localMaven").toURI()
}
tasks.build {
dependsOn("shadowJar")
}

if (providers.environmentVariable("GITHUB_TOKEN").isPresent) {
publishing {
repositories {
maven {
url = uri("https://maven.pkg.github.com/badasintended/explosion")
name = "GitHub"
credentials {
username = providers.environmentVariable("GITHUB_ACTOR").get()
password = providers.environmentVariable("GITHUB_TOKEN").get()
name = "LocalMaven"
url = projectDir.resolve(".localMaven").toURI()
}

if (providers.environmentVariable("GITHUB_TOKEN").isPresent) {
maven {
url = uri("https://maven.pkg.github.com/badasintended/explosion")
name = "GitHub"
credentials {
username = providers.environmentVariable("GITHUB_ACTOR").get()
password = providers.environmentVariable("GITHUB_TOKEN").get()
}
}
}
}

if (providers.environmentVariable("MAVEN_PASSWORD").isPresent) {
maven {
url = uri("https://maven4.bai.lol")
name = "Badasintended"
credentials {
username = providers.environmentVariable("MAVEN_USERNAME").orNull
password = providers.environmentVariable("MAVEN_PASSWORD").orNull
if (providers.environmentVariable("MAVEN_PASSWORD").isPresent) {
maven {
url = uri("https://maven4.bai.lol")
name = "Badasintended"
credentials {
username = providers.environmentVariable("MAVEN_USERNAME").orNull
password = providers.environmentVariable("MAVEN_PASSWORD").orNull
}
}
}
}
Expand Down
26 changes: 26 additions & 0 deletions explosion-gradle-plugin/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
plugins {
`java-gradle-plugin`
`kotlin-dsl`
}

dependencies {
implementation("com.google.guava:guava:32.1.2-jre")
}

gradlePlugin {
plugins {
create("explosion") {
id = "lol.bai.explosion"
implementationClass = "lol.bai.explosion.gradle.ExplosionPlugin"
}
}
}

tasks.processResources {
val meta = "${project.group}:${project.name}:${project.version}"
inputs.property("meta", meta)

filesMatching("__meta.txt") {
expand("meta" to meta)
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package lol.bai.explosion
package lol.bai.explosion.gradle

import org.gradle.api.artifacts.ExternalModuleDependency
import org.gradle.api.provider.Provider
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package lol.bai.explosion
package lol.bai.explosion.gradle

import groovy.lang.Closure
import groovy.lang.DelegatesTo
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package lol.bai.explosion
package lol.bai.explosion.gradle

import lol.bai.explosion.internal.ExplosionExtImpl
import lol.bai.explosion.internal.resolver.ResolverTask
import lol.bai.explosion.gradle.internal.ExplosionExtImpl
import org.gradle.api.Plugin
import org.gradle.api.Project
import org.gradle.kotlin.dsl.*
import org.gradle.kotlin.dsl.create
import org.gradle.kotlin.dsl.maven
import java.io.File
import kotlin.io.path.createDirectories

Expand All @@ -24,12 +24,6 @@ class ExplosionPlugin : Plugin<Project> {
repositories.maven(outputDir.toFile()) {
name = "ExplodedPluginCache"
}

val resolver = configurations.create(ResolverTask.CONFIGURATION)
dependencies {
resolver(embeddedKotlin("stdlib"))
resolver(ExplosionPlugin::class.java.classLoader.getResource("__meta.txt")!!.readText().trim())
}
}

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package lol.bai.explosion.internal
package lol.bai.explosion.gradle.internal

import lol.bai.explosion.ExplosionDesc
import lol.bai.explosion.gradle.ExplosionDesc
import org.gradle.api.Project
import org.gradle.api.artifacts.ExternalModuleDependency
import org.gradle.api.artifacts.FileCollectionDependency
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
package lol.bai.explosion.internal
package lol.bai.explosion.gradle.internal

import com.google.common.hash.Hashing
import lol.bai.explosion.ExplosionDesc
import lol.bai.explosion.ExplosionExt
import lol.bai.explosion.internal.resolver.ResolverTask
import lol.bai.explosion.gradle.ExplosionDesc
import lol.bai.explosion.gradle.ExplosionExt
import org.gradle.api.Action
import org.gradle.api.Project
import org.gradle.api.Transformer
import org.gradle.kotlin.dsl.create
import org.gradle.kotlin.dsl.dependencies
import org.gradle.kotlin.dsl.embeddedKotlin
import org.gradle.kotlin.dsl.invoke
import java.io.File
import java.nio.file.Path
Expand All @@ -33,6 +34,10 @@ open class ExplosionExtImpl(
return resolve(path.joinToString(File.separator))
}

private fun log(message: String) {
project.logger.lifecycle("lol.bai.explosion: $message")
}

private fun createPom(loader: String, name: String, version: String, jarPlacer: (Path) -> Unit): BomDependency {
val group = "exploded"

Expand Down Expand Up @@ -72,11 +77,11 @@ open class ExplosionExtImpl(
val output = dir.resolve("${hash}-1.pom")

if (output.exists()) {
project.logger.lifecycle("Exploded BOM for hash $hash already exists, skipping")
log("Exploded BOM for hash $hash already exists, skipping")
return bom
}

project.logger.lifecycle("Building exploded BOM for hash $hash")
log("Building exploded BOM for hash $hash")

val depTemplate = this.javaClass.classLoader.getResource("bom_dependency.xml")!!.readText()

Expand All @@ -100,12 +105,14 @@ open class ExplosionExtImpl(
.replace("%DEPENDENCIES%", depsStr.toString())

output.writeText(pom)

log("Done")
return bom
}

private fun resolve(
action: Action<ExplosionDesc>,
loader: String
loader: String,
) = project.provider {
val desc = ExplosionDescImpl(project)
action(desc)
Expand All @@ -127,25 +134,37 @@ open class ExplosionExtImpl(
val hash = Hashing.murmur3_128().hashString(hashBuilder.toString(), Charsets.UTF_8).toString()

return@provider getOrCreateBom(hash) {
val task = project.tasks.create<ResolverTask>("__explosion_resolver_" + Any().hashCode()) {
this.loader.set(loader)
val key = "__explosion_resolver_" + Any().hashCode()
val meta = javaClass.classLoader.getResource("__meta.txt")!!.readText().trim()
val (group, _, version) = meta.split(':')
val configuration = project.configurations.create(key)

project.dependencies {
configuration(embeddedKotlin("stdlib"))
configuration(group, "explosion-resolver-${loader}", version)
}

val task = project.tasks.create<ResolverTask>(key) {
this.configuration.set(key)
this.mainClass.set("lol.bai.explosion.resolver.${loader}.MainKt")
this.inputDir.set(inputDir.toFile())
this.outputDir.set(outputDir.toFile())
}

task.exec()
task.enabled = false
val metaLines = outputDir.resolve("__meta.txt").readLines()
val bomDeps = arrayListOf<BomDependency>()
project.configurations.remove(configuration)

for (line in metaLines) {
val trimmed = line.trim()
if (trimmed.isEmpty()) continue

val (modFile, modId, version) = trimmed.split("\t")
bomDeps.add(createPom(loader, modId, version) { path ->
outputDir.resolve(modFile).copyTo(path)
})
val bomDeps = arrayListOf<BomDependency>()
outputDir.resolve("__meta.txt").forEachLine { line ->
val trimmed = line.trim()
if (trimmed.isNotEmpty()) {
val (modFile, modId, modVersion) = trimmed.split("\t")
bomDeps.add(createPom(loader, modId, modVersion) { path ->
outputDir.resolve(modFile).copyTo(path)
})
}
}

return@getOrCreateBom bomDeps
Expand Down
Loading

0 comments on commit e043611

Please sign in to comment.