https://kotlinlang.org logo
#gradle
Title
y

yschimke

06/02/2023, 3:44 PM
I seem to remember a custom gradle hook for IDEs to run for code generation. My situation is using grpc, if a branch has changes to the proto definitions, I need the grpc generation to run whenever I switch branches. Obviously it works when you run, but would be nice not to stare at red lines until the first build.
v

Vampire

06/02/2023, 3:59 PM
Not aware what you could mean. But you could define a lifecycle task called
generate
in your build that depends on all code generation tasks and then configure your IDE to run that task before or after sync. You can also configure this automatic task-running using the
idea-ext
plugin if your IDE is IntelliJ.
Actually, if the code generation tasks are properly registered as `srcDir`s, the IDE sync could maybe trigger the getting of the source files and thus cause the generating tasks to be executed. But I'm not aware that either IDE is doing that currently.
t

Trevor Stone

06/02/2023, 4:52 PM
Copy code
// This triggers code-gen immediate on gradle sync
afterEvaluate {
    tasks
        .maybeCreate("prepareKotlinIdeaImport")
        .dependsOn(tasks.getByPath(":${project.name}:kspKotlin"))
}
This is what I use for jvm KSP
y

yschimke

06/02/2023, 4:57 PM
Thanks @Trevor Stone, that's the one. Think I saw this tweet https://twitter.com/Sellmair/status/1619308362881187840?t=ZmXcptNjl5b2OfLCNEePmg&s=19
t

Trevor Stone

06/02/2023, 4:58 PM
Yep, that's where I learned about it. I couldn't find the initial slack reference for it. Glad you found the tweet