Skip to content

Commit

Permalink
Fix component metadata rules trying to use classifiers
Browse files Browse the repository at this point in the history
  • Loading branch information
MsRandom committed Dec 16, 2024
1 parent 303d23e commit bd35869
Show file tree
Hide file tree
Showing 8 changed files with 188 additions and 85 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ org.gradle.parallel=true
org.gradle.configuration-cache=true

group=net.msrandom
version=0.3.0
version=0.3.2
kotlin.code.style=official
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package net.msrandom.minecraftcodev.accesswidener

import net.msrandom.minecraftcodev.core.resolve.isCodevGeneratedMinecraftJar
import net.msrandom.minecraftcodev.core.utils.toPath
import net.msrandom.minecraftcodev.core.utils.walk
import net.msrandom.minecraftcodev.core.utils.zipFileSystem
import net.msrandom.minecraftcodev.core.utils.*
import org.gradle.api.DefaultTask
import org.gradle.api.artifacts.transform.*
import org.gradle.api.file.ConfigurableFileCollection
import org.gradle.api.file.FileSystemLocation
import org.gradle.api.file.RegularFileProperty
import org.gradle.api.provider.Property
import org.gradle.api.provider.Provider
import org.gradle.api.tasks.*
Expand All @@ -17,42 +17,46 @@ import java.nio.file.Path
import java.nio.file.StandardCopyOption
import kotlin.io.path.*

@CacheableTransform
abstract class AccessWiden : TransformAction<AccessWiden.Parameters> {
abstract class Parameters : TransformParameters {
abstract val accessWideners: ConfigurableFileCollection
@InputFiles
@PathSensitive(PathSensitivity.RELATIVE)
get

abstract val namespace: Property<String>
@Input
@Optional
get
}

abstract val inputFile: Provider<FileSystemLocation>
@InputArtifact
@PathSensitive(PathSensitivity.NONE)
@CacheableTask
abstract class AccessWiden : DefaultTask() {
abstract val inputFile: RegularFileProperty
@InputFile
@Classpath
get

override fun transform(outputs: TransformOutputs) {
val input = inputFile.get().toPath()
abstract val accessWideners: ConfigurableFileCollection
@InputFiles
@PathSensitive(PathSensitivity.RELATIVE)
get

if (parameters.accessWideners.isEmpty || !isCodevGeneratedMinecraftJar(input)) {
outputs.file(inputFile)
abstract val namespace: Property<String>
@Input
@Optional
get

return
}
abstract val outputFile: RegularFileProperty
@OutputFile get

init {
outputFile.convention(
project.layout.file(
inputFile.map {
temporaryDir.resolve("${it.asFile.nameWithoutExtension}-access-widened.${it.asFile.extension}")
},
),
)
}

println("Access widening $input")
@TaskAction
fun accessWiden() {
outputFile.getAsPath().deleteIfExists()

val accessModifiers = loadAccessWideners(parameters.accessWideners, parameters.namespace.takeIf(Property<*>::isPresent)?.get())
val input = inputFile.get().toPath()

val output = outputs.file("${input.nameWithoutExtension}-access-widened.${input.extension}").toPath()
val accessModifiers = loadAccessWideners(accessWideners, namespace.takeIf(Property<*>::isPresent)?.get())

zipFileSystem(input).use { inputZip ->
zipFileSystem(output, true).use { outputZip ->
zipFileSystem(outputFile.getAsPath(), true).use { outputZip ->
inputZip.getPath("/").walk {
for (path in filter(Path::isRegularFile)) {
val name = path.toString()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package net.msrandom.minecraftcodev.core

import net.msrandom.minecraftcodev.core.resolve.MinecraftVersionList
import net.msrandom.minecraftcodev.core.resolve.getAllDependencies
import net.msrandom.minecraftcodev.core.resolve.getClientDependencies
import net.msrandom.minecraftcodev.core.resolve.setupCommon
import org.gradle.api.artifacts.CacheableRule
Expand Down Expand Up @@ -29,41 +30,33 @@ abstract class MinecraftComponentMetadataRule<T : Any> @Inject constructor(
private val versionManifestUrl: String,
private val isOffline: Boolean,
private val client: Boolean,
private val variantName: String,
private val attribute: Attribute<T>,
private val commonAttributeValue: T,
private val clientAttributeValue: T,

private val attribute: Attribute<String>,
private val commonAttributeValue: String,
private val clientAttributeValue: String,
) : ComponentMetadataRule {
abstract val objectFactory: ObjectFactory
@Inject get

override fun execute(context: ComponentMetadataContext) {
val variantName = if (client) {
clientAttributeValue
} else {
commonAttributeValue
}

context.details.addVariant(variantName) {
it.attributes { attributes ->
attributes
.attribute(Category.CATEGORY_ATTRIBUTE, objectFactory.named(Category::class.java, Category.REGULAR_PLATFORM))
.attribute(attribute, clientAttributeValue)
}

it.withCapabilities { capabilities ->
capabilities.addCapability("net.minecraft.dependencies", if (client) "client" else "server", "1.0.0")
.attribute(attribute, variantName)
}

it.withDependencies { dependencies ->
val versionMetadata = getVersionList(cacheDirectory.toPath(), versionManifestUrl, isOffline).version(version)

if (client) {
val id = context.details.id

getClientDependencies(cacheDirectory.toPath(), versionMetadata, isOffline).forEach(dependencies::add)

dependencies.add("${id.group}:${id.name}:${id.version}") { commonDependency ->
commonDependency.attributes { attributes ->
attributes
.attribute(Category.CATEGORY_ATTRIBUTE, objectFactory.named(Category::class.java, Category.REGULAR_PLATFORM))
.attribute(attribute, commonAttributeValue)
}
}
getAllDependencies(versionMetadata).forEach(dependencies::add)
} else {
setupCommon(cacheDirectory.toPath(), versionMetadata, isOffline).forEach(dependencies::add)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package net.msrandom.minecraftcodev.forge

import org.gradle.api.artifacts.CacheableRule
import org.gradle.api.artifacts.ComponentMetadataContext
import org.gradle.api.artifacts.ComponentMetadataDetails
import org.gradle.api.artifacts.ComponentMetadataRule
import org.gradle.api.artifacts.repositories.RepositoryResourceAccessor
import org.gradle.api.attributes.Attribute
import org.gradle.api.attributes.Category
import org.gradle.api.model.ObjectFactory
import javax.inject.Inject

@JvmField
val CLASSIFIER_ATTRIBUTE: Attribute<String> =
Attribute.of("net.msrandom.minecraftcodev.forge-library-classifier", String::class.java)

@CacheableRule
abstract class MinecraftForgeComponentClassifierMetadataRule @Inject constructor(private val userdevs: List<UserdevPath>) :
ComponentMetadataRule {
abstract val repositoryResourceAccessor: RepositoryResourceAccessor
@Inject get

private fun ComponentMetadataDetails.addClassifier(classifier: String) {
withVariant("compile") {
it.attributes.attribute(CLASSIFIER_ATTRIBUTE, "")
}

withVariant("runtime") {
it.attributes.attribute(CLASSIFIER_ATTRIBUTE, "")
}

addVariant("${classifier}-compile", "compile") {
it.attributes.attribute(CLASSIFIER_ATTRIBUTE, classifier)

it.withFiles {
it.removeAllFiles()

it.addFile("${id.name}-${id.version}-$classifier.jar")
}
}

addVariant("${classifier}-runtime", "runtime") {
it.attributes.attribute(CLASSIFIER_ATTRIBUTE, classifier)

it.withFiles {
it.removeAllFiles()

it.addFile("${id.name}-${id.version}-$classifier.jar")
}
}
}

override fun execute(context: ComponentMetadataContext) {
val id = context.details.id

val currentUserdev = userdevs.firstOrNull {
it.group == id.group && it.name == id.name && it.version == id.version
}

if (currentUserdev != null) {
context.details.addClassifier(currentUserdev.classifier)
}

val userdevLibraries = userdevs.flatMapTo(hashSetOf()) {
getUserdev(it, repositoryResourceAccessor)?.libraries ?: emptyList()
}

val library = userdevLibraries.firstOrNull { "${id.group}:${id.name}:${id.version}" in it } ?: return

val classifier = getClassifier(library)

if (classifier != null) {
context.details.addClassifier(classifier)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,49 @@ import org.gradle.api.attributes.Attribute
import org.gradle.api.attributes.Category
import org.gradle.api.model.ObjectFactory
import java.io.File
import java.io.Serializable
import java.util.zip.ZipInputStream
import javax.inject.Inject

class UserdevPath(internal val group: String, internal val name: String, internal val version: String, internal val classifier: String) : Serializable

fun getUserdev(
userdev: UserdevPath,
repositoryResourceAccessor: RepositoryResourceAccessor,
): UserdevConfig? {
val path = "${userdev.group.replace('.', '/')}/${userdev.name}/${userdev.version}/${userdev.name}-${userdev.version}-${userdev.classifier}.jar"

var userdev: UserdevConfig? = null

repositoryResourceAccessor.withResource(path) {
val zipStream = ZipInputStream(it)

while (true) {
val entry = zipStream.nextEntry ?: break

try {
if (entry.name == "config.json") {
userdev = json.decodeFromStream<UserdevConfig>(zipStream)
break
}
} finally {
zipStream.closeEntry()
}
}
}

return userdev
}

fun getClassifier(library: String) = library.split(':').getOrNull(3)?.split('@')?.get(0)

@CacheableRule
abstract class MinecraftForgeComponentMetadataRule<T : Any> @Inject constructor(
private val cacheDirectory: File,
private val version: String,
private val versionManifestUrl: String,
private val isOffline: Boolean,
private val userdevGroup: String,
private val userdevName: String,
private val userdevVersion: String,
private val userdevClassifier: String,
private val userdev: UserdevPath,
private val variantName: String,
private val attribute: Attribute<T>,
private val attributeValue: T,
Expand All @@ -37,29 +67,8 @@ abstract class MinecraftForgeComponentMetadataRule<T : Any> @Inject constructor(

override fun execute(context: ComponentMetadataContext) {
context.details.addVariant(variantName) { variant ->
val userdevJar by lazy {
val path = "${userdevGroup.replace('.', '/')}/$userdevName/$userdevVersion/$userdevName-$userdevVersion-$userdevClassifier.jar"

lateinit var userdev: UserdevConfig

repositoryResourceAccessor.withResource(path) {
val zipStream = ZipInputStream(it)

while (true) {
val entry = zipStream.nextEntry ?: break

try {
if (entry.name == "config.json") {
userdev = json.decodeFromStream<UserdevConfig>(zipStream)
break
}
} finally {
zipStream.closeEntry()
}
}
}

userdev
val userdevJar by lazy(LazyThreadSafetyMode.NONE) {
getUserdev(userdev, repositoryResourceAccessor)!!
}

variant.attributes {
Expand All @@ -76,7 +85,18 @@ abstract class MinecraftForgeComponentMetadataRule<T : Any> @Inject constructor(
}

getAllDependencies(versionMetadata).forEach(it::add)
libraries.forEach(it::add)

for (library in libraries) {
it.add(library) {
val classifier = getClassifier(library)

if (classifier != null) {
it.attributes{ attributes ->
attributes.attribute(CLASSIFIER_ATTRIBUTE, classifier)
}
}
}
}
}

variant.withDependencyConstraints {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -219,10 +219,11 @@ open class ForgeRunsDefaultsContainer(

path.parent.createDirectories()

// TODO Works for neoforge, for forge it should be ${sourceSet.get().runtimeClasspath - sourceSet.get().output}
val runtimeClasspath = project.configurations.getByName(sourceSet.get().runtimeClasspathConfigurationName)

// TODO Hack
val files = runtimeClasspath.map(File::getAbsolutePath).filterNot { "-patched" in it && it.endsWith(".jar") }
// TODO This is making an assumption that the forge Jar is not here
val files = runtimeClasspath.map(File::getAbsolutePath)

path.writeLines(files)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import net.msrandom.minecraftcodev.core.utils.getAsPath
import net.msrandom.minecraftcodev.core.utils.toPath
import net.msrandom.minecraftcodev.core.utils.walk
import net.msrandom.minecraftcodev.core.utils.zipFileSystem
import net.msrandom.minecraftcodev.forge.CLASSIFIER_ATTRIBUTE
import net.msrandom.minecraftcodev.forge.McpConfigFile
import net.msrandom.minecraftcodev.forge.Userdev
import org.gradle.api.Project
Expand Down Expand Up @@ -36,7 +37,13 @@ internal fun resolveFile(
): File =
configurationContainer
.detachedConfiguration(dependencyHandler.create(name))
.apply { isTransitive = false }
.apply {
isTransitive = false

attributes { attributes ->
attributes.attribute(CLASSIFIER_ATTRIBUTE, "")
}
}
.singleFile

internal fun resolveFile(
Expand Down
Loading

0 comments on commit bd35869

Please sign in to comment.