aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--example/build.gradle.kts9
-rw-r--r--example/src/main/kotlin/test.kt16
-rw-r--r--kotlin-plugin/src/main/kotlin/moe/nea/mcautotranslations/kotlin/MCAutoTranslationsCallTransformerAndCollector.kt3
-rw-r--r--kotlin-plugin/src/main/kotlin/moe/nea/mcautotranslations/kotlin/MCAutoTranslationsCommandLineProcessor.kt7
-rw-r--r--kotlin-plugin/src/main/kotlin/moe/nea/mcautotranslations/kotlin/util.kt13
5 files changed, 41 insertions, 7 deletions
diff --git a/example/build.gradle.kts b/example/build.gradle.kts
index b3f0a88..99b62f9 100644
--- a/example/build.gradle.kts
+++ b/example/build.gradle.kts
@@ -9,14 +9,19 @@ repositories {
mavenCentral()
}
+dependencies {
+ implementation("com.google.code.gson:gson:2.11.0")
+}
+
mcAutoTranslations {
translationFunction.set("moe.nea.mcautotranslations.example.tr")
translationFunctionResolved.set("moe.nea.mcautotranslations.example.trResolved")
}
-tasks.register("collectTranslations", CollectTranslations::class) {
+val collectTranslations by tasks.registering(CollectTranslations::class) {
this.baseTranslations.from(file("en_us.json"))
this.classes.from(sourceSets.main.map { it.kotlin.classesDirectory })
- this.outputFile.set(layout.buildDirectory.file("compiled_en_us.json"))
+ this.outputFile.set(layout.buildDirectory.file("en_us.json"))
}
+tasks.processResources { from(collectTranslations) }
diff --git a/example/src/main/kotlin/test.kt b/example/src/main/kotlin/test.kt
index 291cdfe..7702dad 100644
--- a/example/src/main/kotlin/test.kt
+++ b/example/src/main/kotlin/test.kt
@@ -1,8 +1,20 @@
package moe.nea.mcautotranslations.example
-data class Text(val key: String, val args: List<Any>)
+import com.google.gson.Gson
+import com.google.gson.reflect.TypeToken
-fun trResolved(key: String, vararg args: Any) = Text(key, args.toList())
+val resources =
+ Text::class.java.classLoader.getResourceAsStream("en_us.json")!!.reader().use {
+ Gson().fromJson(it, object : TypeToken<HashMap<String, String>>() {})
+ }
+
+class Text(val key: String, val args: Array<out Any>) {
+ override fun toString(): String {
+ return resources[key]!!.format(*args)
+ }
+}
+
+fun trResolved(key: String, vararg args: Any) = Text(key, args)
fun tr(key: String, default: String): Text = error("Did not run compiler plugin")
fun main() {
println(tr("test1", "Hiiiiiii"))
diff --git a/kotlin-plugin/src/main/kotlin/moe/nea/mcautotranslations/kotlin/MCAutoTranslationsCallTransformerAndCollector.kt b/kotlin-plugin/src/main/kotlin/moe/nea/mcautotranslations/kotlin/MCAutoTranslationsCallTransformerAndCollector.kt
index cc38b57..fc1acc1 100644
--- a/kotlin-plugin/src/main/kotlin/moe/nea/mcautotranslations/kotlin/MCAutoTranslationsCallTransformerAndCollector.kt
+++ b/kotlin-plugin/src/main/kotlin/moe/nea/mcautotranslations/kotlin/MCAutoTranslationsCallTransformerAndCollector.kt
@@ -16,6 +16,7 @@ import org.jetbrains.kotlin.ir.expressions.IrConst
import org.jetbrains.kotlin.ir.expressions.IrConstKind
import org.jetbrains.kotlin.ir.expressions.IrExpression
import org.jetbrains.kotlin.ir.expressions.IrStringConcatenation
+import org.jetbrains.kotlin.ir.symbols.UnsafeDuringIrConstructionAPI
import org.jetbrains.kotlin.ir.types.makeNullable
import org.jetbrains.kotlin.ir.util.SYNTHETIC_OFFSET
import org.jetbrains.kotlin.ir.util.kotlinFqName
@@ -25,6 +26,7 @@ import org.jetbrains.kotlin.name.ClassId
import org.jetbrains.kotlin.name.FqName
import org.jetbrains.kotlin.name.Name
+@OptIn(UnsafeDuringIrConstructionAPI::class)
class MCAutoTranslationsCallTransformerAndCollector(
val file: IrFile,
val irPluginContext: IrPluginContext,
@@ -130,6 +132,7 @@ class MCAutoTranslationsCallTransformerAndCollector(
}
+ @Suppress("UNCHECKED_CAST")
fun constString(
text: String,
startOffset: Int = SYNTHETIC_OFFSET,
diff --git a/kotlin-plugin/src/main/kotlin/moe/nea/mcautotranslations/kotlin/MCAutoTranslationsCommandLineProcessor.kt b/kotlin-plugin/src/main/kotlin/moe/nea/mcautotranslations/kotlin/MCAutoTranslationsCommandLineProcessor.kt
index 64286d0..bb6f24c 100644
--- a/kotlin-plugin/src/main/kotlin/moe/nea/mcautotranslations/kotlin/MCAutoTranslationsCommandLineProcessor.kt
+++ b/kotlin-plugin/src/main/kotlin/moe/nea/mcautotranslations/kotlin/MCAutoTranslationsCommandLineProcessor.kt
@@ -10,6 +10,7 @@ import org.jetbrains.kotlin.compiler.plugin.CommandLineProcessor
import org.jetbrains.kotlin.compiler.plugin.ExperimentalCompilerApi
import org.jetbrains.kotlin.config.CompilerConfiguration
import org.jetbrains.kotlin.config.CompilerConfigurationKey
+import org.jetbrains.kotlin.config.messageCollector
import org.jetbrains.kotlin.name.FqName
@@ -36,11 +37,11 @@ class MCAutoTranslationsCommandLineProcessor : CommandLineProcessor {
override fun processOption(option: AbstractCliOption, value: String, configuration: CompilerConfiguration) {
when (option.optionName) {
resolvedFunctionOption.optionName ->
- configuration.put(translateFunction, FqName(value))
+ configuration.put(resolvedFunction, FqName(value))
translateFunctionOption.optionName ->
- configuration.put(resolvedFunction, FqName(value))
+ configuration.put(translateFunction, FqName(value))
else -> error("Unknown config option ${option.optionName}")
}
}
-} \ No newline at end of file
+}
diff --git a/kotlin-plugin/src/main/kotlin/moe/nea/mcautotranslations/kotlin/util.kt b/kotlin-plugin/src/main/kotlin/moe/nea/mcautotranslations/kotlin/util.kt
new file mode 100644
index 0000000..690838e
--- /dev/null
+++ b/kotlin-plugin/src/main/kotlin/moe/nea/mcautotranslations/kotlin/util.kt
@@ -0,0 +1,13 @@
+package moe.nea.mcautotranslations.kotlin
+
+import org.jetbrains.kotlin.cli.common.messages.CompilerMessageSeverity
+import org.jetbrains.kotlin.cli.common.messages.CompilerMessageSourceLocation
+import org.jetbrains.kotlin.cli.common.messages.MessageCollector
+import org.jetbrains.kotlin.config.CompilerConfiguration
+import org.jetbrains.kotlin.config.messageCollector
+
+
+fun CompilerConfiguration.info(text: String) = messageCollector.info(text)
+fun MessageCollector.info(text: String, place: CompilerMessageSourceLocation? = null) {
+ report(CompilerMessageSeverity.WARNING, "MC Auto Translation: $text", place)
+}