Skip to content

Using the DSL API and Groovy Classes

Once your IDE is configured correctly you can further enhance the development experience by using a strongly typed version of the Platform 6 DSL.

This will allow the IDE to suggest and auto-complete on DSL methods as required.

/**
 * Turn the untyped 'p6' binding into a typed DSL for better IDE integration
 */
import io.platform6.core.dsl.TypedP6Dsl
def p6 = new TypedP6Dsl(p6)

p6.log.debug 'Hello World'

In the above example I transform the untyped p6 binding into a TypedP6Dsl which allows the IDE to assist the developer when using the DSL.

Note

Although the TypedP6Dsl primary purpose is to provide help to the developer and their IDE, there is little or no runtime penalty for using the typed p6 everywhere

TypedP6Dsl and local Classes

Here is an example of a local class I create in script module IDEDemoMain:

package io.platform6.app.core.scripts.idedemomain

import io.platform6.core.dsl.TypedP6Dsl

class MyLocalClass {

    TypedP6Dsl p6

    MyLocalClass(TypedP6Dsl p6) {
        this.p6 = p6
    }

    def sayHello(){
        p6.log.debug 'Hello from MyLocalClass'
    }
}
By passing the TypedP6Dsl rather than an untyped binding, the IDE syntax checking is more complete

Note

Because I have auto packaging disabled I am using package and import statements enabling the IDE to better manage my code

This class can be instantiated and called as follows:

package io.platform6.app.core.scripts.idedemomain

import io.platform6.core.dsl.TypedP6Dsl
def p6 = new TypedP6Dsl(p6)

new MyLocalClass(p6).sayHello()

TypedP6Dsl and library classes

Here is an example of a script module class I created for use from many other scripts; a library class:

package io.platform6.app.core.scripts.idedemolib

import io.platform6.core.dsl.TypedP6Dsl

class MyLibClass {

    TypedP6Dsl p6

    MyLibClass(TypedP6Dsl p6) {
        this.p6 = p6
    }

    def sayHello(){
        p6.log.debug 'Hello from MyLibClass'
    }
}

Again I’m passing a typed DSL instance to the library class MyLibClass in module IDEDemoLib

This class can be instantiated and called as follows:

/**
 * Sample Groovy script for development in an IDE using an example library of classes
 *
 * @include IDEDemoLib
 *
 */
package io.platform6.app.core.scripts.idedemomain

import io.platform6.app.core.scripts.idedemolib.MyLibClass
import io.platform6.core.dsl.TypedP6Dsl

def p6 = new TypedP6Dsl(p6)
new MyLibClass(p6).sayHello()

Here I use the @include keyword to include the IDEDemoLib module when I build. I must use import to correctly include a reference to this library class

By using package structures and enabling Gradle to build all /src/main/groovy code onto the classpath, IntelliJ is able to suggest and insert import statements as required

Here is a view of this example code in the IntelliJ project tree:

Example Modules