https://kotlinlang.org logo
Title
p

Paul Woitaschek

11/11/2021, 8:28 AM
Is it possible to leverage ksp to generate non kotlin code? I'd like to leverage the symbol processing to generate swift files
b

Big Chungus

11/11/2021, 9:16 AM
Can't see why not. Just write them via jvm File api
p

Paul Woitaschek

11/11/2021, 9:16 AM
But that breaks incremental things right? How would I know which files to delete and which ones to keep?
b

Big Chungus

11/11/2021, 9:17 AM
Oh you mean you want to fully replace some kotlin files with swift?
p

Paul Woitaschek

11/11/2021, 9:19 AM
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

Big Chungus

11/11/2021, 9:21 AM
Just write a file to different dir not controlled by ksp. You can pass on the path via ksp gradle options
p

Paul Woitaschek

11/11/2021, 9:22 AM
But how would I know when to delete the file?
b

Big Chungus

11/11/2021, 9:22 AM
It won't. That's for you to manage either manually or with a gradle task
p

Paul Woitaschek

11/11/2021, 9:23 AM
But how could I possibly know that?
b

Big Chungus

11/11/2021, 9:24 AM
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

Paul Woitaschek

11/11/2021, 9:29 AM
But what would be in that map?
On incremental rounds, the ksp allFiles() will only contain the changed ones
b

Big Chungus

11/11/2021, 9:30 AM
{
  "path/to/source.kt": "path/to/geterated.swift"
}
☝️ 1
e

elect

11/11/2021, 10:10 AM
I used KSP the other day to generate plain java sources. The previous implementation was using Apache Velocity, Kotlin proved extremely nice
g

Grégory Lureau

11/11/2021, 1:23 PM
Why not just using CodeGenerator.createNewFile(Dependencies(false, false, listOf(“source.kt")), .., extensionName = "swift") ?
p

Paul Woitaschek

11/11/2021, 1:25 PM
Because that would put it in the kotlin sourceset?
g

Grégory Lureau

11/11/2021, 1:25 PM
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

Paul Woitaschek

11/11/2021, 1:37 PM
Uh that might work!
It half works. It creates the files but then ksp crashes with:
> Task :lifecycle:kspKotlinMetadata FAILED
e: Source file or directory not found: /home/paul/repos/yazio/shared/lifecycle/build/generated/ksp/commonMain/kotlin
This is happening when only swift files are generated but no kotlin files for the module
e

elect

11/11/2021, 4:11 PM
open an issue, they are usually quite prompt to reply
p

Paul Woitaschek

11/11/2021, 4:11 PM
Will do 🙂
e

elect

11/11/2021, 4:11 PM
anyway, cool experiments Paul, keep us updated
g

glureau

11/11/2021, 4:54 PM
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

Paul Woitaschek

11/11/2021, 5:20 PM
But what removes it?
g

Grégory Lureau

11/11/2021, 7:33 PM
I was thinking about this feedback, I experienced the same behaviour myself https://kotlinlang.slack.com/archives/C013BA8EQSE/p1635931295085600
p

Paul Woitaschek

11/19/2021, 7:34 AM
@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

Grégory Lureau

11/19/2021, 7:49 AM
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

Paul Woitaschek

11/19/2021, 7:51 AM
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

Grégory Lureau

11/19/2021, 8:01 AM
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)...