diff options
author | Linnea Gräf <nea@nea.moe> | 2024-10-30 16:41:30 +0100 |
---|---|---|
committer | Linnea Gräf <nea@nea.moe> | 2024-10-30 16:41:30 +0100 |
commit | f1f7c31fd3bf9987428f4b7233258a0da802ce43 (patch) | |
tree | 6491003753902c1c09bfd776c511108ff78331b2 | |
parent | 51dddbbc06ae4abc074f3ff6b47de82c67fc29ea (diff) | |
download | mcautotranslations-f1f7c31fd3bf9987428f4b7233258a0da802ce43.tar.gz mcautotranslations-f1f7c31fd3bf9987428f4b7233258a0da802ce43.tar.bz2 mcautotranslations-f1f7c31fd3bf9987428f4b7233258a0da802ce43.zip |
Make plugin configurable
12 files changed, 118 insertions, 17 deletions
diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..1bd3f4d --- /dev/null +++ b/.editorconfig @@ -0,0 +1,11 @@ +root = true + +[*] +charset = utf-8 +end_of_line = lf +indent_size = 4 +indent_style = tab +insert_final_newline = true +max_line_length = 120 + + diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml new file mode 100644 index 0000000..fb56e41 --- /dev/null +++ b/.github/workflows/build.yml @@ -0,0 +1,28 @@ +name: Run Gradle Build +on: + - push + - pull_request + +jobs: + gradle: + runs-on: ubuntu-latest + steps: + - name: Checkout source + uses: actions/checkout@v4.1.1 + + - name: Setup Java + uses: actions/setup-java@v4.0.0 + with: + distribution: temurin + java-version: 17 + + - name: Setup Gradle + uses: gradle/actions/setup-gradle@v3 + + - name: Execute Plugin test + run: ./gradlew test + + - name: Execute Example test + run: | + cd example + ./gradlew test diff --git a/.github/workflows/gradle-wrapper-validation.yml b/.github/workflows/gradle-wrapper-validation.yml new file mode 100644 index 0000000..baad506 --- /dev/null +++ b/.github/workflows/gradle-wrapper-validation.yml @@ -0,0 +1,10 @@ +name: "Validate Gradle Wrapper" +on: [ push, pull_request ] + +jobs: + validation: + name: "Validation" + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4.1.1 + - uses: gradle/wrapper-validation-action@v2.0.0 diff --git a/build.gradle.kts b/build.gradle.kts index 43be34b..8237794 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -11,7 +11,7 @@ plugins { allprojects { group = "moe.nea.mcautotranslations" - version = "1.0-SNAPSHOT" + version = "0.0.1" repositories { mavenCentral() @@ -38,5 +38,7 @@ subprojects { buildConfigField<String>("ANNOTATIONS_GROUP", project(":annotations").group.toString()) buildConfigField<String>("ANNOTATIONS_ARTIFACT", project(":annotations").name) buildConfigField<String>("ANNOTATIONS_VERSION", project(":annotations").version.toString()) + buildConfigField<String>("PLUGIN_OPTION_TRANSLATE_FUNCTION", "PLUGIN_OPTION_TRANSLATE_FUNCTION") + buildConfigField<String>("PLUGIN_OPTION_RESOLVED_FUNCTION", "PLUGIN_OPTION_RESOLVED_FUNCTION") } }
\ No newline at end of file diff --git a/example/build.gradle.kts b/example/build.gradle.kts index 2959882..b3f0a88 100644 --- a/example/build.gradle.kts +++ b/example/build.gradle.kts @@ -9,6 +9,11 @@ repositories { mavenCentral() } +mcAutoTranslations { + translationFunction.set("moe.nea.mcautotranslations.example.tr") + translationFunctionResolved.set("moe.nea.mcautotranslations.example.trResolved") +} + tasks.register("collectTranslations", CollectTranslations::class) { this.baseTranslations.from(file("en_us.json")) this.classes.from(sourceSets.main.map { it.kotlin.classesDirectory }) diff --git a/example/src/main/kotlin/test.kt b/example/src/main/kotlin/test.kt index a7b7255..291cdfe 100644 --- a/example/src/main/kotlin/test.kt +++ b/example/src/main/kotlin/test.kt @@ -1,6 +1,5 @@ -package moe.nea.translatetest +package moe.nea.mcautotranslations.example -// TODO: change this name data class Text(val key: String, val args: List<Any>) fun trResolved(key: String, vararg args: Any) = Text(key, args.toList()) diff --git a/gradle-plugin/src/main/kotlin/moe/nea/mcautotranslations/gradle/MCAutoTranslationsExtension.kt b/gradle-plugin/src/main/kotlin/moe/nea/mcautotranslations/gradle/MCAutoTranslationsExtension.kt index 7367056..726145e 100644 --- a/gradle-plugin/src/main/kotlin/moe/nea/mcautotranslations/gradle/MCAutoTranslationsExtension.kt +++ b/gradle-plugin/src/main/kotlin/moe/nea/mcautotranslations/gradle/MCAutoTranslationsExtension.kt @@ -1,7 +1,8 @@ package moe.nea.mcautotranslations.gradle -abstract class MCAutoTranslationsExtension { - - fun translationFunction(name: String) {} // TODO: actual config +import org.gradle.api.provider.Property +abstract class MCAutoTranslationsExtension { + abstract val translationFunction: Property<String> + abstract val translationFunctionResolved: Property<String> } diff --git a/gradle-plugin/src/main/kotlin/moe/nea/mcautotranslations/gradle/MCAutoTranslationsGradlePlugin.kt b/gradle-plugin/src/main/kotlin/moe/nea/mcautotranslations/gradle/MCAutoTranslationsGradlePlugin.kt index 0e8004a..bc7ca3d 100644 --- a/gradle-plugin/src/main/kotlin/moe/nea/mcautotranslations/gradle/MCAutoTranslationsGradlePlugin.kt +++ b/gradle-plugin/src/main/kotlin/moe/nea/mcautotranslations/gradle/MCAutoTranslationsGradlePlugin.kt @@ -21,7 +21,14 @@ class MCAutoTranslationsGradlePlugin : KotlinCompilerPluginSupportPlugin { compileOnly(BuildConfig.ANNOTATIONS_GROUP + ":" + BuildConfig.ANNOTATIONS_ARTIFACT + ":" + BuildConfig.ANNOTATIONS_VERSION) } return project.provider { - listOf() // TODO: add plugin options from extension in here + listOf( + SubpluginOption(BuildConfig.PLUGIN_OPTION_RESOLVED_FUNCTION, + validateFunctionName("mcAutoTranslations.translationFunctionResolved", + extension.translationFunctionResolved) + ), + SubpluginOption(BuildConfig.PLUGIN_OPTION_TRANSLATE_FUNCTION, + validateFunctionName("mcAutoTranslations.translationFunction", + extension.translationFunction))) } } diff --git a/gradle-plugin/src/main/kotlin/moe/nea/mcautotranslations/gradle/utils.kt b/gradle-plugin/src/main/kotlin/moe/nea/mcautotranslations/gradle/utils.kt new file mode 100644 index 0000000..c0218af --- /dev/null +++ b/gradle-plugin/src/main/kotlin/moe/nea/mcautotranslations/gradle/utils.kt @@ -0,0 +1,15 @@ +package moe.nea.mcautotranslations.gradle + +import org.gradle.api.provider.Property + +internal fun validateFunctionName(origin: String, property: Property<String>): Lazy<String> = lazy { + property.finalizeValueOnRead() + require(property.isPresent) { + "Function property $origin has not been set yet." + } + val name = property.get() + require(name.matches(Regex("^([a-z0-9_]+\\.)+[a-z0-9_A-Z]+$"))) { + "'$name' is not a valid function name. Make sure to set $origin to a valid value." + } + name +} 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 31088cf..64286d0 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 @@ -5,9 +5,12 @@ package moe.nea.mcautotranslations.kotlin import com.google.auto.service.AutoService import moe.nea.mcautotranslation.`kotlin-plugin`.BuildConfig import org.jetbrains.kotlin.compiler.plugin.AbstractCliOption +import org.jetbrains.kotlin.compiler.plugin.CliOption 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.name.FqName @AutoService(CommandLineProcessor::class) @@ -15,9 +18,29 @@ class MCAutoTranslationsCommandLineProcessor : CommandLineProcessor { override val pluginId: String get() = BuildConfig.KOTLIN_PLUGIN_ID override val pluginOptions: Collection<AbstractCliOption> - get() = listOf() + get() = listOf(resolvedFunctionOption, translateFunctionOption) + + companion object { + val resolvedFunctionOption = + CliOption(BuildConfig.PLUGIN_OPTION_RESOLVED_FUNCTION, + "fully qualified function name", + "Set the new replaced translate function.") + val translateFunctionOption = + CliOption(BuildConfig.PLUGIN_OPTION_TRANSLATE_FUNCTION, + "fully qualified function name", + "Set the original translate function that will be replaced with the a resolved function.") + val translateFunction = CompilerConfigurationKey<FqName>(BuildConfig.PLUGIN_OPTION_TRANSLATE_FUNCTION) + val resolvedFunction = CompilerConfigurationKey<FqName>(BuildConfig.PLUGIN_OPTION_RESOLVED_FUNCTION) + } override fun processOption(option: AbstractCliOption, value: String, configuration: CompilerConfiguration) { - // TODO: process options + when (option.optionName) { + resolvedFunctionOption.optionName -> + configuration.put(translateFunction, FqName(value)) + + translateFunctionOption.optionName -> + configuration.put(resolvedFunction, 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/MCAutoTranslationsComponentRegistrar.kt b/kotlin-plugin/src/main/kotlin/moe/nea/mcautotranslations/kotlin/MCAutoTranslationsComponentRegistrar.kt index e1e22dd..02e524a 100644 --- a/kotlin-plugin/src/main/kotlin/moe/nea/mcautotranslations/kotlin/MCAutoTranslationsComponentRegistrar.kt +++ b/kotlin-plugin/src/main/kotlin/moe/nea/mcautotranslations/kotlin/MCAutoTranslationsComponentRegistrar.kt @@ -4,13 +4,11 @@ package moe.nea.mcautotranslations.kotlin import com.google.auto.service.AutoService import org.jetbrains.kotlin.backend.common.extensions.IrGenerationExtension -import org.jetbrains.kotlin.cli.common.messages.CompilerMessageSeverity import org.jetbrains.kotlin.compiler.plugin.CompilerPluginRegistrar import org.jetbrains.kotlin.compiler.plugin.ExperimentalCompilerApi import org.jetbrains.kotlin.config.CompilerConfiguration import org.jetbrains.kotlin.config.messageCollector import org.jetbrains.kotlin.extensions.ProjectExtensionDescriptor -import kotlin.collections.getOrPut @AutoService(CompilerPluginRegistrar::class) class MCAutoTranslationsComponentRegistrar : CompilerPluginRegistrar() { @@ -20,9 +18,11 @@ class MCAutoTranslationsComponentRegistrar : CompilerPluginRegistrar() { override fun ExtensionStorage.registerExtensions(configuration: CompilerConfiguration) { val messageCollector = configuration.messageCollector IrGenerationExtension.registerExtension( - extension = MCAutoTranslationsIrGenerationExtension(messageCollector)) - - messageCollector.report(CompilerMessageSeverity.INFO, "Registering stuff") + extension = MCAutoTranslationsIrGenerationExtension( + messageCollector, + configuration.get(MCAutoTranslationsCommandLineProcessor.translateFunction)!!, + configuration.get(MCAutoTranslationsCommandLineProcessor.resolvedFunction)!!, + )) } fun <T : Any> ExtensionStorage.registerExtensionFirst( diff --git a/kotlin-plugin/src/main/kotlin/moe/nea/mcautotranslations/kotlin/MCAutoTranslationsIrGenerationExtension.kt b/kotlin-plugin/src/main/kotlin/moe/nea/mcautotranslations/kotlin/MCAutoTranslationsIrGenerationExtension.kt index 3b17e68..3fd9224 100644 --- a/kotlin-plugin/src/main/kotlin/moe/nea/mcautotranslations/kotlin/MCAutoTranslationsIrGenerationExtension.kt +++ b/kotlin-plugin/src/main/kotlin/moe/nea/mcautotranslations/kotlin/MCAutoTranslationsIrGenerationExtension.kt @@ -9,12 +9,12 @@ import org.jetbrains.kotlin.name.FqName class MCAutoTranslationsIrGenerationExtension( private val messageCollector: MessageCollector, + private val replace: FqName, + private val resolved: FqName, ) : IrGenerationExtension { override fun generate(moduleFragment: IrModuleFragment, pluginContext: IrPluginContext) { val translationNames: MutableMap<FqName, CallableId> = mutableMapOf() - val target = FqName("moe.nea.translatetest.tr") - val resolved = FqName("moe.nea.translatetest.trResolved") // TODO: make these names configurable - translationNames[target] = CallableId(resolved.parent(), resolved.shortName()) + translationNames[replace] = CallableId(resolved.parent(), resolved.shortName()) moduleFragment.files.forEach { val visitor = MCAutoTranslationsCallTransformerAndCollector( it, |