From 34334396178eec3f72f1228b400cb9ec81c4ce4c Mon Sep 17 00:00:00 2001 From: Linnea Gräf Date: Tue, 7 Jan 2025 14:07:14 +0100 Subject: build: Split up dependency injection into its own package --- build-src/src/main/kotlin/GenerateItemIds.kt | 72 ++++++++++++++++++++++++++++ 1 file changed, 72 insertions(+) create mode 100644 build-src/src/main/kotlin/GenerateItemIds.kt (limited to 'build-src/src/main/kotlin/GenerateItemIds.kt') diff --git a/build-src/src/main/kotlin/GenerateItemIds.kt b/build-src/src/main/kotlin/GenerateItemIds.kt new file mode 100644 index 0000000..24f2f62 --- /dev/null +++ b/build-src/src/main/kotlin/GenerateItemIds.kt @@ -0,0 +1,72 @@ +import com.google.gson.Gson +import org.gradle.api.DefaultTask +import org.gradle.api.file.DirectoryProperty +import org.gradle.api.provider.Property +import org.gradle.api.tasks.Input +import org.gradle.api.tasks.InputDirectory +import org.gradle.api.tasks.Internal +import org.gradle.api.tasks.OutputDirectory +import org.gradle.api.tasks.TaskAction +import java.io.File + +abstract class GenerateItemIds : DefaultTask() { + @get: OutputDirectory + abstract val outputDirectory: DirectoryProperty + + @get: InputDirectory + abstract val repoFiles: DirectoryProperty + + @get: Input + abstract val repoHash: Property + + @get: Input + abstract val packageName: Property + + @get:Internal + val outputFile get() = outputDirectory.asFile.get().resolve(packageName.get().replace(".", "/") + "/ItemIds.java") + + init { + repoHash.convention("unknown-repo-git-hash") + } + + @TaskAction + fun generateItemIds() { + val nonIdName = "[^A-Z0-9_]".toRegex() + + data class Item(val id: String, val file: File) { + val javaName get() = id.replace(nonIdName, { "__" + it.value.single().code }) + } + + val items = mutableListOf() + for (listFile in repoFiles.asFile.get().resolve("items").listFiles() ?: emptyArray()) { + listFile ?: continue + if (listFile.extension != "json") { + error("Unknown file $listFile") + } + items.add(Item(listFile.nameWithoutExtension, listFile)) + } + items.sortedBy { it.id } + outputFile.parentFile.mkdirs() + val writer = outputFile.writer().buffered() + writer.appendLine("// @generated from " + repoHash.get()) + writer.appendLine("package " + packageName.get() + ";") + writer.appendLine() + writer.appendLine("import moe.nea.ledger.ItemId;") + writer.appendLine() + writer.appendLine("/**") + writer.appendLine(" * Automatically generated {@link ItemId} list.") + writer.appendLine(" */") + writer.appendLine("@org.jspecify.annotations.NullMarked") + writer.appendLine("public interface ItemIds {") + val gson = Gson() + for (item in items) { + writer.appendLine("\t/**") + writer.appendLine("\t * @see JSON definition") + writer.appendLine("\t */") + writer.appendLine("\tItemId ${item.javaName} =" + + " ItemId.forName(${gson.toJson(item.id)});") + } + writer.appendLine("}") + writer.close() + } +} -- cgit