Skip to content

Commit

Permalink
[Kword] Rewrite plugin in kotlin (#224)
Browse files Browse the repository at this point in the history
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
npresseault authored Aug 14, 2024
1 parent c089f9d commit 0ee2688
Show file tree
Hide file tree
Showing 15 changed files with 204 additions and 186 deletions.
19 changes: 17 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,24 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).

### Breaking Changes

### Updates
- [kword] Gradle plugin DSL changed. All properties are now lazy.
Instead of

### Breaking Changes
```kotlin
translationFile = file("src/commonMain/resources/translations/translation.en.json")
enumClassName = "com.mirego.sample.KWordTranslation"
generatedDir = file("src/commonMain/generated")
```

Do this

```kotlin
translationFile.set(file("src/commonMain/resources/translations/translation.en.json"))
enumClassName.set("com.mirego.sample.KWordTranslation")
generatedDir.set(file("src/commonMain/generated"))
```

### Updates

## 5.4.0

Expand Down
1 change: 0 additions & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import org.jetbrains.kotlin.gradle.tasks.KotlinCompilationTask
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile

val samplesEnabled = !extra.has("disable_samples")

Expand Down
7 changes: 4 additions & 3 deletions buildSrc/src/main/kotlin/Versions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,13 @@ object Versions {
const val ANDROIDX_LIFECYCLE = "2.8.2"
const val OKIO = "3.9.0"
const val KSP = "2.0.0-1.0.21"
const val KOTLIN_POET = "1.17.0"
const val KOTLIN_POET = "1.18.1"
const val ACCOMPANIST = "0.34.0"
const val COMPILE_TESTING_KSP = "1.6.0"

object Android {
const val TARGET_SDK = 34
const val COMPILE_SDK = 34
const val TARGET_SDK = 35
const val COMPILE_SDK = 35
const val MIN_SDK = 21
}
const val JVM_TOOLCHAIN = 17
Expand Down
28 changes: 28 additions & 0 deletions trikot-kword/kword-plugin/api/kword-plugin.api
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
}

9 changes: 1 addition & 8 deletions trikot-kword/kword-plugin/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,17 +1,10 @@
plugins {
id("groovy")
kotlin("jvm")
id("mirego.publish")
}

java {
toolchain {
languageVersion.set(JavaLanguageVersion.of(17))
}
}

dependencies {
implementation(gradleApi())
implementation(localGroovy())
implementation("com.squareup:kotlinpoet:${Versions.KOTLIN_POET}")
}

Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

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)
}
}
}
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
}
Loading

0 comments on commit 0ee2688

Please sign in to comment.