Slide 1

Slide 1 text

Develop your CI Tools

Slide 2

Slide 2 text

Xavier F. Gouchet Senior Software Engineer at Datadog @xgouchet / @datadoghq

Slide 3

Slide 3 text

No content

Slide 4

Slide 4 text

0. Introduction

Slide 5

Slide 5 text

Small team ▫ 3 engineers ▫ Lots of features ▫ Lots of bug fixes ▫ Lots of improvements ▫ Small bandwidth Unsplash photo by @marvelous

Slide 6

Slide 6 text

CI setup ▫ Gitlab ▫ Bitrise * photo by @xgouchet

Slide 7

Slide 7 text

Delegating to the CI ▫ Runs automatically ▫ Strict and thorough ▫ Great way to offload the PR Review

Slide 8

Slide 8 text

Code Review Pyramid Credit: Gunnar Morling (CC-BY-SA 4.0) CODE STYLE TESTS IMPLEMENTATION API SEMANTICS DOCUMENTATION

Slide 9

Slide 9 text

1. Detekt Your code, your rules…

Slide 10

Slide 10 text

What is Detekt? ▫ Kotlin Static Analysis ▫ Code smells ▫ Performance Issues ▫ Code complexity ▫ Documentation Missing ▫ …

Slide 11

Slide 11 text

Detekt Report

Slide 12

Slide 12 text

How does Detekt work ▫ Visitor pattern over the Syntax Tree (PSI) ▫ With Type Resolution ▫ Report issues on specific patterns

Slide 13

Slide 13 text

Custom Rule class MyCustomRule : Rule() { override val issue: Issue = Issue( javaClass.simpleName, Severity.CodeSmell, "A description of the issue…", Debt.TWENTY_MINS ) }

Slide 14

Slide 14 text

Custom Rule class MyCustomRule : Rule() { override val issue: Issue = Issue( javaClass.simpleName, Severity.CodeSmell, "A description of the issue…", Debt.TWENTY_MINS ) }

Slide 15

Slide 15 text

Custom Rule class MyCustomRule : Rule() { override val issue: Issue = Issue( javaClass.simpleName, Severity.CodeSmell, "A description of the issue…", Debt.TWENTY_MINS ) }

Slide 16

Slide 16 text

Custom Rule override fun visitExpression(expression: KtExpression) { if (invalidExpression(expression)) report( CodeSmell( issue, Entity.from(expression), "A message" ) ) }

Slide 17

Slide 17 text

Custom Rule override fun visitExpression(expression: KtExpression) { if (invalidExpression(expression)) report( CodeSmell( issue, Entity.from(expression), "A message" ) ) }

Slide 18

Slide 18 text

Custom Rule override fun visitExpression(expression: KtExpression) { if (invalidExpression(expression)) report( CodeSmell( issue, Entity.from(expression), "A message" ) ) }

Slide 19

Slide 19 text

A few examples of our custom rules ▫ Forbid throwing exceptions ▫ Forbid using check or require ▫ Forbid usage of !! ▫ Flag unsafe third party methods ▫ Flag TODO comments without a Jira ticket

Slide 20

Slide 20 text

Going Further ▫ Make the rule configurable from detekt.yml

Slide 21

Slide 21 text

Custom Rule class MyCustomRule( config: Config ) : Rule(config) { private val enableFoo: Boolean by config(defaultValue = true) //… }

Slide 22

Slide 22 text

Custom Rule class MyCustomRule( config: Config ) : Rule(config) { private val enableFoo: Boolean by config(defaultValue = true) //… }

Slide 23

Slide 23 text

Custom Rule class MyCustomRule( config: Config ) : Rule(config) { private val enableFoo: Boolean by config(defaultValue = true) //… }

Slide 24

Slide 24 text

More info… ▫ https:/ /detekt.dev/ ▫ https:/ /github.com/detekt/detekt ▫ https:/ /thebakery.dev/30/

Slide 25

Slide 25 text

2. Kotlin Symbol Processor

Slide 26

Slide 26 text

What is KSP? ▫ Lightweight Compiler Plugin ▫ Similar to (K)APT but faster ▫ Source code is read-only ▫ Code generation

Slide 27

Slide 27 text

How does KSP work ▫ Called at compile time ▫ Processor can query files and/or declarations ▫ Can generate new code (but not modify existing one)

Slide 28

Slide 28 text

Custom Processor class MyCustomSymbolProcessor( val codeGenerator: CodeGenerator, val logger: KSPLogger ) : SymbolProcessor { override fun process(resolver: Resolver): List { return emptyList() } }

Slide 29

Slide 29 text

Custom Processor class MyCustomSymbolProcessor( val codeGenerator: CodeGenerator, val logger: KSPLogger ) : SymbolProcessor { override fun process(resolver: Resolver): List { return emptyList() } }

Slide 30

Slide 30 text

Custom Processor class MyCustomSymbolProcessor( val codeGenerator: CodeGenerator, val logger: KSPLogger ) : SymbolProcessor { override fun process(resolver: Resolver): List { return emptyList() } }

Slide 31

Slide 31 text

Custom Processor override fun process(resolver: Resolver): List { val deferred = mutableListOf() resolver.getSymbolsWithAnnotation("com.example.MyAnnotation") .forEach { if (!generateSomething(it)) deferred.add(it) } return deferred }

Slide 32

Slide 32 text

Custom Processor override fun process(resolver: Resolver): List { val deferred = mutableListOf() resolver.getSymbolsWithAnnotation("com.example.MyAnnotation") .forEach { if (!generateSomething(it)) deferred.add(it) } return deferred }

Slide 33

Slide 33 text

Custom Processor override fun process(resolver: Resolver): List { val deferred = mutableListOf() resolver.getSymbolsWithAnnotation("com.example.MyAnnotation") .forEach { if (!generateSomething(it)) deferred.add(it) } return deferred }

Slide 34

Slide 34 text

Custom Processor override fun process(resolver: Resolver): List { val deferred = mutableListOf() resolver.getSymbolsWithAnnotation("com.example.MyAnnotation") .forEach { if (!generateSomething(it)) deferred.add(it) } return deferred }

Slide 35

Slide 35 text

Custom Processor override fun process(resolver: Resolver): List { val deferred = mutableListOf() resolver.getSymbolsWithAnnotation("com.example.MyAnnotation") .forEach { if (!generateSomething(it)) deferred.add(it) } return deferred }

Slide 36

Slide 36 text

An example of custom processor ▫ Generate no-op implementation of annotated interfaces

Slide 37

Slide 37 text

DataWriter.kt @NoOpImplementation internal interface DataWriter { fun write(element: T) fun write(data: List) }

Slide 38

Slide 38 text

NoOpDataWriter.kt internal class NoOpDataWriter : DataWriter { public override fun write(element: T) { } public override fun write(data: List) { } }

Slide 39

Slide 39 text

DataReader.kt @NoOpImplementation internal interface DataReader { fun readNext(): T? }

Slide 40

Slide 40 text

NoOpDataReader.kt internal class NoOpDataReader : DataReader { public override fun readNext(): T? { return null } }

Slide 41

Slide 41 text

Going Further ▫ KotlinPoet library

Slide 42

Slide 42 text

KotlinPoet val file = FileSpec.builder("", "Greeter") .addType( TypeSpec.classBuilder("Greeter") .addFunction( FunSpec.builder("greet") .addParameter("name", String::class) .addStatement("println(%P)", "Hello, \$name").build() ).build() ).build()

Slide 43

Slide 43 text

KotlinPoet val file = FileSpec.builder("", "Greeter") .addType( TypeSpec.classBuilder("Greeter") .addFunction( FunSpec.builder("greet") .addParameter("name", String::class) .addStatement("println(%P)", "Hello, \$name").build() ).build() ).build()

Slide 44

Slide 44 text

KotlinPoet val file = FileSpec.builder("", "Greeter") .addType( TypeSpec.classBuilder("Greeter") .addFunction( FunSpec.builder("greet") .addParameter("name", String::class) .addStatement("println(%P)", "Hello, \$name").build() ).build() ).build()

Slide 45

Slide 45 text

KotlinPoet val file = FileSpec.builder("", "Greeter") .addType( TypeSpec.classBuilder("Greeter") .addFunction( FunSpec.builder("greet") .addParameter("name", String::class) .addStatement("println(%P)", "Hello, \$name").build() ).build() ).build()

Slide 46

Slide 46 text

KotlinPoet class Greeter() { fun greet(name: String) { println("""Hello, $name""") } }

Slide 47

Slide 47 text

More info… ▫ https:/ /kotlinlang.org/docs/ksp-overview.html ▫ https:/ /github.com/google/ksp ▫ https:/ /square.github.io/kotlinpoet/

Slide 48

Slide 48 text

3. Gradle Plugin

Slide 49

Slide 49 text

What is Gradle? ▫ Custom tasks ▫ Compile time checks ▫ Helper classes

Slide 50

Slide 50 text

How does Gradle work ▫ Project object created by Gradle ▫ Plugin adds extension/tasks to it

Slide 51

Slide 51 text

Custom Task open class MyCustomTask : DefaultTask() { init { group = "MyCompany" description = "Description of the task" } @TaskAction fun applyTask() { } }

Slide 52

Slide 52 text

Custom Task open class MyCustomTask : DefaultTask() { init { group = "MyCompany" description = "Description of the task" } @TaskAction fun applyTask() { } }

Slide 53

Slide 53 text

Custom Task open class MyCustomTask : DefaultTask() { init { group = "MyCompany" description = "Description of the task" } @TaskAction fun applyTask() { } }

Slide 54

Slide 54 text

Custom Extension open class MyCustomExtension( var outputFile: File = File(DEFAULT_OUTPUT_FILENAME), var enableSomething: Boolean = false )

Slide 55

Slide 55 text

Custom Plugin class MyCustomPlugin : Plugin { override fun apply(target: Project) { val myExtension = target.extensions .create("myExtension", MyCustomExtension::class.java) val myTask = target.tasks .create("myTask", MyCustomTask::class.java) myTask.extension = myExtension } }

Slide 56

Slide 56 text

Custom Plugin class MyCustomPlugin : Plugin { override fun apply(target: Project) { val myExtension = target.extensions .create("myExtension", MyCustomExtension::class.java) val myTask = target.tasks .create("myTask", MyCustomTask::class.java) myTask.extension = myExtension } }

Slide 57

Slide 57 text

Custom Plugin class MyCustomPlugin : Plugin { override fun apply(target: Project) { val myExtension = target.extensions .create("myExtension", MyCustomExtension::class.java) val myTask = target.tasks .create("myTask", MyCustomTask::class.java) myTask.extension = myExtension } }

Slide 58

Slide 58 text

Custom Plugin class MyCustomPlugin : Plugin { override fun apply(target: Project) { val myExtension = target.extensions .create("myExtension", MyCustomExtension::class.java) val myTask = target.tasks .create("myTask", MyCustomTask::class.java) myTask.extension = myExtension } }

Slide 59

Slide 59 text

Custom Plugin class MyCustomPlugin : Plugin { override fun apply(target: Project) { val myExtension = target.extensions .create("myExtension", MyCustomExtension::class.java) val myTask = target.tasks .create("myTask", MyCustomTask::class.java) myTask.extension = myExtension } }

Slide 60

Slide 60 text

An example of custom Gradle Tasks ▫ Compute dependencies size and OSS Licenses ▫ Keep track of API Surface ▫ Generate Data Class from JSON Schema ▫ From a remote git repository

Slide 61

Slide 61 text

Going Further ▫ Link tasks together

Slide 62

Slide 62 text

Custom Plugin override fun apply(target: Project) { // ... target.afterEvaluate { p -> p.tasks.withType(KotlinCompile::class.java) { dependsOn(myTask) } } }

Slide 63

Slide 63 text

More info… ▫ https:/ /docs.gradle.org/current/userguide/custom_plugins.html ▫ https:/ /www.youtube.com/watch?v=ww0D8WMWPXc ▫ https:/ /speakerdeck.com/xgouchet/rock-the-gradle-mobileera

Slide 64

Slide 64 text

4. Kotlin Compiler Plugin Let’s start with the first set of slides

Slide 65

Slide 65 text

What is it? ▫ Plugin getting access to Kotlin source code / IR ▫ Can manipulate the code before compilation into JVM/JS/… ▫ Similar to Aspect Oriented Programming

Slide 66

Slide 66 text

Cross-cutting concerns

Slide 67

Slide 67 text

How does it work Parsing Resolution Analysis Code Generation Source Code PSI (AST) IR JVM JS Native

Slide 68

Slide 68 text

Compiler Plugin ▫ Kotlin Compiler Plugin ▫ Gradle Plugin / Maven Plugin ▫ IDE Plugin (icons / syntax)

Slide 69

Slide 69 text

Examples of Kotlin Compiler Plugins ▫ Generate Compose boilerplate code ▫ All-open makes all class open by default

Slide 70

Slide 70 text

More info… ▫ https:/ /www.youtube.com/watch?v=w-GMlaziIyo ▫ https:/ /arrow-kt.io/docs/meta/ ▫ https:/ /medium.com/@heyitsmohit/writing-kotlin-compiler-pl ugin-with-arrow-meta-cf7b3689aa3e

Slide 71

Slide 71 text

*. A little bit more Let’s start with the first set of slides

Slide 72

Slide 72 text

PSI Viewer ▫ IDE Plugin for Intellij Idea and Android Studio ▫ Displays the actual PSI for the open tab

Slide 73

Slide 73 text

No content

Slide 74

Slide 74 text

*. Conclusion Let’s start with the first set of slides

Slide 75

Slide 75 text

Detekt Gradle KSP KCP

Slide 76

Slide 76 text

“ Your Detekt rules, KSP, Gradle scripts are still code. Keep them as clean, maintainable and trustworthy as your production code

Slide 77

Slide 77 text

Useful links ▫ https:/ /github.com/datadog/dd-sdk-android

Slide 78

Slide 78 text

Thank You! Any questions? @xgouchet @datadoghq Unsplash photo by @marcosjluiz

Slide 79

Slide 79 text

Credits Special thanks to all the people who made and released these awesome resources for free: ▫ Presentation template by SlidesCarnival ▫ Photographs by Unsplash 79