How to Create Expander Variables
It is possible to define custom variable bindings for ExpandersExpanderA unit of generation that combines a data tree instance (functional M1 model) with a template to generate an artifact., Features etc. This can be useful to avoid having to repeat the same logic for each expanderExpanderA unit of generation that combines a data tree instance (functional M1 model) with a template to generate an artifact..
1. Implement the ExpanderVariable interface
First, create a class that implements the ExpanderVariable interface.
This class needs to implement the resolve(ExpanderVariableContext) method.
It should return a VariableBinding object when it is applicable to do so.
You will also need to add the @VariableScope annotation to define where the variable is needed.
@VariableScope(ARTIFACT_PATH)
public class LayerDir implements ExpanderVariable {
@Override
public Optional<VariableBinding> resolve(ExpanderVariableContext context) {
return context.getExpander()
.map(expander -> {
String dir = expander.getLayerType().getSubdir();
return bind(dir).as("layerDir")
.describedAs("Expander layer sub-directory", LayerDir.class);
});
}
}
2. List the variable classes
Next, create a file META-INF/services/net.democritus.expander.variables.ExpanderVariable.
In this file, list the variable classes you have implemented, each on a separate line.
net.democritus.expander.variables.locations.LayerDir
During Expansion
The expandersExpanderA unit of generation that combines a data tree instance (functional M1 model) with a template to generate an artifact. will find the variable classes and add the variable binding to the mapping context during expansion.
<expander name="...">
...
<artifactPath>$module.rootDirectory$/src/app/$layerDir$</artifactPath>
</expander>