|
| 1 | +package io.papermc.mache.tasks |
| 2 | + |
| 3 | +import io.papermc.mache.MacheRepo |
| 4 | +import io.papermc.mache.convertToPath |
| 5 | +import io.papermc.mache.ensureClean |
| 6 | +import io.papermc.mache.lib.data.meta.MacheDependencies |
| 7 | +import io.papermc.mache.lib.data.meta.MacheMeta |
| 8 | +import io.papermc.mache.lib.data.meta.MacheRepository |
| 9 | +import io.papermc.mache.lib.data.meta.MavenArtifact |
| 10 | +import io.papermc.mache.lib.json |
| 11 | +import javax.inject.Inject |
| 12 | +import kotlin.io.path.writeText |
| 13 | +import kotlinx.serialization.encodeToString |
| 14 | +import org.gradle.api.DefaultTask |
| 15 | +import org.gradle.api.NamedDomainObjectContainer |
| 16 | +import org.gradle.api.artifacts.Configuration |
| 17 | +import org.gradle.api.artifacts.ExternalDependency |
| 18 | +import org.gradle.api.file.ProjectLayout |
| 19 | +import org.gradle.api.file.RegularFileProperty |
| 20 | +import org.gradle.api.provider.ListProperty |
| 21 | +import org.gradle.api.provider.Property |
| 22 | +import org.gradle.api.tasks.Classpath |
| 23 | +import org.gradle.api.tasks.Input |
| 24 | +import org.gradle.api.tasks.Nested |
| 25 | +import org.gradle.api.tasks.OutputFile |
| 26 | +import org.gradle.api.tasks.TaskAction |
| 27 | + |
| 28 | +abstract class GenerateMacheMetadata : DefaultTask() { |
| 29 | + |
| 30 | + @get:Nested |
| 31 | + abstract val repos: NamedDomainObjectContainer<MacheRepo> |
| 32 | + |
| 33 | + @get:Classpath |
| 34 | + abstract val codebookConfiguration: Property<Configuration> |
| 35 | + |
| 36 | + @get:Classpath |
| 37 | + abstract val paramMappingsConfiguration: Property<Configuration> |
| 38 | + |
| 39 | + @get:Classpath |
| 40 | + abstract val constantsConfiguration: Property<Configuration> |
| 41 | + |
| 42 | + @get:Classpath |
| 43 | + abstract val remapperConfiguration: Property<Configuration> |
| 44 | + |
| 45 | + @get:Classpath |
| 46 | + abstract val decompilerConfiguration: Property<Configuration> |
| 47 | + |
| 48 | + @get:Input |
| 49 | + abstract val decompilerArgs: ListProperty<String> |
| 50 | + |
| 51 | + @get:OutputFile |
| 52 | + abstract val outputFile: RegularFileProperty |
| 53 | + |
| 54 | + @get:Inject |
| 55 | + abstract val layout: ProjectLayout |
| 56 | + |
| 57 | + init { |
| 58 | + run { |
| 59 | + outputFile.convention(layout.buildDirectory.dir(name).map { it.file("$name.json") }) |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + @TaskAction |
| 64 | + fun run() { |
| 65 | + val codebook = codebookConfiguration.get().asMavenArtifacts() |
| 66 | + val paramMappings = paramMappingsConfiguration.get().asMavenArtifacts() |
| 67 | + val constants = constantsConfiguration.get().asMavenArtifacts() |
| 68 | + val remapper = remapperConfiguration.get().asMavenArtifacts() |
| 69 | + val decompiler = decompilerConfiguration.get().asMavenArtifacts() |
| 70 | + |
| 71 | + val meta = MacheMeta( |
| 72 | + dependencies = MacheDependencies(codebook, paramMappings, constants, remapper, decompiler), |
| 73 | + repositories = repos.map { r -> |
| 74 | + MacheRepository(r.url.get(), r.name, r.includeGroups.get().takeIf { it.isNotEmpty() }) |
| 75 | + }, |
| 76 | + decompilerArgs = decompilerArgs.get(), |
| 77 | + ) |
| 78 | + val metaJson = json.encodeToString(meta) |
| 79 | + |
| 80 | + outputFile.convertToPath().ensureClean().writeText(metaJson) |
| 81 | + } |
| 82 | + |
| 83 | + private fun Configuration.asMavenArtifacts(): List<MavenArtifact> { |
| 84 | + return dependencies.map { dep -> |
| 85 | + val group = dep.group ?: throw IllegalStateException("Dependency without group: $dep") |
| 86 | + val version = dep.version ?: throw IllegalStateException("Dependency without version: $dep") |
| 87 | + val classifier = (dep as? ExternalDependency)?.artifacts?.firstNotNullOfOrNull { it.classifier } |
| 88 | + val extension = (dep as? ExternalDependency)?.artifacts?.firstNotNullOfOrNull { it.extension }?.takeIf { it != "jar" } |
| 89 | + MavenArtifact(group, dep.name, version, classifier, extension) |
| 90 | + } |
| 91 | + } |
| 92 | +} |
0 commit comments