Archive for the ‘jruby’ tag
JRuby and static imports
The last few weeks have been interesting working with SWT and some newly acquired (J)Ruby chops, to hack together a couple of approaches to build a recorder for SWTBot. Here is one of the interesting hacks which was cobbled together:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | require 'java' module Recorder module SWTInitializer include_package 'org.eclipse.swt' include_package 'org.eclipse.swt.layout' include_package 'org.eclipse.swt.widgets' include_package 'org.eclipse.swt.custom' include_packate 'org.eclipse.swtbot.swt.finder' # loop through a few known classes to implement static imports def method_missing(method, *args, &block) klass = [ org.eclipse.swtbot.swt.finder.matchers.WidgetMatcherFactory, org.eclipse.swtbot.swt.finder.utils.SWTUtils].find do |k| k.respond_to?(method) end return klass.send(method, *args, &block) if klass super end end end |
To use this class:
1 2 3 4 5 6 7 8 | class Application include Recorder::SWTInitializer # the with_text invocation falls into the method_missing # which delegates it to WidgetMatcherFactory def self.run SWTBot.new().widgets(with_text('hello')) end end |
An embedded interpreter for eclipse
As a java developer who is starting to use ruby and javascript for a lot of things lately — there is one thing I miss most. An embedded shell/interpreter for eclipse!
An embedded console or an interpreter is a very powerful tool, it allows you to do some very interesting things with your software as it is running, play around with it, tweak it and anything else you can ever imagine. All of this without the edit-save-compile-relaunch cycle.
Lately I’ve been working on an embedded console for eclipse. The primary motivation was to try out scripting approaches for SWTBot. But I soon realized that I was using it for more than just scripting tests. I was using it to learn how eclipse works, try out different approaches to decide which one is best.
Some of the features include code completion, history lookup. This is possible using jruby’s objectspace and the readline support.
Here’s a small teaser video of what you can do with it an embedded jruby console for eclipse.
Cucumber On JRuby inside Eclipse
Fredrick recently asked on the swtbot newsgroup:
My goal is to be able to write some user acceptance tests (using Cucumber) to be able to tests some of my Eclipse plug-ins.
Cucumber is a BDD framework written in (J)ruby. It executes plain text files as functional tests. As a first step, the goal was to be able to print a simple ‘hello world’:
This required being able to bundle jruby with the necessary gems, the jruby jar is already OSGi-fied, so creating a manifest was not required.
First drop in the jruby-complete.jar that we just created inside the target eclipse’s plugins directory.
Then create an eclipse application with the following in it:
public class CucumberRunner implements IApplication {
public Object start(IApplicationContext context) throws Exception {
Bundle bundle = Platform.getBundle("org.jruby.jruby");
URL jrubyHome = FileLocator.toFileURL(bundle.getEntry("/META-INF/jruby.home"));
RubyInstanceConfig config = new RubyInstanceConfig();
config.setJRubyHome(jrubyHome.toString());
Ruby runtime = JavaEmbedUtils.initialize(new ArrayList(), config);
RubyRuntimeAdapter evaler = JavaEmbedUtils.newRuntimeAdapter();
evaler.eval(runtime, "p 'Hello, Eclipse World'");
JavaEmbedUtils.terminate(runtime);
return EXIT_OK;
}
public void stop() {
// do nothing
}
}
Now all we needed was to be able to execute the cucumber executable instead of printing hello world
Import the cucumber plugin with a sample calculator to execute it:
Run JRuby From Within A Jar And Package Your Own Gems Along
Jruby-in a jar already bundles rspec and rake, so the goal was to find out where it gets packaged.
Download the jruby source zip, extract it and open the build.xml file, search for “rspec” (there’s two occurences) and you’ll find that it’s passed in as an argument to the gem installer, add in another line with “cucumber”:
<target name="install-gems">
<property name="jruby.home" value="${basedir}"/>
<java classname="org.jruby.Main" fork="true" maxmemory="${jruby.launch.memory}" failonerror="true">
<classpath refid="build.classpath"/>
<classpath path="${jruby.classes.dir}"/>
<sysproperty key="jruby.home" value="${jruby.home}"/>
<arg value="--command"/>
<arg value="maybe_install_gems"/>
<arg value="rspec"/>
<arg value="rake"/>
<arg value="cucumber"/> <!-- add cucumber -->
<arg value="--env-shebang"/>
</java>
</target>
Then run ant:
$ ant jar-complete
To verify that everything is fine:
$ java -jar lib/jruby-complete.jar -S gem list *** LOCAL GEMS *** builder (2.1.2) cucumber (0.2.3) diff-lcs (1.1.2) polyglot (0.2.5) rake (0.8.4) rspec (1.2.2) sources (0.0.1) term-ansicolor (1.0.3) treetop (1.2.5)
Great we’ve now managed to package jruby-in-a-jar with some additional gems. Now to run cucumber on jruby in eclipse.
Making Eclipse Plug-ins using JRuby or Groovy
Read more about using JRuby or Groovy to write eclipse plugins here: http://dev.eclipse.org/blogs/wayne/2007/07/26/making-eclipse-plug-ins-using-jruby-or-groovy/

