aboutsummaryrefslogtreecommitdiff
path: root/symbols/src/main/kotlin/process/GameTestContainingClassProcessor.kt
blob: 4fcf91f0575745d2211fb73ed15ee789c37c48f2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package moe.nea.firmament.annotations.process

import com.google.auto.service.AutoService
import com.google.devtools.ksp.containingFile
import com.google.devtools.ksp.processing.CodeGenerator
import com.google.devtools.ksp.processing.Dependencies
import com.google.devtools.ksp.processing.KSPLogger
import com.google.devtools.ksp.processing.Resolver
import com.google.devtools.ksp.processing.SymbolProcessor
import com.google.devtools.ksp.processing.SymbolProcessorEnvironment
import com.google.devtools.ksp.processing.SymbolProcessorProvider
import com.google.devtools.ksp.symbol.KSAnnotated
import com.google.devtools.ksp.symbol.KSClassDeclaration
import com.google.devtools.ksp.symbol.KSFile
import com.google.gson.Gson
import com.google.gson.JsonArray
import com.google.gson.JsonObject
import java.io.OutputStreamWriter
import java.nio.charset.StandardCharsets
import java.util.TreeSet

class GameTestContainingClassProcessor(
	val logger: KSPLogger,
	val codeGenerator: CodeGenerator,
	val sourceSetName: String,
) : SymbolProcessor {


	@AutoService(SymbolProcessorProvider::class)
	class Provider : SymbolProcessorProvider {
		override fun create(environment: SymbolProcessorEnvironment): SymbolProcessor {
			return GameTestContainingClassProcessor(
				environment.logger,
				environment.codeGenerator,
				environment.options["firmament.sourceset"] ?: "main")
		}
	}

	val allClasses: MutableSet<String> = TreeSet()
	val allSources = mutableSetOf<KSFile>()

	override fun process(resolver: Resolver): List<KSAnnotated> {
		val annotated = resolver.getSymbolsWithAnnotation("net.minecraft.test.GameTest").toList()
		annotated.forEach {
			val containingClass = it.parent as KSClassDeclaration
			allClasses.add(containingClass.qualifiedName!!.asString())
			allSources.add(it.containingFile!!)
		}
		return emptyList()
	}

	fun createJson(): JsonObject {
		return JsonObject().apply {
			addProperty("schemaVersion", 1)
			addProperty("id", "firmament-gametest")
			addProperty("name", "Firmament Gametest")
			addProperty("version", "1.0.0")
			addProperty("environment", "*")
			add("entrypoints", JsonObject().apply {
				add("fabric-gametest", JsonArray().apply {
					allClasses.forEach {
						add(it)
					}
				})
			})
		}
	}

	override fun finish() {
		if (allClasses.isEmpty()) return
		val stream = codeGenerator.createNewFile(Dependencies(aggregating = true, *allSources.toTypedArray()),
		                                         "",
		                                         "fabric.mod",
		                                         "json")
		val output = OutputStreamWriter(stream, StandardCharsets.UTF_8)
		Gson().toJson(createJson(), output)
		output.close()
	}

}