Is it possible to leverage ksp to generate non kot...
# ksp
p
Is it possible to leverage ksp to generate non kotlin code? I'd like to leverage the symbol processing to generate swift files
b
Can't see why not. Just write them via jvm File api
p
But that breaks incremental things right? How would I know which files to delete and which ones to keep?
b
Oh you mean you want to fully replace some kotlin files with swift?
p
No of cause not 😛 But right now when I generate a file based on a kotlin file and compile, the generated file gets created. When I now delete the originating file and compile, the generated file will also be deleted. But that step wouldn't happen when I just use the file api so dead generated files will be left I guess?
b
Just write a file to different dir not controlled by ksp. You can pass on the path via ksp gradle options
p
But how would I know when to delete the file?
b
It won't. That's for you to manage either manually or with a gradle task
p
But how could I possibly know that?
b
Keep your own cache during generation maybe? Could be as simple as storing a json map of kt sources ang generated swift files. Then at the end of each ksp task compare the new such map with previous one and clean up
In short, generate map during ksp, compare new map with cached one in your custom gradle task (or doLast {} block in ksp task), cleanip and move new map to cache
p
But what would be in that map?
On incremental rounds, the ksp allFiles() will only contain the changed ones
b
Copy code
{
  "path/to/source.kt": "path/to/geterated.swift"
}
☝️ 1
e
I used KSP the other day to generate plain java sources. The previous implementation was using Apache Velocity, Kotlin proved extremely nice
g
Why not just using CodeGenerator.createNewFile(Dependencies(false, false, listOf(“source.kt")), .., extensionName = "swift") ?
p
Because that would put it in the kotlin sourceset?
g
Mmmh, from memory it goes into a resource directory
Not finding the documentation right now, but an extension ".kt" goes into /kotlin, ".java" into /java and anything else I suppose will go into /resources
p
Uh that might work!
e
open an issue, they are usually quite prompt to reply
p
Will do 🙂
e
anyway, cool experiments Paul, keep us updated
g
Is it because the kotlin directory is removed (not normal but known bug AFAIK) and you still have an explicit reference in your build.gradle on this directory?
p
But what removes it?
g
I was thinking about this feedback, I experienced the same behaviour myself https://kotlinlang.slack.com/archives/C013BA8EQSE/p1635931295085600
p
@elect
anyway, cool experiments Paul, keep us updated
Keeping you updated 😉 https://medium.com/@woitaschek/kotlin-native-using-swift-not-objective-c-d7742c040539
👏 2
g
Thanks ! Do you plan to open-source your tool? (I'm working on similar stuff from JS myself, and could be interested by this one too.)
p
I don't believe that it would work on a one-library-fits-all level. It has many things specific to our language and also some exceptions for non generated code integration. Also this only works because all our data structures are immutable. If you would generate structs from mutable structures that would probably lead to unexpected behavior.
g
Exactly the same problem for JS, that's why I'm providing wrapper by defaults, so those forward everything (binding 2 ways, changing types in the middle)...