aboutsummaryrefslogtreecommitdiff
path: root/build-src
diff options
context:
space:
mode:
authorLinnea Gräf <nea@nea.moe>2025-01-07 14:07:14 +0100
committerLinnea Gräf <nea@nea.moe>2025-01-07 14:07:14 +0100
commit34334396178eec3f72f1228b400cb9ec81c4ce4c (patch)
tree416d05a1193aa787c3fcbe44431a87d9ec555f3b /build-src
parentf2783ceea23147d2702c47c9b47c06ad7fc707a8 (diff)
downloadLocalTransactionLedger-34334396178eec3f72f1228b400cb9ec81c4ce4c.tar.gz
LocalTransactionLedger-34334396178eec3f72f1228b400cb9ec81c4ce4c.tar.bz2
LocalTransactionLedger-34334396178eec3f72f1228b400cb9ec81c4ce4c.zip
build: Split up dependency injection into its own package
Diffstat (limited to 'build-src')
-rw-r--r--build-src/README.md2
-rw-r--r--build-src/build.gradle.kts11
-rw-r--r--build-src/settings.gradle.kts0
-rw-r--r--build-src/src/main/kotlin/GenerateItemIds.kt72
-rw-r--r--build-src/src/main/kotlin/RepoDownload.kt41
-rw-r--r--build-src/src/main/kotlin/ledger-marker.gradle.kts0
-rw-r--r--build-src/src/main/kotlin/ledger-repo.gradle.kts1
7 files changed, 127 insertions, 0 deletions
diff --git a/build-src/README.md b/build-src/README.md
new file mode 100644
index 0000000..66d45bc
--- /dev/null
+++ b/build-src/README.md
@@ -0,0 +1,2 @@
+
+Intentionally not using `buildSrc` over an `includeBuild("build-src")` due to better performance of composite builds.
diff --git a/build-src/build.gradle.kts b/build-src/build.gradle.kts
new file mode 100644
index 0000000..f203fd6
--- /dev/null
+++ b/build-src/build.gradle.kts
@@ -0,0 +1,11 @@
+plugins {
+ kotlin("jvm") version "2.0.20"
+ `kotlin-dsl`
+}
+repositories {
+ mavenCentral()
+}
+dependencies {
+ implementation("com.google.code.gson:gson:2.11.0")
+ implementation(gradleApi())
+}
diff --git a/build-src/settings.gradle.kts b/build-src/settings.gradle.kts
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/build-src/settings.gradle.kts
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<String>
+
+ @get: Input
+ abstract val packageName: Property<String>
+
+ @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<Item>()
+ 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 <a href=${gson.toJson("https://github.com/NotEnoughUpdates/NotEnoughUpdates-REPO/blob/${repoHash.get()}/items/${item.id}.json")}>JSON definition</a>")
+ writer.appendLine("\t */")
+ writer.appendLine("\tItemId ${item.javaName} =" +
+ " ItemId.forName(${gson.toJson(item.id)});")
+ }
+ writer.appendLine("}")
+ writer.close()
+ }
+}
diff --git a/build-src/src/main/kotlin/RepoDownload.kt b/build-src/src/main/kotlin/RepoDownload.kt
new file mode 100644
index 0000000..182c66f
--- /dev/null
+++ b/build-src/src/main/kotlin/RepoDownload.kt
@@ -0,0 +1,41 @@
+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.OutputDirectory
+import org.gradle.api.tasks.TaskAction
+import java.net.URI
+import java.util.zip.ZipInputStream
+
+abstract class RepoDownload : DefaultTask() {
+ @get:Input
+ abstract val hash: Property<String>
+
+ @get:OutputDirectory
+ abstract val outputDirectory: DirectoryProperty
+
+ init {
+ outputDirectory.convention(project.layout.buildDirectory.dir("extracted-test-repo"))
+ }
+
+ @TaskAction
+ fun performDownload() {
+ val outputDir = outputDirectory.asFile.get().absoluteFile
+ outputDir.mkdirs()
+ URI("https://github.com/notEnoughUpdates/notEnoughUpdates-rEPO/archive/${hash.get()}.zip").toURL().openStream()
+ .let(::ZipInputStream)
+ .use { zipInput ->
+ while (true) {
+ val entry = zipInput.nextEntry ?: break
+ val destination = outputDir.resolve(
+ entry.name.substringAfter('/')).absoluteFile
+ require(outputDir in generateSequence(destination) { it.parentFile })
+ if (entry.isDirectory) continue
+ destination.parentFile.mkdirs()
+ destination.outputStream().use { output ->
+ zipInput.copyTo(output)
+ }
+ }
+ }
+ }
+} \ No newline at end of file
diff --git a/build-src/src/main/kotlin/ledger-marker.gradle.kts b/build-src/src/main/kotlin/ledger-marker.gradle.kts
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/build-src/src/main/kotlin/ledger-marker.gradle.kts
diff --git a/build-src/src/main/kotlin/ledger-repo.gradle.kts b/build-src/src/main/kotlin/ledger-repo.gradle.kts
new file mode 100644
index 0000000..1a9be63
--- /dev/null
+++ b/build-src/src/main/kotlin/ledger-repo.gradle.kts
@@ -0,0 +1 @@
+tasks.register("downloadRepo", RepoDownload::class)