Pawan
Pawan

Reputation: 273

Create Eclipse shortcut to run particular target of an build file

I have different projects and several build files with different targets in it.
Generally if i want to run a target i use to navigate to 'Ant' view and then select the build file and then selects the target to run.

Instead of doing several steps every time, is there any way to assign a keyboard shortcut for particular target so that i can run that target easily.

Upvotes: 2

Views: 3284

Answers (3)

Gurce
Gurce

Reputation: 664

NOTE: This is my initial "Practically Macro + Beanshell script" technique, which I've given up on. I prefer the other technique mentioned in my 2nd answer.

I've been looking for a way to do this for specific make targets too. The only in-built shortcut available is to "Rebuilt Last Target", which isn't all that helpful if you're frequently swapping between targets.

While I don't have a concrete solution yet, I'm working towards one.

I'm assessing the "Practically Macro" plug-in. It has the ability to assign a shortcut key to a macro. It also has the ability to define a macro as a beanshell script.

So all that's left is to figure out what kind of beanshell script would be capable of running a specific make target.

I've tried to explore the Eclipse API via the EASE scripting tool.

I'll share my steps/notes on how I successfully ran a make-target programmatically (via their javascript interface):

targetmanager = org.eclipse.cdt.make.core.MakeCorePlugin.getDefault().getTargetManager()

projects = targetmanager.getTargetBuilderProjects()

folder = projects[0].getFolder("Path/To/My/Build/Targets/")

// targets = targetmanager.getTargets(folder)
target = targetmanager.findTarget(folder, "MyBuildTargetName")

target.build(new org.eclipse.core.runtime.NullProgressMonitor())

So I think all that's left is for me (or someone else that's interested) to:

  • convert this script from javascript to beanshell and add it as a macro via the "Practically Macro" plugin
  • Assign a shortcut key to it

...quite an involved way to go about it, so if anyone has any simpler alternatives, I'm open to hear them.

UPDATE:

FWIW, I managed to create a beanshell script for "Practically Macro" in this form:

//Scripts are beanshell format (see http://www.beanshell.org/)

//variable               type
//styledText             the org.eclipse.swt.custom.StyledText instance for the current editor
//console                write output to the macro console via console.write(String), .writeln(String), .write(Exception)
//findTarget             the instance of org.eclipse.jface.text.IFindReplaceTarget
import org.eclipse.swt.custom.StyledText;
import org.eclipse.jface.text.IFindReplaceTarget;

c = org.eclipse.core.runtime.Platform.getBundle("org.eclipse.cdt.make.core").loadClass("org.eclipse.cdt.make.core.MakeCorePlugin");
m = c.getMethod("getDefault", null);
dflt = m.invoke(null, null);

targetmanager = dflt.getTargetManager();

projects = targetmanager.getTargetBuilderProjects();

folder = projects[0].getFolder("Path/To/My/Build/Targets/");

target = targetmanager.findTarget(folder, "MyBuildTargetName");

target.build(new org.eclipse.core.runtime.NullProgressMonitor());

And yes, it does kind-of work, but the only gotchya is that the eclipse ide stalls without any refreshing. Only after the build completes does the console pane get updated with the final build output (in one hit) and Eclipse becomes responsive again.

So it's not perfect, but I suppose it's incremental progress in the direction I'm after... As for what causes this freeze/stall during the build, I can't say for sure, but I suspect that the Practically Macro plugin locks Eclipse up until the macro completes.

Next Time:

Perhaps if I had another window of time to look into this again, I'd try to see if I can trigger an EASE-javascript script via a keyboard shortcut. This page seems to hint that it is possible:

https://wiki.eclipse.org/EASE/Scripts

Upvotes: 0

Gurce
Gurce

Reputation: 664

Ok, I've given the "EASE-script + keyboard shortcut" technique a try and that works much better:

I created a "my_build_target.js" javascript file at the base of my workspace/project:

/**
 * keyboard: Alt+Shift+2
 */

targetmanager = org.eclipse.cdt.make.core.MakeCorePlugin.getDefault().getTargetManager()

projects = targetmanager.getTargetBuilderProjects()

folder = projects[0].getFolder("Test/Scenarios/Win32")

// targets = targetmanager.getTargets(folder)
target = targetmanager.findTarget(folder, "MyBuildTargetName")

target.build(new org.eclipse.core.runtime.NullProgressMonitor())

Note the use of the "magic keyword" within the header comment which specifies the desired shortcut-key you want for the script.

You then need to specify the locations for your scripts to be loaded from by going to:

  • "Windows >> Preferences >> Scripting >> Script Locations"

Personally, I clicked the "Add Workspace" button and specified the base of my workspace/project (that's where I housed my script).

I restarted Eclipse and then used my specified "Alt+Shift+2" shortcut key.

Awesome, it works :)

One gotchya is that I can't cancel a build that's in progress with this method. I suspect it's due to me using that NullProgressMonitor class when I call .build(). If I learn of a way to add a proper progress monitor here (that lets you cancel a progressing build), then I will update this answer...

Upvotes: 0

Sandeep Pathak
Sandeep Pathak

Reputation: 10747

The main preference page for keys can be found under Window > Preferences > General > Keys (or faster: Press Ctrl+3, type Keys and press Enter) . See How to manage keyboard shortcuts in Eclipse and why you should article to achieve what you want.

Upvotes: 1

Related Questions