Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Speaker Deck
PRO
Sign in
Sign up for free
kompile-testing internal
@hotchemi
April 12, 2019
Programming
0
210
kompile-testing internal
At shibuya.apk
@hotchemi
April 12, 2019
Tweet
Share
More Decks by @hotchemi
See All by @hotchemi
The things we’ve learned from iOS×React Native hybrid development
hotchemi
2
4.2k
React Nativeを活用したアプリ開発体制/sapuri meetup
hotchemi
3
6.9k
Type-Safe i18n on RN
hotchemi
2
840
Navigation in a hybrid app
hotchemi
3
980
PermissionsDispatcher × Kotlin
hotchemi
0
1.9k
kotlin compiler plugin
hotchemi
1
520
Rx and Preferences
hotchemi
2
130
Introducing PermissionsDispatcher
hotchemi
1
110
khronos
hotchemi
4
1.6k
Other Decks in Programming
See All in Programming
iOS 16からのロック画面Widget争奪戦に備える
tsuzuki817
0
210
エンジニアによる事業指標計測のススメ
doyaaaaaken
1
180
io22 extended What's new in app performance
veronikapj
0
340
Jetpack Compose, 어디까지 알고 있을까?
jisungbin
0
110
Why Airflow? & What's new in Airflow 2.3?
kaxil
0
110
Branching out to Jetpack Compose
chrisbanes
4
1.2k
trocco® の品質を守る、とても普通な取り組み
kekekenta
0
350
Treasure.map(): Functional programming in JVM-based languages
paranoidmonoid
0
130
I/O Extended 2022 in Android ~ Whats new in Android development tools
pluu
0
540
GitHub Actions を導入した経緯
tamago3keran
1
430
Android スキルセットをフル活用して始めるスマートテレビアプリ開発
satsukies
1
190
#JJUG_CCC 「サポート」は製品開発? - JDBCライブラリ屋さんが実践する攻めのテクニカルサポートとJavaエンジニアのキャリアについて -
cdataj
0
420
Featured
See All Featured
Visualizing Your Data: Incorporating Mongo into Loggly Infrastructure
mongodb
29
4.3k
RailsConf & Balkan Ruby 2019: The Past, Present, and Future of Rails at GitHub
eileencodes
119
28k
How to train your dragon (web standard)
notwaldorf
58
3.9k
XXLCSS - How to scale CSS and keep your sanity
sugarenia
236
1M
Ruby is Unlike a Banana
tanoku
91
9.2k
Build your cross-platform service in a week with App Engine
jlugia
219
17k
How To Stay Up To Date on Web Technology
chriscoyier
780
250k
Six Lessons from altMBA
skipperchong
14
1.4k
Designing with Data
zakiwarfel
91
3.9k
Designing Dashboards & Data Visualisations in Web Apps
destraynor
224
49k
Reflections from 52 weeks, 52 projects
jeffersonlam
337
17k
Responsive Adventures: Dirty Tricks From The Dark Corners of Front-End
smashingmag
237
19k
Transcript
kompile-testing internal @hotchemi
• google/compile-testing • Testing tools for javac and apt •
Used in some code-gen libraries • Dagger2 • JavaPoet • Auto
Compilation compilation = javac() .withProcessors(new Processor()) .compile(JavaFileObjects.forResource("HelloWorld.java")); assertThat(compilation).succeeded(); assertThat(compilation) .generatedSourceFile("GeneratedHelloWorld")
.hasSourceEquivalentTo( JavaFileObjects.forResource("GeneratedHelloWorld.java"));
None
• kompile-testing • Simple API as compile-testing provides • Supports
kotlinc and kapt • Version: 0.1.2(alpha)
kotlinc() .withProcessors(Processor()) .addKotlin("input.kt", """ import kompile.testing.TestAnnotation @TestAnnotation class TestClass """.trimIndent())
.compile() .succeededWithoutWarnings() .generatedFile("generatedKtFile.kt") .hasSourceEquivalentTo(""" class GeneratedKtFile """.trimIndent())
Demo
Under the hood
Register processors - Write KClass info as a jar file
- For specifying kapt options Add Kotlin file - Just write contents to .kt file - Under source directory Run Kotlin compiler API - Define classpath and kapt options - Then run K2JVMCompiler#exec
K2JVMCompiler().exec( errStream = /* stream to output error */), args
= /* CLI options */)
• Command line compiler kotlinc hello.kt -d hello.jar kotlin -classpath
hello.jar HelloKt
• plugin/kapt options kotlinc hello.kt -d hello.jar -Xplugin=$KOTLIN_HOME/lib/kotlin-annotation-processing.jar -P plugin:org.jetbrains.kotlin.kapt3:sources=build/kapt/sources
-P plugin:org.jetbrains.kotlin.kapt3:classes=build/kapt/classes -P plugin:org.jetbrains.kotlin.kapt3:stubs=build/kapt/stubs -P plugin:org.jetbrains.kotlin.kapt3:aptMode=stubsAndApt -P plugin:org.jetbrains.kotlin.kapt3:correctErrorTypes=true -P plugin:org.jetbrains.kotlin.kapt3:apclasspath=lib/apt.jar
fun compile(): Compilation { val args = mutableListOf<String>() args.add("-d") args.add(classesDir.toString())
args.add("-no-stdlib") args.add("-classpath") args.add(fullClasspath().joinToString(separator = ":")) val sourceFiles = sourcesDir.listFiles() if (sourceFiles != null && sourceFiles.isNotEmpty()) { sourceFiles.forEach { args.add(it.toString()) } } val generatedDir = File(rootDir, "generated") args.addAll(annotationProcessorArgs(generatedDir.path)) val systemErrBuffer = Buffer() val stream = PrintStream(systemErrBuffer.outputStream()) val args = *args.toTypedArray() val exitCode = K2JVMCompiler().exec(stream, args) return Compilation(systemErrBuffer.readUtf8(), exitCode, generatedDir) }
private fun annotationProcessorArgs(generatedDirPath: String): List<String> { val sourceDir = File(rootDir,
"kapt/sources") val stubsDir = File(rootDir, "kapt/stubs") val kaptArgs = mutableMapOf<String, String>() kaptArgs["kapt.kotlin.generated"] = generatedDirPath val plugin = classpathFiles.find { it.name.startsWith(“kotlin-annotation-processing-embeddable”) } return listOf( "-Xplugin=$plugin", "-P", "plugin:org.jetbrains.kotlin.kapt3:sources=$sourceDir", "-P", "plugin:org.jetbrains.kotlin.kapt3:classes=$classesDir", "-P", "plugin:org.jetbrains.kotlin.kapt3:stubs=$stubsDir", "-P", "plugin:org.jetbrains.kotlin.kapt3:apclasspath=$servicesJar", "-P", "plugin:org.jetbrains.kotlin.kapt3:aptMode=stubsAndApt", "-P", "plugin:org.jetbrains.kotlin.kapt3:correctErrorTypes=true", "-P", "plugin:org.jetbrains.kotlin.kapt3:apoptions=${options(kaptArgs)}" ) }
• Conclusion • Extensible compiler API once you figure out
• Go into jetbrains/kotlin! • Support Kotlin in your code-gen project with kompile-testing • Still alpha though:D
• References • https://kotlinlang.org/docs/tutorials/command-line.html • https://kotlinlang.org/docs/reference/kapt.html • https://kotlinlang.org/docs/reference/compiler- plugins.html •
https://speakerdeck.com/kevinmost/writing-your-first- kotlin-compiler-plugin