Skip to content

firelion9/kotlinDslGenerator

Repository files navigation

The Kotlin DSL generator

How to use

Apply plugin

Clone this repo to your computer and run ./gradlew publishToMavenLocal. After this, add buildscript dependency and apply plugin to your build.gradle.kts file (for build.gradle change apply(plugin = "com.firelion.dslgen") to apply(plugin: "com.firelion.dslgen")):

buildscript {
    repositories {
//         ...
        mavenLocal()
    }
    dependencies {
//        ...         
      classpath("com.firelion.dslgen:gradlePlugin:0.5.0")
    }
}

apply(plugin = "com.firelion.dslgen")
//...

Annotate a function

Apply @GenerateDsl(MyDslMarker::class) to a top-level function or constructor to generate a DSL for it. MyDslMarker should be an annotation class with at least binary retention applicable to functions, types and classes and marked with @DslMarker:

@DslMarker
@Retention(AnnotationRetention.BINARY)
@Target(AnnotationTarget.CLASS, AnnotationTarget.TYPE, AnnotationTarget.FUNCTION, AnnotationTarget.PROPERTY)
annotation class MyDslMarker

You can modify DSL generation using @UseDefaultConstructions and @UseAlternativeConstruction.

You can check out some examples under tests folder.

Configuration

Some aspects of DSL generator can be configured in gradle build file using DslGenExt:

extensions.configure(DslGenExt::class.java) {
//  Allows generation of DSLs for functions with default parameters (default value is `true`)
//  When set to `false`, default values of functions parameters would be ignored
  allowDefaultArgs = true

//  If `allowDefaultArgs` is set to `true`, this property controls post-processor used to support default parameters
//  Allowed values:
//  * LEGACY: reads all generated class files after compilation and modify their byte-code. Not recommended
//  * COMPILER_PLUGIN(default): modifies class file generation process using kotlin compiler plugin. 
//                              For now, it uses the same byte-code modification technics as LEGACY option.
//                     
  dslPostProcessorMode = DslPostProcessorMode.COMPILER_PLUGIN /* or DslPostProcessorMode.LEGACY */
}

Limitations

  • Updating to a newer version may break binary compatibility of generated DSL

  • Member functions of classes with type parameters are unsupported

  • Experimental implicit receivers (added with context()) are unsupported

  • You can't suppress any warning generated by the DSL generator

  • By default, DSL generator recursively generates sub-DSL with the same marker for each parameter. If you have more than one different marker, marker for common sub-DSL functions of different DSLs is undefined, so you possibly should stop generation of sub-DSL at some point using @UseDefaultConstructions with respective parameters (useDefaultSubDslConstruction, useSubFunctionSetter, useSubFunctionAdder) set to false OR specify explicit DSL marker using @GenerateDsl

  • Only Kotlin/JVM platform is supported for now

About

KSP-based library for kotlin DSL generation

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages