4.3 Hooking into Events

Griffon provides the ability to hook into scripting events. These are events triggered during execution of Griffon target and plugin scripts.

The mechanism is deliberately simple and loosely specified. The list of possible events is not fixed in any way, so it is possible to hook into events triggered by plugin scripts, for which there is no equivalent event in the core target scripts.

#### Defining event handlers

Event handlers are defined in scripts called `_Events.groovy`. Griffon searches for these scripts in the following locations:

- `USER_HOME/.griffon/scripts` - user-specific event handlers
- `PROJECT_HOME/scripts` - application-specific event handlers
- `PLUGINS_HOME/*/scripts` - plugin-specific event handlers

Whenever an event is fired, all the registered handlers for that event are executed. Note that the registration of handlers is performed automatically by Griffon, so you just need to declare them in the relevant `_Events.groovy` file.

Event handlers are blocks defined in `_Events.groovy`, with a name beginning with "event". The following example can be put in your /scripts directory to demonstrate the feature:

    eventCreatedArtefact = { type, name ->
       println "Created $type $name"
    }eventStatusUpdate = { msg ->
       println msg
    }eventStatusFinal = { msg ->
       println msg
    }

You can see here the three handlers `eventCreatedArtefact`, `eventStatusUpdate`, `eventStatusFinal`. Griffon provides some standard events, which are documented in the command line reference guide. For example the compile command fires the following events:

- `CompileStart` - Called when compilation starts, passing the kind of compile - source or tests
- `CompileEnd` - Called when compilation is finished, passing the kind of compile - source or tests

#### Triggering events

To trigger an event simply call the event() closure:

    event("StatusFinal", ["Super duper plugin action complete!"])

#### Common Events

Below is a table of some of the common events that can be leveraged:

Event | Parameters | Description
--- | --- | ---
StatusUpdate | message | Passed a string indicating current script status/progress
StatusError | message | Passed a string indicating an error message from the current script
StatusFinal | message | Passed a string indicating the final script status message, i.e. when completing a target, even if the target does not exit the scripting environment
CreatedArtefact | artefactType,artefactName | Called when a create-xxxx script has completed and created an artifact
CreatedFile | fileName | Called whenever a project source filed is created, not including files constantly managed by Griffon
Exiting | returnCode | Called when the scripting environment is about to exit cleanly
PluginInstalled | pluginName | Called after a plugin has been installed
CompileStart | kind | Called when compilation starts, passing the kind of compile - source or tests
CompileEnd | kind | Called when compilation is finished, passing the kind of compile - source or tests
DocStart | kind | Called when documentation generation is about to start - javadoc or groovydoc
DocEnd | kind | Called when documentation generation has ended - javadoc or groovydoc