© Markus Duft 2018
Markus DuftEclipse TEA Revealedhttps://doi.org/10.1007/978-1-4842-4093-9_13

13. EASE Integration

Markus Duft1 
(1)
Peggau, Steiermark, Austria
 
EASE (Eclipse Advanced Scripting Environment)1 is a very popular extension for Eclipse that has a similar focus to TEA’s: extending the IDE. It can be used to dynamically extend the IDE using scripts in various scripting languages (such as Python—which we’ll use in the samples, JavaScript, Groovy, etc .2). There are situations where EASE is better suited to extend the IDE than TEA—for instance, if users of the IDE should be able to change scripted behavior. Then there are situations where TEA is better suited—for instance when users of the IDE should not be able to change IDE behavior. And, last but not least, there are situations where a combination of both is the best solution—for instance, if users of the IDE should be able to customize parts of the IDE behavior in scripts. To summarize the main differences:
  • TEA is part of the binary IDE. EASE scripts are (usually) part of the target workspace. This makes a huge difference in who can edit/change IDE behavior – and how much effort is required.

  • TEA objects are written in Java (or any language that compiles to Java classes). EASE scripts can be written in various scripting languages.

  • TEA objects may access internals of the IDE when dependencies to these internals are specified. EASE scripts may access any class in the running IDE, as long as the containing bundle exports the class’s package.

At my workplace, we use both TEA and EASE. TEA objects usually provide the base framework for IDE extensions as well as default implementations for TaskChain objects, which wire up these objects in a meaningful default way. EASE scripts are then used whenever something must deviate from the standard setup, or if there are project-specific behaviors that should not be part of the default set of TEA objects.

TEA provides integration with EASE in both directions: TEA TaskChain implementations can reference any EASE script and run it as if it were a TEA Task, and EASE scripts can use the TEA script module to interact with TEA.

Executing EASE Scripts from TEA

Listing 13-1 shows a sample TaskChain that launches the script "/sample.py".
@TaskChainId(description =
                   "CH13-S01: Run EASE sample.py")
@TaskChainMenuEntry(path = "Samples")
@Component
public class EaseTaskChain implements TaskChain {
      @TaskChainContextInit
      public void init(TaskExecutionContext c) {
            c.addTask(
                   new EaseScriptTask("/sample.py"));
      }
}
Listing 13-1

TaskChain Referencing an EASE Script

Now we just need to have an EASE script repository that contains this sample.py script. Launch the TEA-Book-Samples launch configuration once more, to follow along.

In the runtime workspace, add a new project. The project type is not important for this project; you can use the general project. See Figure 13-1.
../images/470772_1_En_13_Chapter/470772_1_En_13_Fig1_HTML.jpg
Figure 13-1

Adding a general project to contain sample scripts

The name of the project is up to you. I’ll call it com.example.scripts for the rest of the example.

Now go to Window ➤ Preferences ➤ Scripting ➤ Script Locations and add a new workspace location—choose the new com.example.scripts project. This is shown in Figure 13-2.
../images/470772_1_En_13_Chapter/470772_1_En_13_Fig2_HTML.jpg
Figure 13-2

Configuring script location for EASE

Next, we need to create a simple script ("sample.py") in the project. Right-click the project and select New ➤ File to do so. Have a look at Listing 13-2 for the content.
# This is a sample script which does not do very much
# io : none
loadModule('/TEA/Tasking')
getLog().info("This is the TEA log from a script")
Listing 13-2

sample.py Content

This script will not do very much. It tells EASE to not create a dedicated console for the script when executing ('# io : none'3). The script will use the TEA console for output anyway.

Now try running the sample in the runtime workspace (TEA-Book-Samples) from TEA ➤ Samples ➤ CH13-S01: Run EASE sample.py. You should see output as shown in Figure 13-3.
../images/470772_1_En_13_Chapter/470772_1_En_13_Fig3_HTML.jpg
Figure 13-3

Output of sample.py executed from TEA

This mechanism can be used to call back to scripts in the workspace (which can be modified by the user) to perform customizable actions while executing a TaskChain that is otherwise not easily modifiable by IDE users (as it is installed in the IDE).

Executing a TaskChain from EASE

The opposite direction of the previous example is calling a TEA TaskChain from an EASE script. This allows integrating TEA wherever EASE supports integration for scripts. This includes (but is not limited to) dynamically creating toolbar or menu entries, reacting to E4 events on Eclipse’s event bus, and much more.

We can reuse the already set up workspace from the previous example. Add another script to com.example.scripts: tea.py. The content is shown in Listing 13-3.

Caution

Scripts in this chapter have been formatted for readability. Please join lines ending with “” when copying into the Eclipse editor. Otherwise, the script engine will not be able to execute it.

# Integrate with TEA
#
# name: Execute TEA Sample (EASE)
# io : none
# toolbar : Console
# image : platform:/plugin/org.eclipse.tea.core.ui/
             resources/tea.png
loadModule("/TEA/Tasking")
def myTask():
      getLog().info("This is a Task!")
runTaskChain(createTaskChain(
      "EASE Sample TaskChain", [ "myTask()" ]))
Listing 13-3

Content of tea.py

You may notice that right after saving the content of this script, a new toolbar button will appear in the Console view of Eclipse. It is shown in Figure 13-4, along with the output you will see when pressing it.
../images/470772_1_En_13_Chapter/470772_1_En_13_Fig4_HTML.jpg
Figure 13-4

Button to Execute Script (Upper-Right Corner) and Output

The last thing to achieve with this script is to integrate it properly with the TEA menu in the main menu toolbar. Let’s change the scripts headers (which are comments processed by EASE). Replace the commented top section of the script with the code shown in Listing 13-4 (again, remember to join lines ending with a “”).
# Integrate with TEA
#
# name: Samples/Execute TEA Sample (EASE)
# io : none
# tea : true
# image : platform:/plugin/org.eclipse.tea.core.ui/
             resources/tea.png
...
Listing 13-4

Integrate Script with TEA Menu

This will tell TEA that this script should be integrated in the TEA menu (the button in the Console View will disappear). The path within the menu is calculated from the script’s name (note the "Samples/" in the name).

You can now execute the script from TEA ➤ Samples ➤ Execute TEA Sample (EASE). It should behave exactly the same as before when running from the Console View.

Following are additional TEA specific EASE headers (interpreted comments at the top of scripts):
  • tea-dev : true: This will instruct TEA to only show the menu entry when Window ➤ Preferences ➤ Tasking (TEA) ➤ TEA Development Options ➤ Show TaskChains intended for development/debugging is checked.

  • tea-grouping : <groupingId> : This will instruct TEA to apply the given groupingId to the menu entry. This behaves exactly like when using groupingId field of the TaskChainMenuEntry annotation, as described in Chapter 6.

As a final example, let’s take a look at how to reuse existing TEA object implementations from scripts. Adapt the tea.py script to have content similar to Listing 13-5 and rerun it from the menu.
# Integrate with TEA
#
# name: Samples/Execute TEA Sample (EASE)
# io : none
# tea : true
# image : platform:/plugin/org.eclipse.tea.core.ui/
             resources/tea.png
loadModule("/TEA/Tasking")
from org.eclipse.tea.core.internal.tasks import
      BuiltinTaskListChains
def myTask():
      getLog().info("This is a Task!")
chain = lookupTaskChain('CleanBuildAllProjects')
if not runTaskChain(
             createTaskChain("EASE Sample TaskChain",
             [ "myTask()", chain,
             BuiltinTaskListChains ])).isOK():
       raise Exception("This did not work")
Listing 13-5

Reuse of Existing TEA Objects

This script now demonstrates how to reuse Task and TaskChain implementations from the script:
  1. 1.

    Use lookupTaskChain('...') to find any existing registered TaskChain.

     
  2. 2.

    Simply import any existing Java class by name and use it directly in the createTaskChain call to reference existing Task implementations.

     

TEA Event Triggered EASE Scripts

One more noteworthy thing: TEA provides a bridge from its internal TaskingLifeCycleListener-related framework to the E4 event bus. This allows attaching EASE scripts to certain TEA events. Try this out by creating a new script ("event.py") in the existing com.example.scripts project. Use the content from Listing 13-6 to kick off the experiments.
# Integrate with TEA
#
# name: Samples/TEA Events
# io : none
# onEventBus : org/eclipse/tea/*
loadModule('/TEA/Tasking')
getLog().info("got event " + event.getTopic())
Listing 13-6

Reacting to TEA Events from EASE

Now run any of the sample TaskChains from the TEA menu. You will notice a lot of output generated from the event.py script,— as shown in Figure 13-5.
../images/470772_1_En_13_Chapter/470772_1_En_13_Fig5_HTML.jpg
Figure 13-5

Output of event.py when executing any TaskChain

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.118.166.98