Carter Hudson
10/08/2024, 8:28 PMmbonnin
10/08/2024, 8:42 PMCarter Hudson
10/08/2024, 8:43 PMKClass
instancesCarter Hudson
10/08/2024, 8:43 PMmbonnin
10/08/2024, 8:43 PMmbonnin
10/08/2024, 8:44 PMCarter Hudson
10/08/2024, 8:45 PMList<KClass<*>>
in a file in each module and combining them manuallyCarter Hudson
10/08/2024, 8:46 PMmbonnin
10/08/2024, 8:46 PMpackage module1
@MyAnnotation
val classes = listOf(Foo1::class, ...)
module2:
package module2
@MyAnnotation
val classes = listOf(Foo2::class, ...)
aggregatingModule processor:
getSymbolsWithAnnotation(@MyAnnotation)
Carter Hudson
10/08/2024, 8:46 PMmbonnin
10/08/2024, 8:46 PMCarter Hudson
10/08/2024, 8:46 PMmbonnin
10/08/2024, 8:47 PMmbonnin
10/08/2024, 8:49 PMcodeGenerator.createNewFile(
Dependencies.ALL_FILES, // or proper dependency tracking
"",
"META-INF/myprocessor/classesList.json",
""
)
Carter Hudson
10/08/2024, 8:50 PMmbonnin
10/08/2024, 8:51 PMmbonnin
10/08/2024, 8:51 PMmbonnin
10/08/2024, 8:52 PMmbonnin
10/08/2024, 8:54 PMgetSymbolsWithAnnotation(@MyAnnotation)
route you'll have to use KSP APIs to read Foo1
, Foo2
, etc.. from the Kotlin AST.mbonnin
10/08/2024, 8:55 PMFoo1
, Foo2
, etc... is inside the initializer and I don't think KSP gives you access to thismbonnin
10/08/2024, 8:55 PMmbonnin
10/08/2024, 8:57 PMpackage module1
@MyAnnotation(Foo1::class, Foo2::class, ...) // This is constant
val classes = listOf(Foo1::class, ...) // This is not
mbonnin
10/08/2024, 8:57 PMCarter Hudson
10/08/2024, 8:58 PMmbonnin
10/08/2024, 8:58 PMmbonnin
10/08/2024, 9:00 PMinDepth
parameter 👀
fun getSymbolsWithAnnotation(annotationName: String, inDepth: Boolean = false): Sequence<KSAnnotated>
Carter Hudson
10/08/2024, 9:01 PMCarter Hudson
10/08/2024, 9:03 PMCarter Hudson
10/08/2024, 9:06 PMCarter Hudson
10/08/2024, 9:06 PMCarter Hudson
10/08/2024, 9:08 PMDavid Rawson
10/08/2024, 9:09 PMresolver.getDeclarationsFromPackage(packageName: String)
API which gets from dependencies and source.Carter Hudson
10/08/2024, 9:09 PMDavid Rawson
10/08/2024, 9:09 PMCarter Hudson
10/08/2024, 9:09 PMmbonnin
10/08/2024, 9:15 PMThere's a promisingCan confirmparameterinDepth
inDepth
doesn't look in dependenciesmbonnin
10/08/2024, 9:25 PMmbonnin
10/08/2024, 9:25 PMgetDeclarationsFromPackage
it is 👍 tilCarter Hudson
10/08/2024, 9:27 PMCarter Hudson
10/08/2024, 11:24 PMgetDeclarationsFromPackage
will work for my scenario. It requires the exact package name, and these classes that I’m getting declarations for have no fixed package :(Carter Hudson
10/08/2024, 11:25 PMDavid Rawson
10/09/2024, 12:31 AMgetSymbolsWithAnnotation
will work for the processor that runs on the individual modules right? This processor could write Kotlin files for the individual modules and set the generated code's package name to something fixed for your entire project. Then the processor that runs on the top-level module can use getDeclarationsFromPackage
to pick up the outputs with the fixed package name.David Rawson
10/09/2024, 1:50 AMmbonnin
10/09/2024, 7:47 AMmbonnin
10/09/2024, 7:47 AMCarter Hudson
10/09/2024, 3:27 PMCarter Hudson
10/09/2024, 5:31 PMksp(project(:finalround))
in that main module? I don’t want each module to trigger the aggregation. This might be a stupid question, but does it need to be a separate processor?mbonnin
10/09/2024, 5:33 PMmbonnin
10/09/2024, 5:33 PMCarter Hudson
10/09/2024, 5:35 PMCarter Hudson
10/09/2024, 5:37 PMCarter Hudson
10/09/2024, 7:28 PMresolver.getAllFiles()
with ksp.incremental=false
, but by the last invocation of the processor, resolver.getAllFiles()
doesn’t contain my generated files, and it tries to start from the beginning. Probably something dumb on my end.Carter Hudson
10/09/2024, 7:31 PMCarter Hudson
10/09/2024, 7:33 PMCarter Hudson
10/10/2024, 7:56 PMZac Sweers
10/10/2024, 9:13 PMZac Sweers
10/10/2024, 9:13 PMCarter Hudson
10/10/2024, 9:52 PMresolver.getDeclarationsFromPackage
, where the package is the same for all modules that generate code. I’m using resolver.getNewFiles
to see if any new files were created, and if they were, I defer the symbols I obtained from getDeclerationsFromPackage
, but no new rounds are triggered for me to attempt aggregation again.Carter Hudson
10/11/2024, 7:22 PMmbonnin
10/11/2024, 10:24 PMCarter Hudson
10/12/2024, 9:37 PMmustRunAfter
or dependsOn
. If there's another mechanism to order KSP tasks, I'm not aware of it.mbonnin
10/12/2024, 10:44 PM// aggregate/build.gradle
// Depend on other module. They will be built before agggregate
dependencies {
implementation(project(":module1"))
implementation(project(":module2"))
}
mbonnin
10/12/2024, 10:45 PMCarter Hudson
10/16/2024, 7:54 PMCarter Hudson
10/16/2024, 7:56 PMCarter Hudson
10/16/2024, 8:01 PMmbonnin
10/16/2024, 8:28 PMCarter Hudson
10/17/2024, 8:49 PMZac Sweers
10/17/2024, 9:11 PMCarter Hudson
10/17/2024, 9:12 PMCarter Hudson
10/17/2024, 9:14 PMksp.incremental.intermodule=true
, but it didn’t change anything with regard to the final output.Carter Hudson
10/23/2024, 1:08 AMCarter Hudson
10/23/2024, 5:18 PMoriginatingKSFiles
in kotlin poet’s writeTo
was taking care of the source files for me somehow, but I see that it’s just aggregating from the `FileSpec`s members, and I still need to specify the files explicitly. 🤦♂️
Live and learn, I guess 🙂 Thanks for the help!
This was a helpful hint: https://github.com/google/ksp/issues/1313