-
Notifications
You must be signed in to change notification settings - Fork 10
DSL Macros
To reuse a parts of DSL you can declare and use simple macros in the body of template. This is experimental feature.
Macros are declared with a word defmacro
following by quoted macro name, colon and macro body. Macro body
is a Groovy closure. Macro arguments are passed as usual closure arguments, but they are prepended with a $
symbol. You should not use reserved variables ($project, $context, $env, $databags and so on) as macros arguments, though you're free to use them in macro body. Variables can have default values.
defmacro "my_macro" : { $message, $value = "my_value" ->
my_step {
value = $value
message = $message
}
}
When you need to use macro in template body, you should call it as a method prepended with the label macro
. Arguments may be positional or prepended with name labels, but you cannot mix these styles. For example, this call is valid:
steps {
macro:my_macro("Message", "value")
}
This call is valid too:
steps {
macro:my_macro($message: "Message")
}
But this is not:
steps {
macro:my_macro($message: "Message", "Value")
}
You can use any valid Groovy values as arument values. If some of arguments is not set and does not have any default value, you'll get compilation error at runtime.
Also, it may be confusing, but in arguments you should use values that you really expect to set in macro. Example:
defmacro "defvar": { $name ->
$name = {
description = "Variable from template"
isOptional = false
clazz = Integer
}
}
...
workflow("with variable") {
variables {
macro:defvar($name:myname)
}
...
}
Note that variable passed to macro here is not quoted, so works as Groovy variable expression which is expected in such context. Quoted name will just not work in this case.