Marc Knaup
02/12/2021, 2:08 PMval umbrella = <svg xmlns="<http://www.w3.org/2000/svg>" viewBox="0 0 40 40"><path d="M21.172,2.4V0H18.828V2.4C8.063,2.933,0,10.857,0,21.172V23.06l1.694-.839a11.586,11.586,0,0,1,4.243-1.049c1.512,0,3.3,1.445,3.86,2l.829.819.826-.819c.561-.556,2.348-2,3.86-2a6.366,6.366,0,0,1,3.516,1.689V36.484a3.516,3.516,0,0,0,7.031,0V34.141H23.516v2.344a1.172,1.172,0,1,1-2.344,0V22.861a6.366,6.366,0,0,1,3.516-1.689c1.512,0,3.3,1.445,3.86,2l.829.819.826-.819c.561-.556,2.426-2,3.938-2a10.963,10.963,0,0,1,4.165,1.049l1.695.84V21.172C40,10.857,31.937,2.933,21.172,2.4Z"/></svg>
That could be converted into kotlinx-html
or React code.dmitriy.novozhilov
02/12/2021, 3:00 PMMarc Knaup
02/12/2021, 3:04 PMdmitriy.novozhilov
02/12/2021, 3:12 PM.xml
file, but it is actually raw xml inside kotlin code. What is your usecase for such feature? If it is kotlin code, why don't write proper code instead of xml?Marc Knaup
02/12/2021, 3:15 PMkt
files based on svg
files located in a Kotlin source directory, true 🤔
Which leads me to another question:
Can I create actual Kotlin source files with a compiler plugin that become part of the same build? Similar to what KAPT would do.edrd
02/12/2021, 3:19 PMedrd
02/12/2021, 3:21 PMedrd
02/12/2021, 3:22 PMMarc Knaup
02/12/2021, 3:22 PMdmitriy.novozhilov
02/12/2021, 3:22 PMa
2. You create module a.generator
(or something) which contains your svg
files and code which converts that files to .kt
files with source code inside a
module
3. Module a.generator
contains some :generate
task which runs this generation
4. You configure your build so a:build
task depends on :a.generator:generate
This is very simple solution which works well if your .svg
files are not changed too much oftendmitriy.novozhilov
02/12/2021, 3:23 PMMarc Knaup
02/12/2021, 3:24 PMMarc Knaup
02/12/2021, 3:25 PM.svg
files into your source set
2. add compiler plugin
3. your build automatically contains generated code (just like with KAPT) for these SVG filesMarc Knaup
02/12/2021, 3:25 PMKSerializer
for example.dmitriy.novozhilov
02/12/2021, 3:27 PMbuild.gradle.kts
for all modules which need such thingdmitriy.novozhilov
02/12/2021, 3:28 PMMarc Knaup
02/12/2021, 3:29 PMdmitriy.novozhilov
02/12/2021, 3:32 PMWell the generator only depends on SVG files so it would/could always run before any KT file is even parsed - even if it’s a compiler plugin.Basically yes, you can implement generator using only stdlib (it's just sometimes useful to have some types available in generator to simplify it's code)
I actually only need the code-generating part of the compilerWhat is code generation part? You can just produces some
.kt
sources and give them to compiler, so it make all dirty jobMarc Knaup
02/12/2021, 3:35 PMkotlin-react
as an example.
1. Parse SVG file.
2. Generate kotlin-react code for that SVG file, e.g . val fooIcon = reactElement { svg { attrs["viewport"] = "…"; etc } }
3. Compiler picks up these files when building.
Now it’s basically an SVG -> KT transformation before the actual Kotlin build.
I just need a way to create Kotlin code. I could use kotlinpoet but if the Kotlin compiler has a good API for plugins to construct Kotlin code then that would be preferred.dmitriy.novozhilov
02/12/2021, 3:40 PMkotlin-poet
is very nice as I know. Also arrow-meta
provides some tools for source generation.
In compiler team we are usually implement new ad-hock solutions, because it may be faster to quickly write something for specific generation problem instead of using kotlin-poet
which covers all the cases, but requires some time to get into itMarc Knaup
02/12/2021, 3:44 PMdmitriy.novozhilov
02/12/2021, 3:45 PMDo you happen to know how the J2K converter generates Kotlin code?Not well, I just know that it's a java PSI to kotlin PSI conversion You can ivestigate it's code if you want: https://github.com/JetBrains/kotlin/blob/master/nj2k/src/org/jetbrains/kotlin/nj2k/NewJavaToKotlinConverter.kt
Marc Knaup
02/12/2021, 3:50 PMdmitriy.novozhilov
02/12/2021, 3:52 PMMarc Knaup
02/12/2021, 3:53 PMHanno
02/12/2021, 9:48 PMjimn
02/23/2021, 2:58 AMMarc Knaup
02/23/2021, 9:59 AM