-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Kword] Rewrite plugin in kotlin (#224)
Rewrite kword plugin in kotlin. The gradle DSL also gets an update. Gradle plugin DSL changed. All properties are now lazy. Instead of translationFile = file("src/commonMain/resources/translations/translation.en.json") enumClassName = "com.mirego.sample.KWordTranslation" generatedDir = file("src/commonMain/generated") Do this translationFile.set(file("src/commonMain/resources/translations/translation.en.json")) enumClassName.set("com.mirego.sample.KWordTranslation") generatedDir.set(file("src/commonMain/generated")) Using kotlin will make it easier to maintain it. Aditionaly, we were using non lazy properties gradle which could result in non optimal gradle builds.
- Loading branch information
1 parent
c089f9d
commit 0ee2688
Showing
15 changed files
with
204 additions
and
186 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
public abstract class com/mirego/kword/KWordEnumGenerate : org/gradle/api/DefaultTask { | ||
public static final field Companion Lcom/mirego/kword/KWordEnumGenerate$Companion; | ||
public fun <init> ()V | ||
public final fun generate ()V | ||
public abstract fun getEnumClassName ()Lorg/gradle/api/provider/Property; | ||
public final fun getGeneratedClassFile ()Lorg/gradle/api/provider/Provider; | ||
public abstract fun getGeneratedDir ()Lorg/gradle/api/file/DirectoryProperty; | ||
public abstract fun getTranslationFiles ()Lorg/gradle/api/file/ConfigurableFileCollection; | ||
} | ||
|
||
public final class com/mirego/kword/KWordEnumGenerate$Companion { | ||
public final fun underscoreKey (Ljava/lang/String;)Ljava/lang/String; | ||
} | ||
|
||
public abstract class com/mirego/kword/KWordExtension { | ||
public fun <init> ()V | ||
public abstract fun getEnumClassName ()Lorg/gradle/api/provider/Property; | ||
public abstract fun getGeneratedDir ()Lorg/gradle/api/file/DirectoryProperty; | ||
public abstract fun getTranslationFile ()Lorg/gradle/api/file/RegularFileProperty; | ||
public abstract fun getTranslationFiles ()Lorg/gradle/api/file/ConfigurableFileCollection; | ||
} | ||
|
||
public final class com/mirego/kword/KWordPlugin : org/gradle/api/Plugin { | ||
public fun <init> ()V | ||
public synthetic fun apply (Ljava/lang/Object;)V | ||
public fun apply (Lorg/gradle/api/Project;)V | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
88 changes: 0 additions & 88 deletions
88
trikot-kword/kword-plugin/src/main/groovy/com/mirego/kword/KWordEnumGenerate.groovy
This file was deleted.
Oops, something went wrong.
54 changes: 0 additions & 54 deletions
54
trikot-kword/kword-plugin/src/main/groovy/com/mirego/kword/KWordExtension.groovy
This file was deleted.
Oops, something went wrong.
17 changes: 0 additions & 17 deletions
17
trikot-kword/kword-plugin/src/main/groovy/com/mirego/kword/KWordPlugin.groovy
This file was deleted.
Oops, something went wrong.
98 changes: 98 additions & 0 deletions
98
trikot-kword/kword-plugin/src/main/kotlin/com.mirego.kword/KWordEnumGenerate.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
package com.mirego.kword | ||
|
||
import com.squareup.kotlinpoet.ClassName | ||
import com.squareup.kotlinpoet.FileSpec | ||
import com.squareup.kotlinpoet.FunSpec | ||
import com.squareup.kotlinpoet.KModifier | ||
import com.squareup.kotlinpoet.PropertySpec | ||
import com.squareup.kotlinpoet.TypeSpec | ||
import groovy.json.JsonSlurper | ||
import org.gradle.api.DefaultTask | ||
import org.gradle.api.file.ConfigurableFileCollection | ||
import org.gradle.api.file.DirectoryProperty | ||
import org.gradle.api.file.RegularFile | ||
import org.gradle.api.provider.Property | ||
import org.gradle.api.provider.Provider | ||
import org.gradle.api.tasks.Input | ||
import org.gradle.api.tasks.InputDirectory | ||
import org.gradle.api.tasks.InputFiles | ||
import org.gradle.api.tasks.OutputFile | ||
import org.gradle.api.tasks.TaskAction | ||
import java.util.Locale | ||
|
||
abstract class KWordEnumGenerate : DefaultTask() { | ||
|
||
@get:InputFiles | ||
abstract val translationFiles: ConfigurableFileCollection | ||
|
||
@get:Input | ||
abstract val enumClassName: Property<String> | ||
|
||
@get:InputDirectory | ||
abstract val generatedDir: DirectoryProperty | ||
|
||
@get:OutputFile | ||
val generatedClassFile: Provider<RegularFile> get() = | ||
generatedDir.file(enumClassName.map { it.replace(".", "/") + ".kt" }) | ||
|
||
@TaskAction | ||
fun generate() { | ||
logger.warn("Generating KWord enum for ${enumClassName.get()}") | ||
logger.warn("Translation files: ${translationFiles.files}") | ||
val generatedClassName = ClassName.bestGuess(enumClassName.get()) | ||
val enumBuilder = enumBuilder(generatedClassName) | ||
addEnumConstants(enumBuilder) | ||
writeFile(generatedClassName, enumBuilder) | ||
} | ||
|
||
private fun enumBuilder(generatedClassName: ClassName): TypeSpec.Builder { | ||
return TypeSpec.enumBuilder(generatedClassName) | ||
.primaryConstructor( | ||
FunSpec.constructorBuilder() | ||
.addParameter("translationKey", String::class) | ||
.build() | ||
) | ||
.addProperty( | ||
PropertySpec.builder("translationKey", String::class, KModifier.OVERRIDE) | ||
.initializer("translationKey") | ||
.build() | ||
) | ||
.addSuperinterface(ClassName.bestGuess("com.mirego.trikot.kword.KWordKey")) | ||
} | ||
|
||
private fun addEnumConstants(enumBuilder: TypeSpec.Builder) { | ||
parseKeys() | ||
.map { key -> underscoreKey(key) to key } | ||
.forEach { | ||
enumBuilder.addEnumConstant( | ||
it.first, | ||
TypeSpec.anonymousClassBuilder() | ||
.addSuperclassConstructorParameter("%S", it.second) | ||
.build() | ||
) | ||
} | ||
} | ||
|
||
private fun writeFile(generatedClassName: ClassName, enumBuilder: TypeSpec.Builder) { | ||
FileSpec.builder(generatedClassName.packageName, generatedClassName.simpleName) | ||
.indent(" ") | ||
.addType(enumBuilder.build()) | ||
.build() | ||
.writeTo(generatedDir.get().asFile) | ||
} | ||
|
||
private fun parseKeys(): List<String> { | ||
val keys = mutableSetOf<String>() | ||
translationFiles.forEach { | ||
val translations = JsonSlurper().parse(it) as Map<String, Any> | ||
keys.addAll(translations.keys) | ||
} | ||
return keys.toList() | ||
} | ||
|
||
companion object { | ||
fun underscoreKey(key: String): String { | ||
return key.replace(Regex("([A-Z])"), "_$1").replace(".", "_").uppercase(Locale.ENGLISH) | ||
} | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
trikot-kword/kword-plugin/src/main/kotlin/com.mirego.kword/KWordExtension.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package com.mirego.kword | ||
|
||
import org.gradle.api.file.ConfigurableFileCollection | ||
import org.gradle.api.file.DirectoryProperty | ||
import org.gradle.api.file.RegularFileProperty | ||
import org.gradle.api.provider.Property | ||
|
||
abstract class KWordExtension { | ||
abstract val translationFile: RegularFileProperty | ||
|
||
abstract val translationFiles: ConfigurableFileCollection | ||
|
||
abstract val enumClassName: Property<String> | ||
|
||
abstract val generatedDir: DirectoryProperty | ||
} |
Oops, something went wrong.