MrPowerGamerBR
02/25/2022, 3:14 PM.yml
files for localization files, if there is a key commands.hello: "Hello {user}!"
, a class + function is generated named I18nKeysData.Commands.Hello(name: Any)
Currently what I'm doing is this
// HACKY WORKAROUND!!!
// This makes the generateI18nKeys task to always be ran before the compileKotlin step
// We need to do this (instead of using withType) because, for some reason, it doesn't work and the task isn't executed.
//
// We need to keep it within the "afterEvaluate" block because, if it isn't, the compile*InsertStuffHere* tasks won't exist!
// <https://stackoverflow.com/a/58763804/7271796>
project.afterEvaluate {
project.tasks.filter { it.name.startsWith("compileKotlin") }.forEach {
it.dependsOn(task)
}
}
But I think it is very hacky and it broke in Kotlin 1.6.20-M1 because they added a new compileCommonMainKotlinMetadata
task, so I wonder if there is a better solution for thismbonnin
02/25/2022, 3:18 PMsrcDir
:
(project.extensions.findByName("kotlin") as? KotlinProjectExtension).sourceSets.getByName("main").kotlin.srcDir(outputDir)
mbonnin
02/25/2022, 3:19 PMoutputDir
is a Gradle Property
from another task, it will setup the task dependency automaticallymbonnin
02/25/2022, 3:20 PMflatMap
:
val outputDir = codegenTaskProvider.flatMap { it.outputDir }
mbonnin
02/25/2022, 3:20 PMVampire
02/25/2022, 3:20 PMsrcDir
if that is the only output of it.MrPowerGamerBR
02/25/2022, 3:22 PMVampire
02/25/2022, 3:26 PMafterEvaluate
(no, it is not necessary, whatever SO says, you just need a proper lazy construct instead of looking what tasks are available right now)
β’ Don't iterate through all tasks, or all have to be realized, totally destroying task configuration avoidance
β’ Don't match the tasks by name when you actually want to match by type
β’ Don't generate into the checked in sources directory, and add a task dependency but instead properly register your generation task as Martin showed
πVampire
02/25/2022, 3:28 PMtasks.withType<KotlinCompile>().configureEach { dependsOn(task) }
instead of the whole snippet, or if name is actually important too, then
tasks.withType<KotlinCompile>().matching { name.startsWith("compileKotlin") }.configureEach { dependsOn(task) }
(still using withType
to not destroy task configuration avoidance completely)Vampire
02/25/2022, 3:34 PMkotlin {
sourceSets {
main {
kotlin.srcDir(generatorTask)
}
}
}
or this:
kotlin.sourceSets.main.get().kotlin.srcDir(generatorTask)
mbonnin
02/25/2022, 3:35 PMplugin{}
blockVampire
02/25/2022, 3:36 PMMrPowerGamerBR
02/26/2022, 1:16 AMgenerateI18nKeys
task is ran before transformCommonMainDependenciesMetadata
and compileCommonMainKotlinMetadata
! And now my project compiles without any compilation errors when using Kotlin 1.6.20-M1 :D
I ended up going with the kotlin { sourceSets {} }
version because I was already doing that before, but I was "hardcoding" the path (kotlin.srcDir("build/generated/languages")
) instead of referencing the property (kotlin.srcDir(generateI18nKeys.get().languageTargetFolder)
)
Thanks for helping @Vampire @mbonnin :)Vampire
02/26/2022, 1:24 AMdependsOn
, thanks actually a code smell. πMrPowerGamerBR
02/26/2022, 1:55 AMsrcDir
to be just generateI18nKeys
and it still works, thanks!