Thursday, May 14, 2009

OCRA: One-Click Ruby Application Builder

I recently mentioned the fact that RubyScript2Exe 0.5.3 doesn't play well with recent versions of RubyGems. At the end of that post I mentioned that there are alternatives emerging for creating executables from your Ruby code, including Lars Christensen's OCRA, the One-Click Ruby Application Builder.

I've had a chance to take OCRA for a short test drive and it looks promising. Like Erik Veenstra's RubyScript2Exe, OCRA lets you "compile" your ruby code into an EXE file that you can distribute to others, without requiring that the users have Ruby installed on their PCs. To quote the OCRA page:

The executable is a self-extracting, self-running executable that contains the Ruby interpreter, your source code and any additionally needed ruby libraries or DLL.

OCRA can be installed via RubyGems: open a console window and type:

gem install ocra

It's also available from the RubyForge page. Version 1.0.2 is the latest as of this writing.

The syntax to compile a script is...

ocra.rb [option] your/script.rb

...where option is one or more of the following:

--dll dllname Include additional DLLs from the Ruby bindir.
--no-lzma Disable LZMA compression of the executable.
--quiet Suppress output.
--help Display this information.
--windows Force Windows application (rubyw.exe)
--console Force console application (ruby.exe)
--no-autoload Don't load/include script.rb's autoloads

Thus far, I have exercised only the --windows and --console options.

To create a non-console application, either use the --windows option or give your script the .rbw filename extension. Compiling a basic non-console script is as simple as opening a console window, navigating to the folder containing your script, and typing:

ocra.rb myscript.rbw

OCRA will then run your script to check for dependencies, gather the necessary files, and create your executable. An option to avoid fully running the script would be nice, similar to RubyScript2Exe's exit if RUBYSCRIPT2EXE.is_compiling? idiom.

OCRA uses LZMA compression, so the resulting executable is relatively small. A simple wxRuby app, for example, resulted in a 2.5 Mb executable. The same app compiled with RubyScript2Exe weighed in at over 9 Mb. Apps that do not require a GUI toolkit will be even smaller, perhaps 500k.

I haven't spent a lot of time with OCRA yet, but I think it shows great potential and I want to thank Lars Christensen for his efforts. If you have a need to distribute Ruby programs to non-technical users, you should check it out and pass your feedback along to Lars.

Thanks for stopping by!

17 comments:

BackOrder said...

"An option to avoid fully running the script would be nice, similar to RubyScript2Exe's exit if RUBYSCRIPT2EXE.is_compiling? idiom."

exit if Object.const_defined?(:Ocra)

Enjoy.

Anonymous said...

Thanks. With OCRA and your clear directions it produced an executable. However, when I tried to run the executable, nothing happened, even when I right clicked the .exe file and selected "Run as ..." and deselected the option to protect my computer and data before clicking OK. I am using wxRuby for the windowing interface and am wondering if that is not working within an OCRA created executable. Any thoughts would be appreciated.

Ross
rossgoodell@hotmail.com

David Mullet said...

@Ross:

I saved the following code...

http://pastie.org/487330

...as "wxtest.rbw".

I then opened a console window, navigated to the directory containing the above script, and executed the command:

ocra.rb wxtest.rbw

OCRA compiled the code into "wxtest.exe", which runs as it should, either from the command line or by double-clicking the file icon.

This is on Vista.

Hope that helps. Feel free to send me your code.

David

David Mullet said...

Thanks, BackOrder!

Your suggestion worked like a charm for me, and I mentioned it in my latest post here.

David

Anonymous said...

Thanks to David's help I discovered that my program works okay with the latest version of ocra (ocrasa.rb) after I installed the latest version of wxRuby (2.0.0), which also brought in Ruby 1.9.1.

Ross Goodell
rossgoodell@hotmail.com

Anonymous said...

I have used David's code:
http://pastie.org/487330
and the latest version of wxruby.
Having thread error:

C:/DOCUME~1/compile/LOCALS~1/Temp/seb1F.tmp/lib/ruby/site_ruby/1.8/rubygems.rb:2
11:in `synchronize': stopping only thread (ThreadError)

Anonymous said...

Install the latest rubygems version to avoid Thread error

JP said...

I still have a console window popping up, even though I'm 'compiling' a .rbw file. Any ideas?

If you're so inclined you can see my code at http://github.com/jphastings/WinWebloc

John said...

I've got a script that uses the pdf-reader gem. Pdf-reader stores some info in text files inside the gem directories. OCRA doesn't automatically include these text files. How can I get them included automatically, or manually add them to the right place?

Manny said...

To all the people responsible for making this program work, I salute you. Thank you! Thank you! Thank you!
Works beautifully! Keep it up!

Anonymous said...

Tried using the DATA idiom & got the following:

./boc.rb:33: uninitialized constant DATA (NameError)
from C:/Ruby187/lib/ruby/gems/1.8/gems/ocra-1.2.0/bin/ocra:647:in `load'
from C:/Ruby187/lib/ruby/gems/1.8/gems/ocra-1.2.0/bin/ocra:647
from C:/Ruby187/bin/ocra:19:in `load'
from C:/Ruby187/bin/ocra:19

That's really the only twizzle I found tho. Great job!

David Mullet said...

@ Anonymous:

The problem using the DATA idiom is a documented issue:

https://github.com/larsch/ocra/issues#issue/6

So there's probably a good chance it will be addressed in near-future releases.

-David

Anonymous said...

https://github.com/larsch/ocra/issues#issue/6

Absolutely fantastic. Thanks. The issue is simple to work around - just include a string constant with whatever DATA & load it into a StringIO object. From there on out it works the same way.
-SHAWN-

Anonymous said...

And now for the good news! It works with SHOES as well !!!! (at least on win xp).

I used Green Shoes (a shoes variant installed as a gem).

I followed this procedure to install Green Shoes:

http://vgoff.posterous.com/greenshoes-gem-is-now-on-rubygemsorg

and then: gem install ocra

But always check for up-to-date informations on Green Shoes at http://vgoff.posterous.com/ on how to install Green Shoes.

Hope this helped. Bye.
Oh, and thanks for Ocra, it's great.

Facebook Application Developers said...

is ruby language helpfull in facebook applications ??

OCRA_newbie said...

Hi,

I converted a .rb file using ocra. The resultant exe runs fine on my windows PC (which has ruby) but gives an error on other windows PCs...

"Windows can't find 'rubyw'. Make sure you typed the name correctly, and then try again. To search for a file, click the Start button, and then click Search."...

Any tips????

Anonymous said...

Hi Team

Ocra rocks.

How to add dependencies w.r.t ruby script and need to know does this output.exe can be De-compile to check source code of ruby script.
if yes how please send link.