-
Notifications
You must be signed in to change notification settings - Fork 69
howto_create a new plugin
New plug-ins can implement an input reader, a merger, a matcher, a trigger interpreter, and/or a template engine as explained here.
|
Note
|
It is discouraged to have |
Each plug-in has to have an plug-in activator class implementing the interface GeneratorPluginActivator from the core-api. This class will be used to load the plug-in using the PluginRegistry as explained here. This class implements two methods:
-
bindMerger()→ returns a mapping of merge strategies and its implementation to be registered. -
bindTriggerInterpreter()→ returns the trigger interpreters to be provided by this plug-in.
Both methods create and register instances of mergers and trigger interpreters to be provided by the new plug-in.
The trigger interpreter has to implement the TriggerInterpreter interface from the core. The trigger interpreter defines the type for the new plugin and creates new InputReader and new Matcher objects.
The input reader is responsible of read the input object and parse it into FreeMarker models. The input reader must be implemented for the type of the input file. If there is any existent plugin that has the same file type as input, there will be no need to add a new input reader to the new plug-in.
The interface needed to add a new input reader is defined at the core. Each new sub plug-in must implements this interface if is needed an input reader for it.
The interface implements the basic methods that an input reader must have,
but if additional methods are required, the developer must add a new interface
that extends the original interface `InputReader.java` from the core-api
and implement that on the sub plug-in.
The methods to be implemented by the input reader of the new sub plugin are:
| Method | Return Type | Description |
|---|---|---|
|
|
This function will be called if matching triggers or matching templates should be retrieved for a given input object. |
|
Map<String, Object> |
This function should create the FreeMarker object model from the given input. |
|
|
States whether the given input object combines multiple input objects to be used for generation. |
|
List<Object> |
Will return the set of combined input objects if the given input combines multiple input objects. |
|
Map<String, Object> |
This method returns available template methods from the plugins as Map. If the plugin which corresponds to the input does not provide any template methods an empty Map will be returned. |
|
List<Object> |
Will return the set of combined input objects if the given input combines multiple input objects. |
The Input reader will create a model for FreeMarker. A FreeMarker model must
have variables to use them at the .ftl template file. Refer to Java Model to see the FreeMarker model example for java input files.
The input reader is an object that can be retrieved using the correspondent get
method of the trigger interpreter object. The trigger interpreter object is
loaded at the eclipse plug-in using the load plug-in method explained
here.
That way, when the core needs the input reader, only needs to call that getInputReader method.
The matcher implements the MatcherInterpreter interface from the core-api.
Should be implemented for providing a new input matcher. Input matcher are
defined as part of a trigger and provide the ability to restrict specific
inputs to a set of templates.
This restriction is implemented with a MatcherType enum.
E.g JavaPlugin
private enum MatcherType {
/** Full Qualified Name Matching */
FQN,
/** Package Name Matching */
PACKAGE,
/** Expression interpretation */
EXPRESSION
}Furthermore, matchers may provide several variable assignments, which might be dependent on any information of the matched input and thus should be resolvable by the defined matcher.
E.g JavaPlugin
private enum VariableType {
/** Constant variable assignment */
CONSTANT,
/** Regular expression group assignment */
REGEX
}The merger is responsible to perform merge action between new output with the
existent data at the file if it already exists. Must implement the Merger
interface from the core-api.
The implementation of the Merge interface must override the following methods:
| Method | Return Type | Description |
|---|---|---|
|
String |
Returns the type, this merger should handle. |
|
String |
Merges the patch into the base file. |
Is important to know that any exception caused by the merger must throw a MergeException from the core-api to the eclipse-plugin handle it.
Since version 3.x the Eclipse and Maven plugins of CobiGen utilize the Java ServiceLoader mechanic to find and register plugins at runtime. To enable a new plugin to be discovered by this mechanic the following steps are needed:
-
create the file
META-INF/services/com.devonfw.cobigen.api.extension.GeneratorPluginActivatorcontaining just the full qualified name of the class implementing theGeneratorPluginActivatorinterface, if the plugin provides aMergerand/or aTriggerInterpreter -
create the file
META-INF/services/com.devonfw.cobigen.api.extension.TextTemplateEnginecontaining just the full qualified name of the class implementing theTextTemplateEngineinterface, if provided by the plugin -
include
META-INFinto the target bundle (i.e. the folderMETA-INFhas to be present in the target jar file)
The java plugin provides both a Merger and a TriggerInterpreter. It contains therefore a com.devonfw.cobigen.api.extension.GeneratorPluginActivator file with the following content:
com.devonfw.cobigen.javaplugin.JavaPluginActivatorJavaPluginActivator class discoverable by the ServiceLoader at runtime.
-
to properly include the plugin into the current system and use existing infrastructure, you need to add the plugin as a module in
/cobigen/pom.xml(in case of aMerger/TriggerInterpreterproviding plugin) and declare that as the plugin’s parent in it’s ownpom.xmlvia
<parent>
<groupId>com.devonfw</groupId>
<artifactId>cobigen-parent</artifactId>
<version>dev-SNAPSHOT</version>
</parent>
or /cobigen/cobigen-templateengines/pom.xml (in case of a Merger/TriggerInterpreter providing plugin) and declare that as the plugin’s parent in it’s own pom.xml via
<parent>
<groupId>com.devonfw</groupId>
<artifactId>cobigen-tempeng-parent</artifactId>
<version>dev-SNAPSHOT</version>
</parent>If the plugin provides both just use the /cobigen/pom.xml.
-
The dependencies of the plugin are included in the bundle
-
To make the plugin available to the Eclipse plugin it must be included into the current
compositeContent.xmlandcompositeArtifacts.xmlfiles. Both files are located inhttps://github.com/devonfw/cobigen/tree/gh-pages/updatesite/{test|stable}. To do so, add an<child>entry to the<children>tag in both files and adapt thesizeattribute to match the new number of references. Thelocationattribute of the new<child>tag needs to be the artifact id of the pluginspom.xml.
In case of the Java plugin, the entry is
<child location="cobigen-javaplugin"/>Disclaimer
If you discover any documentation bugs or would like to request new content, please raise them as an issue or create a pull request. Contributions to this wiki are done through the main repo under the folder documentation.
License
This documentation is licensed under the Creative Commons License (Attribution-NoDerivatives 4.0 International)
